toggle.dart 1.6 KB

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