forked from ibhavikmakwana/FlutterPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressButton.dart
More file actions
112 lines (99 loc) · 2.69 KB
/
ProgressButton.dart
File metadata and controls
112 lines (99 loc) · 2.69 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/material.dart';
class ProgressButton extends StatefulWidget {
ProgressButton({Key key, this.title}) : super(key: key);
final String title;
@override
_ProgressButtonState createState() => _ProgressButtonState();
}
class _ProgressButtonState extends State<ProgressButton>
with TickerProviderStateMixin {
int _state = 0;
Animation _animation;
AnimationController _controller;
GlobalKey _globalKey = GlobalKey();
double _width = double.infinity;
@override
void dispose() {
super.dispose();
_controller.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: PhysicalModel(
elevation: 8.0,
shadowColor: Colors.lightGreenAccent,
color: Colors.lightGreen,
borderRadius: BorderRadius.circular(25.0),
child: Container(
key: _globalKey,
height: 48.0,
width: _width,
child: RaisedButton(
padding: EdgeInsets.all(0.0),
child: setUpButtonChild(),
onPressed: () {
setState(() {
if (_state == 0) {
animateButton();
}
});
},
elevation: 4.0,
color: Colors.lightGreen,
),
),
),
),
);
}
///
/// Set up the child widget for the RaisedButton
///
setUpButtonChild() {
if (_state == 0) {
return Text(
"Click Here",
style: const TextStyle(
color: Colors.white,
fontSize: 16.0,
),
);
} else if (_state == 1) {
return CircularProgressIndicator(
value: null,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
);
} else {
return Icon(Icons.check, color: Colors.white);
}
}
void animateButton() {
double initialWidth = _globalKey.currentContext.size.width;
_controller =
AnimationController(duration: Duration(milliseconds: 300), vsync: this);
_animation = Tween(begin: 0.0, end: 1.0).animate(_controller)
..addListener(() {
setState(() {
_width = initialWidth - ((initialWidth - 48.0) * _animation.value);
});
});
_controller.forward();
setState(() {
_state = 1;
});
Timer(Duration(milliseconds: 3300), () {
setState(() {
_state = 2;
});
});
}
}