forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimated_loading_button.dart
More file actions
75 lines (69 loc) · 2.14 KB
/
Copy pathanimated_loading_button.dart
File metadata and controls
75 lines (69 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import 'package:flutter/material.dart';
class AnimatedLoadingButton extends StatefulWidget {
final String text;
final Future<void> Function() onPressed;
final double width;
final double height;
final Color color;
final Color loaderColor;
final TextStyle textStyle;
final Duration animationDuration;
const AnimatedLoadingButton({
super.key,
required this.text,
required this.onPressed,
this.width = 200,
this.height = 40,
required this.color,
this.loaderColor = Colors.white,
this.textStyle = const TextStyle(fontSize: 16, color: Colors.white),
this.animationDuration = const Duration(milliseconds: 300),
});
@override
State<AnimatedLoadingButton> createState() => _AnimatedLoadingButtonState();
}
class _AnimatedLoadingButtonState extends State<AnimatedLoadingButton> {
bool _isLoading = false;
void _handleOnPressed() async {
if (mounted) {
setState(() {
_isLoading = true;
});
}
await widget.onPressed();
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return AnimatedContainer(
duration: widget.animationDuration,
width: _isLoading ? widget.height : widget.width,
height: widget.height,
decoration: BoxDecoration(color: widget.color, borderRadius: BorderRadius.circular(widget.height / 2)),
child: InkWell(
onTap: _isLoading ? null : _handleOnPressed,
borderRadius: BorderRadius.circular(widget.height / 2),
child: Center(
child: AnimatedSwitcher(
duration: widget.animationDuration,
child: _isLoading
? SizedBox(
width: widget.height / 2,
height: widget.height / 2,
child: CircularProgressIndicator(
key: const ValueKey('loader'),
color: widget.loaderColor,
strokeWidth: 3.0,
),
)
: Text(widget.text, key: const ValueKey('buttonText'), style: widget.textStyle),
),
),
),
);
}
}