SliderConfirm.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_habit/common/I18N.dart';
  3. class SliderConfirm extends StatefulWidget {
  4. final Function function;
  5. SliderConfirm({required this.function});
  6. @override
  7. _SliderConfirmState createState() => _SliderConfirmState();
  8. }
  9. class _SliderConfirmState extends State<SliderConfirm> {
  10. double? currentValue;
  11. late bool isEnd;
  12. late bool isFormStart;
  13. @override
  14. void initState() {
  15. super.initState();
  16. currentValue = 0;
  17. isEnd = false;
  18. isFormStart = false;
  19. }
  20. @override
  21. Widget build(BuildContext context) {
  22. return Stack(
  23. children: <Widget>[
  24. Center(
  25. child: Container(
  26. decoration: BoxDecoration(
  27. color: Theme.of(context).focusColor,
  28. borderRadius: BorderRadius.circular(100),
  29. ),
  30. child: Slider(
  31. activeColor: Theme.of(context).colorScheme.secondary,
  32. inactiveColor: Colors.transparent,
  33. value: currentValue!,
  34. min: 0,
  35. max: 1,
  36. onChanged: (value) {
  37. if (value <= 0.1) {
  38. currentValue = value;
  39. isFormStart = true;
  40. setState(() {});
  41. }
  42. if (isFormStart) {
  43. currentValue = value;
  44. isEnd = currentValue == 1;
  45. setState(() {});
  46. }
  47. },
  48. onChangeEnd: (value) {
  49. if (isEnd) {
  50. widget.function();
  51. } else {
  52. isFormStart = false;
  53. currentValue = 0;
  54. setState(() {});
  55. }
  56. },
  57. ),
  58. ),
  59. ),
  60. Center(
  61. child: isEnd
  62. ? Text(
  63. I18N.of("松开以继续"),
  64. style: Theme.of(context)
  65. .textTheme
  66. .titleSmall!
  67. .copyWith(color: Theme.of(context).colorScheme.secondary),
  68. )
  69. : Text(
  70. I18N.of("向右滑动"),
  71. style: Theme.of(context).textTheme.titleLarge,
  72. ),
  73. ),
  74. ],
  75. );
  76. }
  77. }