From 98cdab0277ed7b2ced13d2ac576c0749c6ce3551 Mon Sep 17 00:00:00 2001 From: reidha Date: Sun, 12 Apr 2020 21:08:32 +0700 Subject: [PATCH] 0.1.0: Update README.md, made small changes --- CHANGELOG.md | 7 ++ lib/checkbox_formfield.dart | 104 +------------------------- lib/checkbox_icon_formfield.dart | 50 +++++++++++++ lib/checkbox_list tile_formfield.dart | 47 ++++++++++++ pubspec.yaml | 3 +- 5 files changed, 108 insertions(+), 103 deletions(-) create mode 100644 lib/checkbox_icon_formfield.dart create mode 100644 lib/checkbox_list tile_formfield.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 8362286..fd29a63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [0.1.0] - April 12, 2020 + +* Big fix: CheckboxIconFormField.enabled was not working +* Default colors can be set by providing `context` properly +* Update README.md + + ## [0.0.1] - April 12, 2020 * Add CheckboxListTileFormField diff --git a/lib/checkbox_formfield.dart b/lib/checkbox_formfield.dart index 19e36da..dd7afee 100644 --- a/lib/checkbox_formfield.dart +++ b/lib/checkbox_formfield.dart @@ -1,104 +1,4 @@ library checkbox_formfield; -import 'package:flutter/material.dart'; - -class CheckboxListTileFormField extends FormField { - CheckboxListTileFormField({ - Key key, - Widget title, - BuildContext context, - FormFieldSetter onSaved, - FormFieldValidator validator, - bool initialValue = false, - bool autovalidate = false, - bool enabled = true, - bool dense = false, - Color activeColor, - Color checkColor = const Color(0xFFFFFFFF), - ListTileControlAffinity listTileControlAffinity = - ListTileControlAffinity.leading, - Widget secondary, - }) : super( - key: key, - onSaved: onSaved, - validator: validator, - initialValue: initialValue, - autovalidate: autovalidate, - builder: (FormFieldState state) { - return CheckboxListTile( - title: title, - dense: dense, - // if active color is null, Theme.of(context).accentColor will be used. - activeColor: activeColor, - checkColor: checkColor, - value: state.value, - onChanged: enabled ? state.didChange : null, - subtitle: state.hasError - ? Text( - state.errorText, - style: context == null - ? TextStyle(color: Colors.red) - : TextStyle(color: Theme.of(context).errorColor), - ) - : null, - controlAffinity: listTileControlAffinity, - secondary: secondary, - ); - }, - ); -} - -class CheckboxIconFormField extends FormField { - CheckboxIconFormField({ - Key key, - BuildContext context, - FormFieldSetter onSaved, - //FormFieldValidator validator, - bool initialValue = false, - bool autovalidate = false, - bool enabled = true, - IconData trueIcon = Icons.check, - Color trueIconColor, - IconData falseIcon = Icons.check_box_outline_blank, - Color falseIconColor, - double padding = 24.0, - double iconSize = 24.0, - }) : super( - key: key, - onSaved: onSaved, - //validator: validator, - initialValue: initialValue, - autovalidate: autovalidate, - builder: (FormFieldState state) { - return Padding( - padding: EdgeInsets.all(padding), - child: _createTappableIcon(state, trueIcon, trueIconColor, - falseIcon, falseIconColor, iconSize), - ); - }, - ); - - static Widget _createTappableIcon( - FormFieldState state, - IconData trueIcon, - Color trueIconColor, - IconData falseIcon, - Color falseIconColor, - double iconSize) { - if (state.value) { - return GestureDetector( - onTap: () { - state.didChange(false); - }, - child: Icon(trueIcon, color: trueIconColor, size: iconSize), - ); - } else { - return GestureDetector( - onTap: () { - state.didChange(true); - }, - child: Icon(falseIcon, color: falseIconColor, size: iconSize), - ); - } - } -} +export './checkbox_list tile_formfield.dart'; +export './checkbox_icon_formfield.dart'; diff --git a/lib/checkbox_icon_formfield.dart b/lib/checkbox_icon_formfield.dart new file mode 100644 index 0000000..6455500 --- /dev/null +++ b/lib/checkbox_icon_formfield.dart @@ -0,0 +1,50 @@ +import 'package:flutter/material.dart'; + +class CheckboxIconFormField extends FormField { + CheckboxIconFormField({ + Key key, + BuildContext context, + FormFieldSetter onSaved, + bool initialValue = false, + bool autovalidate = false, + bool enabled = true, + IconData trueIcon = Icons.check, + IconData falseIcon = Icons.check_box_outline_blank, + Color trueIconColor, + Color falseIconColor, + Color disabledColor, + double padding = 24.0, + double iconSize, + }) : super( + key: key, + onSaved: onSaved, + initialValue: initialValue, + autovalidate: autovalidate, + builder: (FormFieldState state) { + trueIconColor ??= (context == null + ? null + : Theme.of(context).accentIconTheme.color); + + return Padding( + padding: EdgeInsets.all(padding), + child: state.value + ? _createTappableIcon(state, enabled, trueIcon, + trueIconColor, disabledColor, iconSize) + : _createTappableIcon(state, enabled, falseIcon, + falseIconColor, disabledColor, iconSize)); + }, + ); + + static Widget _createTappableIcon(FormFieldState state, bool enabled, + IconData icon, Color iconColor, Color disabledColor, double iconSize) { + return IconButton( + onPressed: enabled + ? () { + state.didChange(!state.value); + } + : null, + icon: Icon(icon, + color: enabled ? iconColor : disabledColor, size: iconSize), + ); + } +} diff --git a/lib/checkbox_list tile_formfield.dart b/lib/checkbox_list tile_formfield.dart new file mode 100644 index 0000000..a62e39c --- /dev/null +++ b/lib/checkbox_list tile_formfield.dart @@ -0,0 +1,47 @@ +import 'package:flutter/material.dart'; + +class CheckboxListTileFormField extends FormField { + CheckboxListTileFormField({ + Key key, + Widget title, + BuildContext context, + FormFieldSetter onSaved, + FormFieldValidator validator, + bool initialValue = false, + bool autovalidate = false, + bool enabled = true, + bool dense = false, + Color errorColor, + Color activeColor, + Color checkColor, + ListTileControlAffinity controlAffinity = ListTileControlAffinity.leading, + Widget secondary, + }) : super( + key: key, + onSaved: onSaved, + validator: validator, + initialValue: initialValue, + autovalidate: autovalidate, + builder: (FormFieldState state) { + errorColor ??= + (context == null ? Colors.red : Theme.of(context).errorColor); + + return CheckboxListTile( + title: title, + dense: dense, + activeColor: activeColor, + checkColor: checkColor, + value: state.value, + onChanged: enabled ? state.didChange : null, + subtitle: state.hasError + ? Text( + state.errorText, + style: TextStyle(color: errorColor), + ) + : null, + controlAffinity: controlAffinity, + secondary: secondary, + ); + }, + ); +} diff --git a/pubspec.yaml b/pubspec.yaml index a5a176e..d8262ce 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,8 @@ name: checkbox_formfield description: A few kinds of checkbox that can be used as FormField -version: 0.0.1 +version: 0.1.0 homepage: https://reidha.github.io/ +repository: https://github.com/reidha/checkbox_formfield environment: sdk: ">=2.1.0 <3.0.0"