switch_button.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vitalapp/pages/medical/models/item.dart';
  4. // ignore: must_be_immutable
  5. class AnimatedToggle extends StatefulWidget {
  6. final List<String> values;
  7. final ValueChanged onToggleCallback;
  8. final Color backgroundColor;
  9. final Color buttonColor;
  10. final Color textColor;
  11. PressureStatus statusValue;
  12. AnimatedToggle({
  13. super.key,
  14. required this.values,
  15. required this.onToggleCallback,
  16. this.backgroundColor = const Color(0xFFe7e7e8),
  17. this.buttonColor = const Color(0xFFFFFFFF),
  18. this.textColor = Colors.black,
  19. this.statusValue = PressureStatus.left,
  20. });
  21. @override
  22. _AnimatedToggleState createState() => _AnimatedToggleState();
  23. }
  24. class _AnimatedToggleState extends State<AnimatedToggle> {
  25. // bool initialPosition = true;
  26. @override
  27. Widget build(BuildContext context) {
  28. return SizedBox(
  29. width: Get.width * 0.24,
  30. height: Get.width * 0.052,
  31. // margin: const EdgeInsets.all(20),
  32. child: Stack(
  33. children: <Widget>[
  34. GestureDetector(
  35. onTap: () {
  36. if (widget.statusValue == PressureStatus.left) {
  37. widget.statusValue = PressureStatus.right;
  38. } else {
  39. widget.statusValue = PressureStatus.left;
  40. }
  41. widget.onToggleCallback(widget.statusValue);
  42. setState(() {});
  43. },
  44. child: Container(
  45. width: Get.width * 0.24,
  46. height: Get.width * 0.052,
  47. decoration: ShapeDecoration(
  48. color: widget.backgroundColor,
  49. shape: RoundedRectangleBorder(
  50. borderRadius: BorderRadius.circular(Get.width * 0.04),
  51. ),
  52. ),
  53. child: Row(
  54. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  55. children: List.generate(
  56. widget.values.length,
  57. (index) => Padding(
  58. padding: EdgeInsets.symmetric(
  59. horizontal: Get.width * 0.4 * 0.05),
  60. child: Text(
  61. widget.values[index],
  62. style: TextStyle(
  63. fontFamily: 'Rubik',
  64. fontSize: Get.width * 0.02,
  65. fontWeight: FontWeight.bold,
  66. color: Colors.black,
  67. ),
  68. ),
  69. ),
  70. ),
  71. ),
  72. ),
  73. ),
  74. AnimatedAlign(
  75. duration: const Duration(milliseconds: 250),
  76. curve: Curves.decelerate,
  77. alignment: widget.statusValue == PressureStatus.left
  78. ? Alignment.centerLeft
  79. : Alignment.centerRight,
  80. child: Container(
  81. width: Get.width * 0.132,
  82. height: Get.width * 0.052,
  83. decoration: ShapeDecoration(
  84. color: widget.buttonColor,
  85. shape: RoundedRectangleBorder(
  86. borderRadius: BorderRadius.circular(Get.width * 0.04),
  87. ),
  88. ),
  89. alignment: Alignment.center,
  90. child: Text(
  91. widget.statusValue == PressureStatus.left
  92. ? widget.values[0]
  93. : widget.values[1],
  94. style: TextStyle(
  95. fontFamily: 'Rubik',
  96. fontSize: Get.width * 0.018,
  97. color: widget.textColor,
  98. fontWeight: FontWeight.bold,
  99. ),
  100. ),
  101. ),
  102. ),
  103. ],
  104. ),
  105. );
  106. }
  107. }