content: import 'package:flutter/cupertino.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter_yyets/app/Api.dart';
  import 'package:flutter_yyets/model/provider/RRUser.dart';
  import 'package:flutter_yyets/ui/widgets/wrapped_material_dialog.dart';
  import 'package:flutter_yyets/utils/toast.dart';
  import 'package:flutter_yyets/utils/tools.dart';
  import 'package:provider/provider.dart';
  
  class LoginPage extends StatefulWidget {
    @override
    State createState() => _LoginPageState();
  }
  
  class _LoginPageState extends State<LoginPage> {
    GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  
    TextEditingController _nameController;
    TextEditingController _passController;
    bool _isShowClear = false;
    bool _isShowPwd = false;
  
    //焦点
    FocusNode _focusNodeName = new FocusNode();
    FocusNode _focusNodePassWord = new FocusNode();
  
    @override
    void initState() {
      super.initState();
  
      _nameController = TextEditingController();
      _nameController.addListener(() {
        bool old = _isShowClear;
        _isShowClear = _nameController.text.isNotEmpty;
        print(_isShowClear);
        if (old != _isShowClear) {
          setState(() {});
        }
      });
      _passController = TextEditingController();
      webUserHint();
    }
  
    void webUserHint() {
      if (PlatformExt.isWeb) {
        Future.delayed(
          Duration(seconds: 1),
          () {
            if (!mounted) return;
            showDialog(
              context: context,
              builder: (c) => WrappedMaterialDialog(
                c,
                title: Text("提示"),
                content: Text("由于浏览器存在跨域问题，此web app使用bird.ioliu.cn作为代理。\n"
                    "您需要了解的是：yyets原生登录接口采用明文传输，若您使用此方式登录造成损失，此App将不负任何责任。感谢理解！"),
                actions: [
                  FlatButton(
                    onPressed: () => Navigator.pop(c),
                    child: Text("我知道了"),
                  ),
                  FlatButton(
                    onPressed: () {
                      Navigator.pop(c);
                      Navigator.pop(context);
                    },
                    child: Text("不登录了"),
                  ),
                ],
              ),
            );
          },
        );
      }
    }
  
    @override
    Widget build(BuildContext context) {
      var user = Provider.of<RRUser>(context);
  
      return Scaffold(
        backgroundColor: Colors.blueAccent,
        body: Center(
          child: Card(
              color: context.isDarkMode ? Colors.black87 : Colors.white,
              elevation: 10,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20),
              ),
              child: Form(
                  key: _formKey,
                  child: Padding(
                    padding: EdgeInsets.all(10),
                    child: Container(
                      width: 270,
                      height: 350,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          TextFormField(
                            focusNode: _focusNodeName,
                            controller: _nameController,
                            decoration: InputDecoration(
                              focusColor: Colors.lightGreen,
                              labelText: "邮箱",
                              labelStyle: TextStyle(color: Colors.blueAccent),
                              prefixIcon: Icon(
                                Icons.person,
                                color: Colors.grey,
                              ),
                              //尾部添加清除按钮
                              suffixIcon: (_isShowClear)
                                  ? IconButton(
                                      icon: Icon(Icons.clear),
                                      onPressed: () {
                                        // 清空输入框内容
                                        _nameController.clear();
                                      },
                                    )
                                  : null,
                            ),
                            validator: (s) {
                              if (s.isEmpty) {
                                return "不可空";
                              }
                              return null;
                            },
                          ),
                          TextFormField(
                            focusNode: _focusNodePassWord,
                            controller: _passController,
                            obscureText: !_isShowPwd,
                            validator: (s) {
                              if (s.isEmpty) {
                                return "不可空";
                              }
                              return null;
                            },
                            decoration: InputDecoration(
                              labelText: "密码",
                              labelStyle: TextStyle(color: Colors.blueAccent),
                              prefixIcon: Icon(Icons.lock, color: Colors.grey,),
                              // 是否显示密码
                              suffixIcon: IconButton(
                                icon: Icon((_isShowPwd)
                                    ? Icons.visibility
                                    : Icons.visibility_off),
                                // 点击改变显示或隐藏密码
                                onPressed: () {
                                  setState(() {
                                    _isShowPwd = !_isShowPwd;
                                  });
                                },
                              ),
                            ),
                          ),
                          Container(
                            height: 20,
                          ),
                          Row(
                            children: [
                              FlatButton(
                                child: Text("忘记密码"),
                                onPressed: () => launchUri(
                                    "http://www.rrys2019.com/user/password/forgot"),
                              ),
                            ],
                          ),
                          Align(
                            alignment: Alignment.centerRight,
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                RaisedButton(
                                  elevation: 10,
                                  child: Text(
                                    "登录",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                  shape: RoundedRectangleBorder(
                                      borderRadius:
                                      BorderRadius.circular(20)),
                                  onPressed: () => _login(user, false),
                                ),
                                Container(width: 10),
                                RaisedButton(
                                    elevation: 10,
                                    child: Text(
                                      "注册",
                                      style: TextStyle(color: Colors.white),
                                    ),
                                    shape: RoundedRectangleBorder(
                                        borderRadius:
                                        BorderRadius.circular(20)),
                                    onPressed: () => _login(user, true)),
                              ],
                            ),
                          ),
                          Container(
                            height: 5,
                          ),
                          Center(
                            child: IconButton(
                              icon: Icon(Icons.close),
                              onPressed: () {
                                Navigator.pop(context);
                              },
                            ),
                          )
                        ],
                      ),
                    ),
                  ))),
        ),
      );
    }
  
    void _login(user, reg) {
      _focusNodeName.unfocus();
      _focusNodePassWord.unfocus();
      if (!_formKey.currentState.validate()) {
        return;
      }
      Future apiCall;
      String pass = _passController.text;
      if (reg) {
        String mail = _nameController.text;
        String name = mail.substring(0, mail.indexOf('@'));
        apiCall = Api.register(mail, name, pass);
      } else {
        apiCall = Api.login(_nameController.text, _passController.text);
      }
      apiCall.then((data) async {
        //uid token
        print(data);
        user.setUidAndToken(data['uid'], data['token']);
        return Api.userInfo();
      }).then((data) {
        return user.save(data);
      }).then((value) {
        toast(reg ? "注册成功" : "登录成功");
        Navigator.pop(context);
      }).catchError((e) {
        print(e);
        toast(e.toString());
      });
    }
  }
  