import 'package:flutter/cupertino.dart'; import 'package:provider/provider.dart'; import '../types/abstracts.dart'; import '../widgets/text_field.dart'; class Authentication extends StatefulWidget { @override _AuthenticationState createState() => _AuthenticationState(); } class _AuthenticationState extends State { final TextEditingController _passwordController = TextEditingController(); final TextEditingController _confirmController = TextEditingController(); bool _invalid = false; bool _passesDontMatch = false; ConfigRepo _config; Future _passwordIsSet; @override didChangeDependencies() { super.didChangeDependencies(); _config = Provider.of(context); _passwordIsSet = _config.passwordSet; } @override Widget build(BuildContext context) { return CupertinoPageScaffold( child: Container( margin: const EdgeInsets.symmetric(horizontal: 50.0), child: FutureBuilder( future: _passwordIsSet, builder: (BuildContext context, AsyncSnapshot snapshot) => snapshot.connectionState == ConnectionState.done ? Column( children: _buildColumnChildren(context, snapshot.data)) : Center(child: CupertinoActivityIndicator()), ), ), ); } List _buildColumnChildren(BuildContext context, bool passwordIsSet) { List children = [ const Spacer(flex: 4), const Flexible(child: Text('Master password:')), Flexible( child: TextField( obscure: true, autofocus: true, controller: _passwordController, ), ), ]; if (!passwordIsSet) { children.add(const Flexible(child: Text('Re-enter password:'))); children.add(Flexible( child: TextField( obscure: true, controller: _confirmController, ), )); } if (_invalid) { children.add(const Flexible( child: Text( 'invalid masterpass', style: TextStyle(color: CupertinoColors.destructiveRed), ), )); } if (_passesDontMatch) { children.add(const Flexible( child: Text( 'passwords don\'t match', style: TextStyle(color: CupertinoColors.destructiveRed), ), )); } children.add(CupertinoButton( child: Text('Enter'), onPressed: _buildSubmitPressedBuilder(context), )); if (_passesDontMatch) { children.add(const Spacer(flex: 1)); } else if (_invalid || !passwordIsSet) { children.add(const Spacer(flex: 2)); } else { children.add(const Spacer(flex: 3)); } return children; } VoidCallback _buildSubmitPressedBuilder(BuildContext context) { return () async { if (await _passwordIsSet) { if (await _config.matchesPasswordHash(_passwordController.text)) { Navigator.of(context).pushReplacementNamed( '/home', arguments: await _config.connectionConfig, ); return; } this.setState(() => _invalid = true); return; } if (_passwordController.text != _confirmController.text) { this.setState(() { _passesDontMatch = true; }); return; } _config.setPassword(_passwordController.text); _passwordIsSet = Future.value(true); Navigator.of(context).pushReplacementNamed('/config'); }; } }