Guest User

Untitled

a guest
Aug 7th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:modal_progress_hud/modal_progress_hud.dart';
  5.  
  6. void main() => runApp(MyApp());
  7.  
  8. class MyApp extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: FetchUserName(loginData: LoginData()),
  16. );
  17. }
  18. }
  19.  
  20. class FetchUserName extends StatefulWidget {
  21. final LoginData loginData;
  22. FetchUserName({@required this.loginData});
  23. @override
  24. _FetchUserNameState createState() => _FetchUserNameState();
  25. }
  26.  
  27. class _FetchUserNameState extends State<FetchUserName> {
  28. Future<String> fetchUserName() {
  29. return Future.delayed(Duration(seconds: 1), () => 'username1');
  30. }
  31.  
  32. @override
  33. initState() {
  34. super.initState();
  35. fetchUserName().then((username) {
  36. print('username = $username');
  37. setState(() {
  38. widget.loginData.userName = username;
  39. });
  40. });
  41. }
  42.  
  43. @override
  44. Widget build(BuildContext context) {
  45. return widget.loginData.userName == null
  46. ? Scaffold(
  47. appBar: AppBar(
  48. title: Text('Modal Progress HUD Demo'),
  49. backgroundColor: Colors.blue,
  50. ),
  51. body: ModalProgressHUD(
  52. inAsyncCall: true,
  53. child: Container(),
  54. ))
  55. : LoginPage(loginData: widget.loginData);
  56. }
  57. }
  58.  
  59. class LoginPage extends StatefulWidget {
  60. final LoginData loginData;
  61. LoginPage({@required this.loginData});
  62. @override
  63. _LoginPageState createState() => _LoginPageState(loginData: loginData);
  64. }
  65.  
  66. class _LoginPageState extends State<LoginPage> {
  67. final LoginData loginData;
  68. _LoginPageState({this.loginData});
  69.  
  70. final GlobalKey<FormState> _loginFormKey = GlobalKey<FormState>();
  71. // manage state of modal progress HUD widget
  72. bool _inAsyncCall = false;
  73.  
  74. final LoginData _validLoginData = LoginData(
  75. userName: 'username1',
  76. password: 'password1',
  77. );
  78. bool _isValidUserName = true; // managed by response from server
  79. bool _isValidPassword = true; // managed by response from server
  80.  
  81. // validate user name
  82. String _validateUserName(String userName) {
  83. if (userName.length < 8) {
  84. return 'Username must be at least 8 characters';
  85. }
  86.  
  87. if (!_isValidUserName) {
  88. _isValidUserName = true;
  89. return 'Incorrect user name';
  90. }
  91.  
  92. return null;
  93. }
  94.  
  95. // validate password
  96. String _validatePassword(String password) {
  97. if (password.length < 8) {
  98. return 'Password must be at least 8 characters';
  99. }
  100.  
  101. if (!_isValidPassword) {
  102. _isValidPassword = true;
  103. return 'Incorrect password';
  104. }
  105.  
  106. return null;
  107. }
  108.  
  109. void _submit() {
  110. if (_loginFormKey.currentState.validate()) {
  111. _loginFormKey.currentState.save();
  112.  
  113. // dismiss keyboard
  114. FocusScope.of(context).requestFocus(new FocusNode());
  115.  
  116. // start the modal progress HUD
  117. setState(() {
  118. _inAsyncCall = true;
  119. });
  120.  
  121. // Simulate a service call
  122. Future.delayed(Duration(seconds: 1), () {
  123. setState(() {
  124. if (loginData.userName == _validLoginData.userName)
  125. _isValidUserName = true;
  126. else
  127. _isValidUserName = false;
  128. if (loginData.password == _validLoginData.password)
  129. _isValidPassword = true;
  130. else
  131. _isValidPassword = false;
  132. if (_isValidUserName && _isValidPassword) {
  133. loginData.loggedIn = true;
  134. }
  135. // stop the modal progress HUD
  136. _inAsyncCall = false;
  137. });
  138. });
  139. }
  140. }
  141.  
  142. @override
  143. Widget build(BuildContext context) {
  144. return Scaffold(
  145. appBar: AppBar(
  146. title: Text('Modal Progress HUD Demo'),
  147. backgroundColor: Colors.blue,
  148. ),
  149. body: ModalProgressHUD(
  150. child: LoginForm(
  151. loginFormKey: _loginFormKey,
  152. submit: _submit,
  153. loginData: loginData,
  154. validateUserName: _validateUserName,
  155. validatePassword: _validatePassword,
  156. ),
  157. inAsyncCall: _inAsyncCall,
  158. // demo of some additional parameters
  159. opacity: 0.5,
  160. progressIndicator: CircularProgressIndicator(),
  161. ),
  162. );
  163. }
  164. }
  165.  
  166. class LoginForm extends StatelessWidget {
  167. final GlobalKey<FormState> loginFormKey;
  168. final LoginData loginData;
  169. final Function validateUserName;
  170. final Function validatePassword;
  171. final Function submit;
  172. LoginForm({
  173. @required this.loginFormKey,
  174. @required this.submit,
  175. @required this.loginData,
  176. @required this.validateUserName,
  177. @required this.validatePassword,
  178. });
  179.  
  180. @override
  181. Widget build(BuildContext context) {
  182. final ThemeData themeData = Theme.of(context);
  183. final TextTheme textTheme = themeData.textTheme;
  184. print('loginData = $loginData');
  185. // run the validators on reload
  186. loginFormKey.currentState?.validate();
  187. return Form(
  188. key: this.loginFormKey,
  189. child: Column(
  190. children: [
  191. Padding(
  192. padding: const EdgeInsets.fromLTRB(32.0, 4.0, 32.0, 4.0),
  193. child: TextFormField(
  194. key: Key('username'),
  195. initialValue: loginData.userName,
  196. keyboardType: TextInputType.text,
  197. decoration: InputDecoration(
  198. hintText: 'enter username', labelText: 'User Name'),
  199. style: TextStyle(fontSize: 20.0, color: textTheme.button.color),
  200. validator: validateUserName,
  201. onSaved: (String value) {
  202. loginData.userName = value;
  203. },
  204. ),
  205. ),
  206. Padding(
  207. padding: const EdgeInsets.fromLTRB(32.0, 4.0, 32.0, 32.0),
  208. child: TextFormField(
  209. key: Key('password'),
  210. obscureText: true,
  211. initialValue: loginData.password,
  212. keyboardType: TextInputType.text,
  213. decoration: InputDecoration(
  214. hintText: 'enter password', labelText: 'Password'),
  215. style: TextStyle(fontSize: 20.0, color: textTheme.button.color),
  216. validator: validatePassword,
  217. onSaved: (String value) {
  218. loginData.password = value;
  219. },
  220. ),
  221. ),
  222. RaisedButton(
  223. key: Key('login'),
  224. onPressed: submit,
  225. child: Text('Login'),
  226. ),
  227. Padding(
  228. padding: const EdgeInsets.fromLTRB(32.0, 32.0, 32.0, 4.0),
  229. child: loginData.loggedIn
  230. ? Text(
  231. 'Login successful!',
  232. key: Key('loggedIn'),
  233. style: TextStyle(fontSize: 20.0),
  234. )
  235. : Text(
  236. 'Not logged in',
  237. key: Key('notLoggedIn'),
  238. style: TextStyle(fontSize: 20.0),
  239. ),
  240. ),
  241. ],
  242. ),
  243. );
  244. }
  245. }
  246.  
  247. class LoginData {
  248. String userName;
  249. String password;
  250. bool loggedIn;
  251. LoginData({this.userName, this.password, this.loggedIn = false});
  252.  
  253. @override
  254. String toString() {
  255. return 'LoginData{\n'
  256. '\tuserName = $userName\n'
  257. '\tpassword = $password\n'
  258. '\tloggedIn = $loggedIn\n'
  259. '}';
  260. }
  261. }
Add Comment
Please, Sign In to add comment