Fix checkbox functionality (#541)
* Fix checkbox functionality * Remove class QuillCheckboxpull/555/head
parent
aa213a5e9e
commit
d2444a2af0
6 changed files with 102 additions and 81 deletions
@ -1,60 +0,0 @@ |
||||
import 'package:flutter/material.dart'; |
||||
|
||||
class QuillCheckbox extends StatelessWidget { |
||||
const QuillCheckbox({ |
||||
Key? key, |
||||
this.style, |
||||
this.width, |
||||
this.isChecked = false, |
||||
this.offset, |
||||
this.onTap, |
||||
this.uiBuilder, |
||||
}) : super(key: key); |
||||
final TextStyle? style; |
||||
final double? width; |
||||
final bool isChecked; |
||||
final int? offset; |
||||
final Function(int, bool)? onTap; |
||||
final QuillCheckboxBuilder? uiBuilder; |
||||
|
||||
void _onCheckboxClicked(bool? newValue) { |
||||
if (onTap != null && newValue != null && offset != null) { |
||||
onTap!(offset!, newValue); |
||||
} |
||||
} |
||||
|
||||
@override |
||||
Widget build(BuildContext context) { |
||||
Widget child; |
||||
if (uiBuilder != null) { |
||||
child = uiBuilder!.build( |
||||
context: context, |
||||
isChecked: isChecked, |
||||
onChanged: _onCheckboxClicked, |
||||
); |
||||
} else { |
||||
child = Container( |
||||
alignment: AlignmentDirectional.topEnd, |
||||
width: width, |
||||
padding: const EdgeInsetsDirectional.only(end: 13), |
||||
child: GestureDetector( |
||||
onLongPress: () => _onCheckboxClicked(!isChecked), |
||||
child: Checkbox( |
||||
value: isChecked, |
||||
onChanged: _onCheckboxClicked, |
||||
), |
||||
), |
||||
); |
||||
} |
||||
|
||||
return child; |
||||
} |
||||
} |
||||
|
||||
abstract class QuillCheckboxBuilder { |
||||
Widget build({ |
||||
required BuildContext context, |
||||
required bool isChecked, |
||||
required void Function(bool?) onChanged, |
||||
}); |
||||
} |
@ -0,0 +1,78 @@ |
||||
import 'package:flutter/material.dart'; |
||||
|
||||
class CheckboxPoint extends StatefulWidget { |
||||
const CheckboxPoint({ |
||||
required this.size, |
||||
required this.value, |
||||
required this.enabled, |
||||
required this.onChanged, |
||||
this.uiBuilder, |
||||
Key? key, |
||||
}) : super(key: key); |
||||
|
||||
final double size; |
||||
final bool value; |
||||
final bool enabled; |
||||
final ValueChanged<bool> onChanged; |
||||
final QuillCheckboxBuilder? uiBuilder; |
||||
|
||||
@override |
||||
_CheckboxPointState createState() => _CheckboxPointState(); |
||||
} |
||||
|
||||
class _CheckboxPointState extends State<CheckboxPoint> { |
||||
@override |
||||
Widget build(BuildContext context) { |
||||
if (widget.uiBuilder != null) { |
||||
return widget.uiBuilder!.build( |
||||
context: context, |
||||
isChecked: widget.value, |
||||
onChanged: widget.onChanged, |
||||
); |
||||
} |
||||
final theme = Theme.of(context); |
||||
final fillColor = widget.value |
||||
? (widget.enabled |
||||
? theme.colorScheme.primary |
||||
: theme.colorScheme.onSurface.withOpacity(0.5)) |
||||
: theme.colorScheme.surface; |
||||
final borderColor = widget.value |
||||
? (widget.enabled |
||||
? theme.colorScheme.primary |
||||
: theme.colorScheme.onSurface.withOpacity(0)) |
||||
: (widget.enabled |
||||
? theme.colorScheme.onSurface.withOpacity(0.5) |
||||
: theme.colorScheme.onSurface.withOpacity(0.3)); |
||||
return Center( |
||||
child: SizedBox( |
||||
width: widget.size, |
||||
height: widget.size, |
||||
child: Material( |
||||
color: fillColor, |
||||
shape: RoundedRectangleBorder( |
||||
side: BorderSide( |
||||
color: borderColor, |
||||
), |
||||
borderRadius: BorderRadius.circular(2), |
||||
), |
||||
child: InkWell( |
||||
onTap: |
||||
widget.enabled ? () => widget.onChanged(!widget.value) : null, |
||||
child: widget.value |
||||
? Icon(Icons.check, |
||||
size: widget.size, color: theme.colorScheme.onPrimary) |
||||
: null, |
||||
), |
||||
), |
||||
), |
||||
); |
||||
} |
||||
} |
||||
|
||||
abstract class QuillCheckboxBuilder { |
||||
Widget build({ |
||||
required BuildContext context, |
||||
required bool isChecked, |
||||
required ValueChanged<bool> onChanged, |
||||
}); |
||||
} |
@ -1,3 +1,3 @@ |
||||
export 'bullet_point.dart'; |
||||
export 'checkbox.dart'; |
||||
export 'checkbox_point.dart'; |
||||
export 'number_point.dart'; |
||||
|
Loading…
Reference in new issue