Widget _switchShowcase(bool showCupertinoSwitches){
RxBool isOn1 = true.obs;
final ColorScheme colorScheme = Theme.of(context).colorScheme;
final bool isLight = Theme.of(context).brightness == Brightness.light;
return RepaintBoundary(
child: Obx(
()=> Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 8,
runSpacing: 8,
children: <Widget>[
if (showCupertinoSwitches) const Text('M3:'),
Switch(
value: isOn1.value,
onChanged: (bool value) {
isOn1.value = value;
},
),
Switch(
value: !isOn1.value,
onChanged: (bool value) {
isOn1.value = !value;
},
),
Switch(
thumbIcon: MaterialStateProperty.resolveWith<Icon?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return Icon(Icons.check,
color:
isLight ? colorScheme.primary : colorScheme.onPrimary);
}
// All other states will use the default thumbIcon.
return Icon(Icons.close, color: colorScheme.onPrimary);
}),
value: isOn1.value,
onChanged: (bool value) {
isOn1.value = value;
},
),
const Text('Disabled'),
Switch(
value: isOn1.value,
onChanged: null,
),
Switch(
value: !isOn1.value,
onChanged: null,
),
if (showCupertinoSwitches) ...<Widget>[
const Text('iOS'),
CupertinoSwitch(
activeColor: colorScheme.primary,
value: isOn1.value,
onChanged: (bool value) {
isOn1.value = value;
},
),
CupertinoSwitch(
activeColor: colorScheme.primary,
value: isOn1.value,
onChanged: null,
),
CupertinoSwitch(
activeColor: colorScheme.primary,
value: !isOn1.value,
onChanged: (bool value) {
isOn1.value = !value;
},
),
CupertinoSwitch(
activeColor: colorScheme.primary,
value: !isOn1.value,
onChanged: null,
),
],
],
),
),
);
}
SwitchScreen.dart
class SwitchScreen extends StatelessWidget {
const SwitchScreen({super.key});
ThemeController themeController = Get.find();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.background,
appBar: showAppBar
? AppBar(
title: const Text("Switch Theme"),
)
: null,
body: Center(
child: Container(
constraints: const BoxConstraints(
maxWidth: 1000,
),
alignment: Alignment.topCenter,
padding: const EdgeInsets.all(16),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Obx(
() => CustomContainer(
title: 'Switch main color',
description: 'Background color',
child: ThemePopupMenu3(
schemeIndex:
themeController.switchSchemeIndex.value,
onChanged: themeController.setSwitchSchemeColor),
),
),
const SizedBox(
height: 20,
),
_switchShowcase(false),
const SizedBox(
height: 20,
),
Obx(
() => CustomContainer(
title: 'Switch On State color',
description: 'Hover/pressed/Focused color',
child: ThemePopupMenu3(
schemeIndex: themeController
.switchOnStateSchemeIndex.value,
onChanged: themeController
.setSwitchOnStateSchemeColor),
),
),
const SizedBox(height: 20,),
Obx(
()=> SwitchListTileReveal(
title: const Text('Thumb size is fixed'),
subtitle: const Text('Turn ON to keep the Switch thumb the same '
'size when Switch is ON or OFF. Only available in Material-3 '
'mode. When on the Material-3 Switch looks even more like an '
'iOS Switch.\n'),
value: themeController.switchThumbFixedSize.value &&
themeController.useMaterial3.value,
onChanged:
themeController.useMaterial3.value
? themeController.setSwitchThumbFixedSize
: null,
),
),
],
),
),
),
);
}
Widget _switchShowcase(bool showCupertinoSwitches){
RxBool isOn1 = true.obs;
final ColorScheme colorScheme = Theme.of(context).colorScheme;
final bool isLight = Theme.of(context).brightness == Brightness.light;
return RepaintBoundary(
child: Obx(
()=> Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 8,
runSpacing: 8,
children: <Widget>[
if (showCupertinoSwitches) const Text('M3:'),
Switch(
value: isOn1.value,
onChanged: (bool value) {
isOn1.value = value;
},
),
Switch(
value: !isOn1.value,
onChanged: (bool value) {
isOn1.value = !value;
},
),
Switch(
thumbIcon: MaterialStateProperty.resolveWith<Icon?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return Icon(Icons.check,
color:
isLight ? colorScheme.primary : colorScheme.onPrimary);
}
// All other states will use the default thumbIcon.
return Icon(Icons.close, color: colorScheme.onPrimary);
}),
value: isOn1.value,
onChanged: (bool value) {
isOn1.value = value;
},
),
const Text('Disabled'),
Switch(
value: isOn1.value,
onChanged: null,
),
Switch(
value: !isOn1.value,
onChanged: null,
),
if (showCupertinoSwitches) ...<Widget>[
const Text('iOS'),
CupertinoSwitch(
activeColor: colorScheme.primary,
value: isOn1.value,
onChanged: (bool value) {
isOn1.value = value;
},
),
CupertinoSwitch(
activeColor: colorScheme.primary,
value: isOn1.value,
onChanged: null,
),
CupertinoSwitch(
activeColor: colorScheme.primary,
value: !isOn1.value,
onChanged: (bool value) {
isOn1.value = !value;
},
),
CupertinoSwitch(
activeColor: colorScheme.primary,
value: !isOn1.value,
onChanged: null,
),
],
],
),
),
);
}
}