toggle.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'package:app_flowy/workspace/presentation/widgets/toggle/toggle_style.dart';
  2. import 'package:flowy_infra/color_extension.dart';
  3. import 'package:flutter/material.dart';
  4. class Toggle extends StatelessWidget {
  5. final ToggleStyle style;
  6. final bool value;
  7. final Color? thumbColor;
  8. final Color? activeBackgroundColor;
  9. final Color? inactiveBackgroundColor;
  10. final void Function(bool) onChanged;
  11. final EdgeInsets padding;
  12. const Toggle({
  13. Key? key,
  14. required this.value,
  15. required this.onChanged,
  16. required this.style,
  17. this.thumbColor,
  18. this.activeBackgroundColor,
  19. this.inactiveBackgroundColor,
  20. this.padding = const EdgeInsets.all(8.0),
  21. }) : super(key: key);
  22. @override
  23. Widget build(BuildContext context) {
  24. final backgroundColor = value
  25. ? activeBackgroundColor ?? Theme.of(context).colorScheme.primary
  26. : activeBackgroundColor ?? CustomColors.of(context).toggleOffFill;
  27. return GestureDetector(
  28. onTap: (() => onChanged(value)),
  29. child: Padding(
  30. padding: padding,
  31. child: Stack(
  32. children: [
  33. Container(
  34. height: style.height,
  35. width: style.width,
  36. decoration: BoxDecoration(
  37. color: backgroundColor,
  38. borderRadius: BorderRadius.circular(style.height / 2),
  39. ),
  40. ),
  41. AnimatedPositioned(
  42. duration: const Duration(milliseconds: 150),
  43. top: (style.height - style.thumbRadius) / 2,
  44. left: value ? style.width - style.thumbRadius - 1 : 1,
  45. child: Container(
  46. height: style.thumbRadius,
  47. width: style.thumbRadius,
  48. decoration: BoxDecoration(
  49. color: thumbColor ?? Theme.of(context).colorScheme.onPrimary,
  50. borderRadius: BorderRadius.circular(style.thumbRadius / 2),
  51. ),
  52. ),
  53. ),
  54. ],
  55. ),
  56. ),
  57. );
  58. }
  59. }