Skip to content

Instantly share code, notes, and snippets.

@carloswm85
Created November 29, 2023 13:39
Show Gist options
  • Select an option

  • Save carloswm85/193dd064c6aa5f19244df9c65008fe0f to your computer and use it in GitHub Desktop.

Select an option

Save carloswm85/193dd064c6aa5f19244df9c65008fe0f to your computer and use it in GitHub Desktop.
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