diff --git a/lib/screens/auth/login_page.dart b/lib/screens/auth/login_page.dart index c760d16..109d3ef 100644 --- a/lib/screens/auth/login_page.dart +++ b/lib/screens/auth/login_page.dart @@ -11,20 +11,39 @@ class AuthorizationPage extends StatefulWidget { class _AuthorizationPage extends State { final TextEditingController _loginController = new TextEditingController(); final TextEditingController _passwordController = new TextEditingController(); + final _formKey = GlobalKey(); final borederRadius = BorderRadius.all(Radius.circular(10)); bool passwordVisible = false; void _login(){ - final String login = _loginController.text; - final String password = _passwordController.text; + if (_formKey.currentState!.validate()) { + final String login = _loginController.text; + final String password = _passwordController.text; - if (login == 'admin' && password == '123456') { - Navigator.of(context).push(MaterialPageRoute( - builder: (ctx) => HomePage()) - ); + if (login == 'admin' && password == '123456') { + Navigator.of(context).push(MaterialPageRoute( + builder: (ctx) => HomePage()) + ); + } } } + String? _validatePassword(String? password){ + if (password == null || password.isEmpty) + return 'Введите пароль'; + if (password != '123456') + return 'Пароль: 123456'; + return null; + } + + String? _validateLogin(String? login){ + if (login == null || login.isEmpty) + return 'Введите логин'; + if (login != 'admin') + return 'Логин: admin'; + return null; + } + @override void dispose() { super.dispose(); @@ -61,20 +80,22 @@ class _AuthorizationPage extends State { } Widget _buildFormWidget(BuildContext context) { - return Column( + return Form( + key: _formKey, + child: Column( children: [ - TextField( + TextFormField( controller: _loginController, decoration: InputDecoration( prefixIcon: Icon(Icons.account_circle), border: OutlineInputBorder( borderRadius: borederRadius ), - labelText: 'Логин' - ) - ), + labelText: 'Логин'), + validator: (text) => _validateLogin(text), + ), SizedBox(height: 25,), - TextField( + TextFormField( controller: _passwordController, obscureText: !passwordVisible, decoration: InputDecoration( @@ -85,17 +106,13 @@ class _AuthorizationPage extends State { labelText: 'Пароль', suffixIcon: IconButton( icon: Icon( - passwordVisible ? Icons.visibility : Icons.visibility_off - ), - onPressed: () { - setState(() { - passwordVisible = !passwordVisible; - }); - } - ) - ) + passwordVisible ? Icons.visibility : Icons.visibility_off), + onPressed: () => setState(() => passwordVisible = !passwordVisible), + )), + validator: (text) => _validatePassword(text), ), ], - ); + ) + ); } } \ No newline at end of file