| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import 'package:dio/dio.dart';
- import 'package:e2ee_chat/common/global.dart';
- import 'package:e2ee_chat/models/theme_model.dart';
- import 'package:e2ee_chat/models/user_model.dart';
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- class LoginRoute extends StatefulWidget {
- @override
- _LoginRouteState createState() => _LoginRouteState();
- }
- class _LoginRouteState extends State<LoginRoute> {
- TextEditingController _unameController = new TextEditingController();
- TextEditingController _pwdController = new TextEditingController();
- bool pwdShow = false; //密码是否显示明文
- GlobalKey _formKey = new GlobalKey<FormState>();
- bool _nameAutoFocus = true;
- @override
- void initState() {
- // 自动填充上次登录的用户名,填充后将焦点定位到密码输入框
- if (Global.profile.username != null) {
- _unameController.text = Global.profile.username!;
- _nameAutoFocus = true;
- }
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- var gm = GmLocalizations.of(context);
- return Scaffold(
- appBar: AppBar(title: Text(gm.login)),
- body: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Form(
- key: _formKey,
- autovalidateMode: AutovalidateMode.always, // TODO: what is AutovalidateMode?
- child: Column(
- children: <Widget>[
- TextFormField(
- autofocus: _nameAutoFocus,
- controller: _unameController,
- decoration: InputDecoration(
- labelText: gm.userName,
- hintText: gm.userNameOrEmail,
- prefixIcon: Icon(Icons.person),
- ),
- // 校验用户名(不能为空)
- validator: (v) {
- return v!.trim().isNotEmpty ? null : gm.userNameRequired;
- }),
- TextFormField(
- controller: _pwdController,
- autofocus: !_nameAutoFocus,
- decoration: InputDecoration(
- labelText: gm.password,
- hintText: gm.password,
- prefixIcon: Icon(Icons.lock),
- suffixIcon: IconButton(
- icon: Icon(
- pwdShow ? Icons.visibility_off : Icons.visibility),
- onPressed: () {
- setState(() {
- pwdShow = !pwdShow;
- });
- },
- )),
- obscureText: !pwdShow,
- //校验密码(不能为空)
- validator: (v) {
- return v!.trim().isNotEmpty ? null : gm.passwordRequired;
- },
- ),
- Padding(
- padding: const EdgeInsets.only(top: 25),
- child: ConstrainedBox(
- constraints: BoxConstraints.expand(height: 55.0),
- child: ElevatedButton(
- style: ButtonStyle(
- foregroundColor: MaterialStateProperty.all<Color>(Provider.of<ThemeModel>(context).theme),
- ),
- onPressed: _onLogin,
- child: Text(gm.login),
- ),
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- void _onLogin() async {
- // 提交前,先验证各个表单字段是否合法
- if ((_formKey.currentState as FormState).validate()) {
- Fluttertoast.showToast(msg: GmLocalizations.of(context).loading);
- try {
- // user = await Git(context).login(_unameController.text, _pwdController.text);
- // 因为登录页返回后,首页会build,所以我们传false,更新user后不触发更新
- Provider.of<UserModel>(context).login(username: _unameController.text, password: _pwdController.text);
- } on DioError catch(e) {
- //登录失败则提示
- if (e.response?.statusCode == 401) {
- Fluttertoast.showToast(msg: GmLocalizations.of(context).userNameOrPasswordWrong);
- } else {
- Fluttertoast.showToast(msg: e.toString());
- }
- }
- if (Global.profile.isLogin) {
- // 返回
- Navigator.of(context).pop();
- }
- }
- }
- }
|