From bc23099a38d91f3651928847ec092ea59fc4f36a Mon Sep 17 00:00:00 2001 From: Arne Molland Date: Thu, 26 Jan 2023 20:25:15 +0100 Subject: [PATCH] components/input/tonal_switch: implement TonalSwitch --- lib/src/components.dart | 1 + lib/src/components/input/tonal_switch.dart | 69 ++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 lib/src/components/input/tonal_switch.dart diff --git a/lib/src/components.dart b/lib/src/components.dart index f96e007..3c04052 100644 --- a/lib/src/components.dart +++ b/lib/src/components.dart @@ -8,6 +8,7 @@ export 'components/material/top_bar.dart'; export 'components/input/input_field.dart'; export 'components/input/validation_message.dart'; +export 'components/input/tonal_switch.dart'; export 'components/icons.dart'; export 'components/shimmer.dart'; diff --git a/lib/src/components/input/tonal_switch.dart b/lib/src/components/input/tonal_switch.dart new file mode 100644 index 0000000..0a1fc81 --- /dev/null +++ b/lib/src/components/input/tonal_switch.dart @@ -0,0 +1,69 @@ +import 'dart:io'; + +import 'package:flume/flume.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +class TonalSwitch extends StatelessWidget { + const TonalSwitch({super.key, required this.value, required this.onChanged}); + + final bool value; + final ValueChanged onChanged; + + @override + Widget build(BuildContext context) { + if (Platform.isMacOS || Platform.isIOS) { + return _CupertinoSwitch(key: key, value: value, onChanged: onChanged); + } else { + return _MaterialSwitch(key: key, value: value, onChanged: onChanged); + } + } +} + +class _CupertinoSwitch extends StatelessWidget { + const _CupertinoSwitch({ + super.key, + required this.value, + required this.onChanged, + }); + + final bool value; + final ValueChanged onChanged; + + @override + Widget build(BuildContext context) { + return CupertinoSwitch( + value: value, + onChanged: onChanged, + activeColor: context.ambiance.palette.light, + trackColor: context.ambiance.palette.mediumDark, + thumbColor: value + ? context.ambiance.palette.dark + : context.ambiance.palette.light, + ); + } +} + +class _MaterialSwitch extends StatelessWidget { + const _MaterialSwitch({ + super.key, + required this.value, + required this.onChanged, + }); + + final bool value; + final ValueChanged onChanged; + + @override + Widget build(BuildContext context) { + return Switch( + value: value, + onChanged: onChanged, + activeColor: context.ambiance.palette.dark, + activeTrackColor: context.ambiance.palette.light, + inactiveThumbColor: context.ambiance.palette.light, + inactiveTrackColor: context.ambiance.palette.mediumDark, + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + ); + } +}