Created
May 22, 2024 13:31
-
-
Save carloswm85/a2c1b91ca9bd11c1bca4c02da15efb0a to your computer and use it in GitHub Desktop.
This file contains 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
/** 1 */ | |
// Inside build method, in form widget | |
void toggleDropdownEnabled(bool val) { | |
ref | |
.read(providerTrackerDrowndownIndustrialDegree.notifier) | |
.update((state) => val); | |
} | |
/** 2 */ | |
// Inside form | |
CustomButtonSwitch( | |
title: '🟫 ¿Es mineral industrializado?', | |
subtitle: 'Según decreto Nº 525/2010', | |
controller: _isIndustrializedMineral, | |
onPressed: toggleDropdownEnabled, | |
), | |
/** 3 - SWITCH */ | |
import 'package:flutter/material.dart'; | |
class CustomButtonSwitch extends StatefulWidget { | |
final String title; | |
final String subtitle; | |
final SwitchController controller; | |
final void Function(bool)? onPressed; | |
const CustomButtonSwitch({ | |
super.key, | |
required this.title, | |
required this.subtitle, | |
required this.controller, | |
this.onPressed, | |
}); | |
@override | |
State<StatefulWidget> createState() => _CustomButtonSwitchState(); | |
} | |
class _CustomButtonSwitchState extends State<CustomButtonSwitch> { | |
@override | |
Widget build(BuildContext context) { | |
final border = BorderRadius.circular(10); | |
final colors = Theme.of(context).colorScheme; | |
return Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: SwitchListTile( | |
contentPadding: const EdgeInsets.fromLTRB(15, 0, 15, 5), | |
dense: false, | |
title: Text(widget.title), | |
subtitle: Text(widget.subtitle), | |
value: widget.controller.isSwitchOn, | |
shape: RoundedRectangleBorder( | |
side: BorderSide(color: colors.onBackground), borderRadius: border), | |
onChanged: (bool newValue) { | |
setState(() { | |
widget.onPressed!(newValue); | |
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