From 8d1ad0ca022be45d06297ee7eb97d3f02e13e3fd Mon Sep 17 00:00:00 2001 From: Jacques Date: Tue, 6 Feb 2024 10:29:04 +0100 Subject: [PATCH] feat: Add checkbox as option to the flutter input switch which is now called FlutterFormInputBool --- CHANGELOG.md | 5 +- example/lib/main.dart | 2 +- example/pubspec.lock | 28 ++-- .../{switch/switch.dart => bool/bool.dart} | 22 ++- lib/src/inputs/bool/bool_field.dart | 127 ++++++++++++++++++ lib/src/inputs/inputs.dart | 2 +- lib/src/inputs/switch/switch_field.dart | 68 ---------- pubspec.yaml | 2 +- 8 files changed, 166 insertions(+), 90 deletions(-) rename lib/src/inputs/{switch/switch.dart => bool/bool.dart} (60%) create mode 100644 lib/src/inputs/bool/bool_field.dart delete mode 100644 lib/src/inputs/switch/switch_field.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cbed76..a695ad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,4 +59,7 @@ * Addition of 'obscureText' parameter to 'FlutterFormInputPlainText' ## 2.7.0 -* Addition of 'decoration' parameter to 'FlutterFormInputPassword' \ No newline at end of file +* Addition of 'decoration' parameter to 'FlutterFormInputPassword' + +## 3.0.0 +* Updated the FlutterFormInputSwitch to FlutterFormInputBool. This now includes a parameter to either show a checkbox or switch \ No newline at end of file diff --git a/example/lib/main.dart b/example/lib/main.dart index 211cc22..738077d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -55,7 +55,7 @@ class _MyHomePageState extends State { children: [ Container(height: 10), const Text('FlutterFormInputSwitch'), - FlutterFormInputSwitch( + FlutterFormInputBool( initialValue: true, onChanged: (v) { debugPrint('Switch changed to $v'); diff --git a/example/pubspec.lock b/example/pubspec.lock index 2b34ecf..4365e71 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -37,10 +37,10 @@ packages: dependency: transitive description: name: collection - sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.2" + version: "1.18.0" cupertino_icons: dependency: "direct main" description: @@ -68,7 +68,7 @@ packages: path: ".." relative: true source: path - version: "2.4.0" + version: "2.7.0" flutter_lints: dependency: "direct dev" description: @@ -118,10 +118,10 @@ packages: dependency: transitive description: name: meta - sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" path: dependency: transitive description: @@ -147,18 +147,18 @@ packages: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" string_scanner: dependency: transitive description: @@ -179,10 +179,10 @@ packages: dependency: transitive description: name: test_api - sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.6.1" vector_math: dependency: transitive description: @@ -195,10 +195,10 @@ packages: dependency: transitive description: name: web - sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 url: "https://pub.dev" source: hosted - version: "0.1.4-beta" + version: "0.3.0" sdks: - dart: ">=3.1.0-185.0.dev <4.0.0" + dart: ">=3.2.0-194.0.dev <4.0.0" flutter: ">=1.17.0" diff --git a/lib/src/inputs/switch/switch.dart b/lib/src/inputs/bool/bool.dart similarity index 60% rename from lib/src/inputs/switch/switch.dart rename to lib/src/inputs/bool/bool.dart index c0962f2..826836e 100644 --- a/lib/src/inputs/switch/switch.dart +++ b/lib/src/inputs/bool/bool.dart @@ -4,17 +4,20 @@ import 'package:flutter/material.dart'; -import 'package:flutter_input_library/src/inputs/switch/switch_field.dart'; +import 'package:flutter_input_library/src/inputs/bool/bool_field.dart'; -class FlutterFormInputSwitch extends StatelessWidget { +class FlutterFormInputBool extends StatelessWidget { final Widget? label; final Function(bool?)? onSaved; final String? Function(bool?)? validator; final Function(bool?)? onChanged; final bool? initialValue; final FocusNode? focusNode; + final BoolWidgetType widgetType; + final Widget? leftWidget; + final Widget? rightWidget; - const FlutterFormInputSwitch({ + const FlutterFormInputBool({ Key? key, this.label, this.onSaved, @@ -22,18 +25,29 @@ class FlutterFormInputSwitch extends StatelessWidget { this.onChanged, this.focusNode, this.initialValue = false, + this.widgetType = BoolWidgetType.switchWidget, + this.leftWidget, + this.rightWidget, }) : super( key: key, ); @override Widget build(BuildContext context) { - return SwitchFormField( + return BoolFormField( onSaved: (value) => onSaved?.call(value), onChanged: (value) => onChanged?.call(value), validator: (value) => validator?.call(value), initialValue: initialValue ?? false, focusNode: focusNode, + widgetType: widgetType, + leftWidget: leftWidget, + rightWidget: rightWidget, ); } } + +enum BoolWidgetType { + switchWidget, + checkbox, +} diff --git a/lib/src/inputs/bool/bool_field.dart b/lib/src/inputs/bool/bool_field.dart new file mode 100644 index 0000000..7fd6970 --- /dev/null +++ b/lib/src/inputs/bool/bool_field.dart @@ -0,0 +1,127 @@ +// SPDX-FileCopyrightText: 2022 Iconica +// +// SPDX-License-Identifier: BSD-3-Clause + +import 'package:flutter/material.dart'; +import 'package:flutter_input_library/flutter_input_library.dart'; + +class BoolFormField extends FormField { + BoolFormField({ + Key? key, + required FormFieldSetter onSaved, + required FormFieldValidator validator, + FocusNode? focusNode, + bool initialValue = false, + bool autovalidate = false, + void Function(bool? value)? onChanged, + BoolWidgetType widgetType = BoolWidgetType.switchWidget, + Widget? leftWidget, + Widget? rightWidget, + }) : super( + key: key, + onSaved: onSaved, + validator: validator, + initialValue: initialValue, + builder: (FormFieldState state) { + return BoolWidget( + initialValue: initialValue, + state: state, + focusNode: focusNode, + onChanged: onChanged, + widgetType: widgetType, + errorText: state.errorText, + leftWidget: leftWidget, + rightWidget: rightWidget, + ); + }); +} + +class BoolWidget extends StatefulWidget { + const BoolWidget({ + this.initialValue = false, + required this.state, + this.onChanged, + this.focusNode, + this.widgetType = BoolWidgetType.switchWidget, + this.errorText, + this.leftWidget, + this.rightWidget, + super.key, + }); + + final bool initialValue; + final FormFieldState state; + final FocusNode? focusNode; + final void Function(bool? value)? onChanged; + final BoolWidgetType widgetType; + final String? errorText; + final Widget? leftWidget; + final Widget? rightWidget; + + @override + State createState() => _BoolWidgetState(); +} + +class _BoolWidgetState extends State { + late Widget child; + late bool value = widget.initialValue; + + void onChanged(bool value) { + widget.onChanged?.call(value); + + widget.state.didChange(value); + + setState(() { + this.value = value; + }); + } + + @override + Widget build(BuildContext context) { + var theme = Theme.of(context); + + late Widget child; + + switch (widget.widgetType) { + case BoolWidgetType.switchWidget: + child = Switch( + value: value, + focusNode: widget.focusNode, + onChanged: onChanged, + ); + break; + + case BoolWidgetType.checkbox: + child = Checkbox( + value: value, + focusNode: widget.focusNode, + onChanged: (bool? value) { + if (value != null) { + onChanged(value); + } + }, + ); + break; + } + + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + Row( + mainAxisSize: MainAxisSize.min, + children: [ + widget.leftWidget ?? const SizedBox.shrink(), + child, + widget.rightWidget ?? const SizedBox.shrink(), + ], + ), + if (widget.errorText != null) ...[ + Text( + widget.errorText!, + style: theme.inputDecorationTheme.errorStyle, + ), + ], + ], + ); + } +} diff --git a/lib/src/inputs/inputs.dart b/lib/src/inputs/inputs.dart index 9f74a31..dfe8205 100644 --- a/lib/src/inputs/inputs.dart +++ b/lib/src/inputs/inputs.dart @@ -3,6 +3,6 @@ export 'number_picker/number_picker.dart'; export 'text/password.dart'; export 'text/plain_text.dart'; export 'slider/slider.dart'; -export 'switch/switch.dart'; +export 'bool/bool.dart'; export 'date_picker/date_picker.dart'; export 'scroll_picker/scroll_picker.dart'; diff --git a/lib/src/inputs/switch/switch_field.dart b/lib/src/inputs/switch/switch_field.dart deleted file mode 100644 index 6720f1d..0000000 --- a/lib/src/inputs/switch/switch_field.dart +++ /dev/null @@ -1,68 +0,0 @@ -// SPDX-FileCopyrightText: 2022 Iconica -// -// SPDX-License-Identifier: BSD-3-Clause - -import 'package:flutter/material.dart'; - -class SwitchFormField extends FormField { - SwitchFormField({ - Key? key, - required FormFieldSetter onSaved, - required FormFieldValidator validator, - FocusNode? focusNode, - bool initialValue = false, - bool autovalidate = false, - void Function(bool? value)? onChanged, - }) : super( - key: key, - onSaved: onSaved, - validator: validator, - initialValue: initialValue, - builder: (FormFieldState state) { - return SwitchWidget( - initialValue: initialValue, - state: state, - focusNode: focusNode, - onChanged: onChanged, - ); - }); -} - -class SwitchWidget extends StatefulWidget { - const SwitchWidget({ - this.initialValue = false, - required this.state, - this.onChanged, - this.focusNode, - super.key, - }); - - final bool initialValue; - final FormFieldState state; - final FocusNode? focusNode; - final void Function(bool? value)? onChanged; - - @override - State createState() => _SwitchWidgetState(); -} - -class _SwitchWidgetState extends State { - late bool value = widget.initialValue; - - @override - Widget build(BuildContext context) { - return Switch( - value: value, - focusNode: widget.focusNode, - onChanged: (bool value) { - widget.onChanged?.call(value); - - widget.state.didChange(value); - - setState(() { - this.value = value; - }); - }, - ); - } -} diff --git a/pubspec.yaml b/pubspec.yaml index 145ad3a..fbe59f8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_input_library description: A new Flutter package project. -version: 2.7.0 +version: 3.0.0 repository: https://github.com/Iconica-Development/flutter_input_library environment: