Skip to content

Commit eb88d11

Browse files
committed
Merge branch 'dev'
2 parents 20490ae + dbd2ae5 commit eb88d11

11 files changed

+473
-207
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# modal_progress_hud
22

3-
A simple widget wrapper to enable modal progress HUD (a modal progress indicator)
3+
A simple widget wrapper to enable modal progress HUD (a modal progress indicator, HUD = Heads Up Display)
44

55
[![pub package](https://img.shields.io/pub/v/modal_progress_hud.svg)](https://pub.dartlang.org/packages/modal_progress_hud)
66
[![Build Status](https://travis-ci.org/mmcc007/modal_progress_hud.svg?branch=master)](https://travis-ci.org/mmcc007/modal_progress_hud)

example/lib/main.dart

+52-54
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,46 @@ class MyApp extends StatelessWidget {
1313
primarySwatch: Colors.blue,
1414
),
1515
home: LoginPage(
16-
inputLoginData: LoginData(),
16+
onSignIn: () => print('login successful!'),
1717
),
1818
);
1919
}
2020
}
2121

2222
class LoginPage extends StatefulWidget {
23-
final LoginData inputLoginData;
24-
LoginPage({@required this.inputLoginData});
23+
final VoidCallback _onSignIn;
24+
25+
LoginPage({@required onSignIn})
26+
: assert(onSignIn != null),
27+
_onSignIn = onSignIn;
28+
2529
@override
26-
_LoginPageState createState() =>
27-
_LoginPageState(inputLoginData: inputLoginData);
30+
_LoginPageState createState() => _LoginPageState();
2831
}
2932

3033
class _LoginPageState extends State<LoginPage> {
31-
final LoginData inputLoginData;
32-
_LoginPageState({this.inputLoginData});
33-
34+
// maintains validators and state of form fields
3435
final GlobalKey<FormState> _loginFormKey = GlobalKey<FormState>();
36+
3537
// manage state of modal progress HUD widget
36-
bool _inAsyncCall = false;
38+
bool _isInAsyncCall = false;
3739

38-
final LoginData _serverLoginData = LoginData(
39-
userName: 'username1',
40-
password: 'password1',
41-
);
42-
bool _isValidUserName = true; // managed by response from server
43-
bool _isValidPassword = true; // managed by response from server
40+
bool _isInvalidAsyncUser = false; // managed after response from server
41+
bool _isInvalidAsyncPass = false; // managed after response from server
42+
43+
String _username;
44+
String _password;
45+
bool _isLoggedIn = false;
4446

4547
// validate user name
4648
String _validateUserName(String userName) {
4749
if (userName.length < 8) {
4850
return 'Username must be at least 8 characters';
4951
}
5052

51-
if (!_isValidUserName) {
52-
_isValidUserName = true;
53+
if (_isInvalidAsyncUser) {
54+
// disable message until after next async call
55+
_isInvalidAsyncUser = false;
5356
return 'Incorrect user name';
5457
}
5558

@@ -62,8 +65,9 @@ class _LoginPageState extends State<LoginPage> {
6265
return 'Password must be at least 8 characters';
6366
}
6467

65-
if (!_isValidPassword) {
66-
_isValidPassword = true;
68+
if (_isInvalidAsyncPass) {
69+
// disable message until after next async call
70+
_isInvalidAsyncPass = false;
6771
return 'Incorrect password';
6872
}
6973

@@ -74,35 +78,40 @@ class _LoginPageState extends State<LoginPage> {
7478
if (_loginFormKey.currentState.validate()) {
7579
_loginFormKey.currentState.save();
7680

77-
// dismiss keyboard
81+
// dismiss keyboard during async call
7882
FocusScope.of(context).requestFocus(new FocusNode());
7983

8084
// start the modal progress HUD
8185
setState(() {
82-
_inAsyncCall = true;
86+
_isInAsyncCall = true;
8387
});
8488

8589
// Simulate a service call
8690
Future.delayed(Duration(seconds: 1), () {
91+
final _accountUsername = 'username1';
92+
final _accountPassword = 'password1';
8793
setState(() {
88-
if (inputLoginData.userName == _serverLoginData.userName) {
89-
_isValidUserName = true;
90-
// only validate password if username exists in database
91-
if (inputLoginData.password == _serverLoginData.password)
92-
_isValidPassword = true;
93-
else
94-
_isValidPassword = false;
94+
if (_username == _accountUsername) {
95+
_isInvalidAsyncUser = false;
96+
if (_password == _accountPassword) {
97+
// username and password are correct
98+
_isInvalidAsyncPass = false;
99+
_isLoggedIn = true;
100+
} else
101+
// username is correct, but password is incorrect
102+
_isInvalidAsyncPass = true;
95103
} else {
96-
_isValidUserName = false;
97-
// no such user, so password validator not triggered
98-
_isValidPassword = true;
99-
}
100-
if (_isValidUserName && _isValidPassword) {
101-
inputLoginData.isLoggedIn = true;
104+
// incorrect username and have not checked password result
105+
_isInvalidAsyncUser = true;
106+
// no such user, so no need to trigger async password validator
107+
_isInvalidAsyncPass = false;
102108
}
103109
// stop the modal progress HUD
104-
_inAsyncCall = false;
110+
_isInAsyncCall = false;
105111
});
112+
if (_isLoggedIn)
113+
// do something
114+
widget._onSignIn();
106115
});
107116
}
108117
}
@@ -114,14 +123,16 @@ class _LoginPageState extends State<LoginPage> {
114123
title: Text('Modal Progress HUD Demo'),
115124
backgroundColor: Colors.blue,
116125
),
126+
// display modal progress HUD (heads-up display, or indicator)
127+
// when in async call
117128
body: ModalProgressHUD(
118129
child: SingleChildScrollView(
119130
child: Container(
120131
padding: const EdgeInsets.all(16.0),
121132
child: buildLoginForm(context),
122133
),
123134
),
124-
inAsyncCall: _inAsyncCall,
135+
inAsyncCall: _isInAsyncCall,
125136
// demo of some additional parameters
126137
opacity: 0.5,
127138
progressIndicator: CircularProgressIndicator(),
@@ -130,9 +141,8 @@ class _LoginPageState extends State<LoginPage> {
130141
}
131142

132143
Widget buildLoginForm(BuildContext context) {
133-
final ThemeData themeData = Theme.of(context);
134-
final TextTheme textTheme = themeData.textTheme;
135-
// run the validators on reload
144+
final TextTheme textTheme = Theme.of(context).textTheme;
145+
// run the validators on reload to process async results
136146
_loginFormKey.currentState?.validate();
137147
return Form(
138148
key: this._loginFormKey,
@@ -146,9 +156,7 @@ class _LoginPageState extends State<LoginPage> {
146156
hintText: 'enter username', labelText: 'User Name'),
147157
style: TextStyle(fontSize: 20.0, color: textTheme.button.color),
148158
validator: _validateUserName,
149-
onSaved: (String value) {
150-
inputLoginData.userName = value;
151-
},
159+
onSaved: (value) => _username = value,
152160
),
153161
),
154162
Padding(
@@ -160,22 +168,19 @@ class _LoginPageState extends State<LoginPage> {
160168
hintText: 'enter password', labelText: 'Password'),
161169
style: TextStyle(fontSize: 20.0, color: textTheme.button.color),
162170
validator: _validatePassword,
163-
onSaved: (String value) {
164-
inputLoginData.password = value;
165-
},
171+
onSaved: (value) => _password = value,
166172
),
167173
),
168174
Padding(
169175
padding: const EdgeInsets.all(32.0),
170176
child: RaisedButton(
171-
key: Key('login'),
172177
onPressed: _submit,
173178
child: Text('Login'),
174179
),
175180
),
176181
Padding(
177182
padding: const EdgeInsets.all(8.0),
178-
child: inputLoginData.isLoggedIn
183+
child: _isLoggedIn
179184
? Text(
180185
'Login successful!',
181186
key: Key('loggedIn'),
@@ -192,10 +197,3 @@ class _LoginPageState extends State<LoginPage> {
192197
);
193198
}
194199
}
195-
196-
class LoginData {
197-
String userName;
198-
String password;
199-
bool isLoggedIn;
200-
LoginData({this.userName, this.password, this.isLoggedIn = false});
201-
}

example/pubspec.lock

+10-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ packages:
4242
name: collection
4343
url: "https://pub.dartlang.org"
4444
source: hosted
45-
version: "1.14.11"
45+
version: "1.14.6"
4646
convert:
4747
dependency: transitive
4848
description:
@@ -63,7 +63,7 @@ packages:
6363
name: csslib
6464
url: "https://pub.dartlang.org"
6565
source: hosted
66-
version: "0.14.5"
66+
version: "0.14.4+1"
6767
cupertino_icons:
6868
dependency: "direct main"
6969
description:
@@ -77,7 +77,7 @@ packages:
7777
name: file
7878
url: "https://pub.dartlang.org"
7979
source: hosted
80-
version: "5.0.4"
80+
version: "5.0.1"
8181
flutter:
8282
dependency: "direct main"
8383
description: flutter
@@ -113,7 +113,7 @@ packages:
113113
name: html
114114
url: "https://pub.dartlang.org"
115115
source: hosted
116-
version: "0.13.3+3"
116+
version: "0.13.3+2"
117117
http:
118118
dependency: transitive
119119
description:
@@ -183,14 +183,14 @@ packages:
183183
name: matcher
184184
url: "https://pub.dartlang.org"
185185
source: hosted
186-
version: "0.12.3+1"
186+
version: "0.12.2+1"
187187
meta:
188188
dependency: transitive
189189
description:
190190
name: meta
191191
url: "https://pub.dartlang.org"
192192
source: hosted
193-
version: "1.1.6"
193+
version: "1.1.5"
194194
mime:
195195
dependency: transitive
196196
description:
@@ -342,7 +342,7 @@ packages:
342342
name: string_scanner
343343
url: "https://pub.dartlang.org"
344344
source: hosted
345-
version: "1.0.4"
345+
version: "1.0.3"
346346
term_glyph:
347347
dependency: transitive
348348
description:
@@ -356,14 +356,14 @@ packages:
356356
name: test
357357
url: "https://pub.dartlang.org"
358358
source: hosted
359-
version: "1.3.0"
359+
version: "0.12.41"
360360
typed_data:
361361
dependency: transitive
362362
description:
363363
name: typed_data
364364
url: "https://pub.dartlang.org"
365365
source: hosted
366-
version: "1.1.6"
366+
version: "1.1.5"
367367
utf:
368368
dependency: transitive
369369
description:
@@ -407,4 +407,4 @@ packages:
407407
source: hosted
408408
version: "2.1.15"
409409
sdks:
410-
dart: ">=2.0.0-dev.68.0 <3.0.0"
410+
dart: ">=2.0.0-dev.62.0 <=2.0.0-dev.69.5.flutter-eab492385c"

0 commit comments

Comments
 (0)