| 1 | import 'package:cake_wallet/src/widgets/primary_button.dart'; |
| 2 | import 'package:cake_wallet/themes/core/theme_extension.dart'; |
| 3 | import 'package:flutter/material.dart'; |
| 4 | |
| 5 | class StandardSlideButton extends StatefulWidget { |
| 6 | const StandardSlideButton({ |
| 7 | Key? key, |
| 8 | required this.onSlideComplete, |
| 9 | this.buttonText = '', |
| 10 | this.height = 48.0, |
| 11 | required this.accessibleNavigationModeButtonText, |
| 12 | this.tileBackgroundColor, |
| 13 | this.knobColor, |
| 14 | this.isDisabled = false, |
| 15 | }) : super(key: key); |
| 16 | |
| 17 | final VoidCallback onSlideComplete; |
| 18 | final String buttonText; |
| 19 | final double height; |
| 20 | final String accessibleNavigationModeButtonText; |
| 21 | final Color? tileBackgroundColor; |
| 22 | final Color? knobColor; |
| 23 | final bool isDisabled; |
| 24 | |
| 25 | @override |
| 26 | StandardSlideButtonState createState() => StandardSlideButtonState(); |
| 27 | } |
| 28 | |
| 29 | class StandardSlideButtonState extends State<StandardSlideButton> { |
| 30 | double _dragPosition = 0.0; |
| 31 | double get dragPosition => _dragPosition; |
| 32 | |
| 33 | double sideMargin = 4.0; |
| 34 | double effectiveMaxWidth = 0.0; |
| 35 | double sliderWidth = 42.0; |
| 36 | |
| 37 | @override |
| 38 | Widget build(BuildContext context) { |
| 39 | final bool accessible = MediaQuery.of(context).accessibleNavigation; |
| 40 | |
| 41 | final tileBackgroundColor = widget.isDisabled |
| 42 | ? context.currentTheme.customColors.backgroundGradientColor.withOpacity(0.5) |
| 43 | : context.currentTheme.customColors.backgroundGradientColor; |
| 44 | |
| 45 | return accessible |
| 46 | ? PrimaryButton( |
| 47 | text: widget.accessibleNavigationModeButtonText, |
| 48 | color: Theme.of(context).colorScheme.primary, |
| 49 | textColor: Theme.of(context).colorScheme.onPrimary, |
| 50 | onPressed: widget.isDisabled ? null : () => widget.onSlideComplete(), |
| 51 | ) |
| 52 | : LayoutBuilder( |
| 53 | builder: (context, constraints) { |
| 54 | final double maxWidth = constraints.maxWidth; |
| 55 | const double sideMargin = 4.0; |
| 56 | final double effectiveMaxWidth = maxWidth - 2 * sideMargin; |
| 57 | const double sliderWidth = 42.0; |
| 58 | |
| 59 | return Container( |
| 60 | height: widget.height, |
| 61 | decoration: BoxDecoration( |
| 62 | borderRadius: BorderRadius.circular(10), |
| 63 | color: (widget.isDisabled |
| 64 | ? widget.tileBackgroundColor?.withOpacity(0.5) |
| 65 | : widget.tileBackgroundColor) ?? |
| 66 | tileBackgroundColor, |
| 67 | ), |
| 68 | child: Stack( |
| 69 | alignment: Alignment.centerLeft, |
| 70 | children: [ |
| 71 | Center( |
| 72 | child: Text( |
| 73 | widget.buttonText, |
| 74 | style: Theme.of(context).textTheme.bodyMedium!.copyWith( |
| 75 | fontSize: 16, |
| 76 | fontWeight: FontWeight.w600, |
| 77 | ), |
| 78 | ), |
| 79 | ), |
| 80 | Positioned( |
| 81 | left: sideMargin + _dragPosition, |
| 82 | child: GestureDetector( |
| 83 | key: ValueKey('standard_slide_button_widget_slider_key'), |
| 84 | onHorizontalDragUpdate: widget.isDisabled |
| 85 | ? null |
| 86 | : (details) { |
| 87 | setState(() { |
| 88 | _dragPosition += details.delta.dx; |
| 89 | if (_dragPosition < 0) _dragPosition = 0; |
| 90 | if (_dragPosition > effectiveMaxWidth - sliderWidth) { |
| 91 | _dragPosition = effectiveMaxWidth - sliderWidth; |
| 92 | } |
| 93 | }); |
| 94 | }, |
| 95 | onHorizontalDragEnd: widget.isDisabled |
| 96 | ? null |
| 97 | : (details) { |
| 98 | if (_dragPosition >= effectiveMaxWidth - sliderWidth - 10) { |
| 99 | widget.onSlideComplete(); |
| 100 | } else { |
| 101 | setState(() => _dragPosition = 0); |
| 102 | } |
| 103 | }, |
| 104 | child: Container( |
| 105 | key: ValueKey('standard_slide_button_widget_slider_container_key'), |
| 106 | width: sliderWidth, |
| 107 | height: widget.height - 8, |
| 108 | decoration: BoxDecoration( |
| 109 | borderRadius: BorderRadius.circular(10), |
| 110 | color: widget.knobColor ?? Theme.of(context).colorScheme.surface, |
| 111 | ), |
| 112 | alignment: Alignment.center, |
| 113 | child: Icon( |
| 114 | key: ValueKey('standard_slide_button_widget_slider_icon_key'), |
| 115 | Icons.arrow_forward, |
| 116 | color: widget.isDisabled |
| 117 | ? Theme.of(context).colorScheme.onSurface.withOpacity(0.2) |
| 118 | : Theme.of(context).colorScheme.onSurface, |
| 119 | ), |
| 120 | ), |
| 121 | ), |
| 122 | ) |
| 123 | ], |
| 124 | ), |
| 125 | ); |
| 126 | }, |
| 127 | ); |
| 128 | } |
| 129 | } |