Created
November 29, 2023 13:39
-
-
Save carloswm85/193dd064c6aa5f19244df9c65008fe0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| class CustomSwitchFormField extends StatefulWidget { | |
| final String title; | |
| final String subtitle; | |
| final SwitchController controller; | |
| const CustomSwitchFormField( | |
| {super.key, | |
| required this.title, | |
| required this.subtitle, | |
| required this.controller}); | |
| @override | |
| State<StatefulWidget> createState() => _CustomSwitchFormFieldState(); | |
| } | |
| class _CustomSwitchFormFieldState extends State<CustomSwitchFormField> { | |
| @override | |
| Widget build(BuildContext context) { | |
| return Center( | |
| child: SwitchListTile( | |
| title: Text(widget.title), | |
| subtitle: Text(widget.subtitle), | |
| value: widget.controller.isSwitchOn, | |
| onChanged: (bool newValue) { | |
| setState(() { | |
| widget.controller.setValue(newValue); | |
| }); | |
| }, | |
| ), | |
| ); | |
| } | |
| } | |
| class SwitchController extends ChangeNotifier { | |
| late bool isSwitchOn = false; | |
| void setValue(bool value) { | |
| isSwitchOn = value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment