Skip to content

Commit

Permalink
components/input/tonal_switch: implement TonalSwitch
Browse files Browse the repository at this point in the history
  • Loading branch information
arnemolland committed Jan 26, 2023
1 parent de7ef4d commit bc23099
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/src/components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
69 changes: 69 additions & 0 deletions lib/src/components/input/tonal_switch.dart
Original file line number Diff line number Diff line change
@@ -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<bool> 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<bool> 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<bool> 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,
);
}
}

0 comments on commit bc23099

Please sign in to comment.