feat: Add checkbox as option to the flutter input switch which is now called FlutterFormInputBool

This commit is contained in:
Jacques 2024-02-06 10:29:04 +01:00
parent 8eb1d80a9f
commit 8d1ad0ca02
8 changed files with 166 additions and 90 deletions

View file

@ -60,3 +60,6 @@
## 2.7.0
* 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

View file

@ -55,7 +55,7 @@ class _MyHomePageState extends State<MyHomePage> {
children: [
Container(height: 10),
const Text('FlutterFormInputSwitch'),
FlutterFormInputSwitch(
FlutterFormInputBool(
initialValue: true,
onChanged: (v) {
debugPrint('Switch changed to $v');

View file

@ -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"

View file

@ -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,
}

View file

@ -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<bool> {
BoolFormField({
Key? key,
required FormFieldSetter<bool> onSaved,
required FormFieldValidator<bool> 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<bool> 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<bool> state;
final FocusNode? focusNode;
final void Function(bool? value)? onChanged;
final BoolWidgetType widgetType;
final String? errorText;
final Widget? leftWidget;
final Widget? rightWidget;
@override
State<BoolWidget> createState() => _BoolWidgetState();
}
class _BoolWidgetState extends State<BoolWidget> {
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,
),
],
],
);
}
}

View file

@ -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';

View file

@ -1,68 +0,0 @@
// SPDX-FileCopyrightText: 2022 Iconica
//
// SPDX-License-Identifier: BSD-3-Clause
import 'package:flutter/material.dart';
class SwitchFormField extends FormField<bool> {
SwitchFormField({
Key? key,
required FormFieldSetter<bool> onSaved,
required FormFieldValidator<bool> 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<bool> 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<bool> state;
final FocusNode? focusNode;
final void Function(bool? value)? onChanged;
@override
State<SwitchWidget> createState() => _SwitchWidgetState();
}
class _SwitchWidgetState extends State<SwitchWidget> {
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;
});
},
);
}
}

View file

@ -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: