account_login_page.dart 12.9 KB
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:fluwx/fluwx.dart';
import 'package:one_key/app.dart';
import 'package:one_key/models/checkbind_phone_model.dart';
import 'package:one_key/models/store_info.dart';
import 'package:one_key/net/dio_util.dart';
import 'package:one_key/provider/create_store_model.dart';
import 'package:one_key/provider/other_login_provider.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/route/navigate_service.dart';
import 'package:one_key/route/navigator_util.dart';
import 'package:one_key/utils/dialog_util.dart';
import 'package:one_key/utils/utils.dart';
import 'package:one_key/widgets/MyDivider.dart';
import 'package:one_key/widgets/h_empty_view.dart';
import 'package:one_key/widgets/loading.dart';
import 'package:one_key/widgets/round_button.dart';
import 'package:one_key/widgets/system_app_bar.dart';
import 'package:one_key/widgets/v_empty_view.dart';
import 'package:provider/provider.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';

class AccountLoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new AccountLoginPageState();
  }
}

class AccountLoginPageState extends State<AccountLoginPage>
    with WidgetsBindingObserver {
  TextEditingController _sellerCodeController = TextEditingController();
  TextEditingController _accountController = TextEditingController();
  final TextEditingController _pwdController = TextEditingController();
  final ScrollController _scrollController = new ScrollController();

  final FocusNode _sellerCodeFocusNode = new FocusNode();
  final FocusNode _accountFocusNode = new FocusNode();
  final FocusNode _pwdFocusNode = new FocusNode();

  bool isActivate = false; //是否激活登录

  StreamSubscription<BaseWeChatResponse> streamSubscription;
  OtherLoginProvider otherLoginProvider;
  CreateStoreModel createStoreModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    WidgetsBinding.instance.addObserver(this);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final UserModel userModel =
          Provider.of<UserModel>(context, listen: false);
      if (userModel != null && userModel.user != null) {
        Future.delayed(Duration(milliseconds: 50), () {
          _sellerCodeController.text = userModel.user.sellercode;
          _accountController.text = userModel.user.account;
          // FocusScope.of(context).requestFocus(_pwdFocusNode);
        });
      }
    });
  }

  //当页面离开时要清除监听器
  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
    if (streamSubscription != null) {
      streamSubscription.cancel();
    }
  }

  //监听输入框,设置登录按钮状态
  setActivate() {
    if (_sellerCodeController.text.isNotEmpty &&
        _accountController.text.isNotEmpty &&
        _pwdController.text.isNotEmpty) {
      setState(() {
        isActivate = true;
      });
    } else {
      setState(() {
        isActivate = false;
      });
    }
  }

  _showLoading(BuildContext context) {
    showGeneralDialog(
        context: context,
        barrierDismissible: false,
        barrierLabel:
            MaterialLocalizations.of(context).modalBarrierDismissLabel,
        transitionDuration: const Duration(milliseconds: 150),
        pageBuilder: (BuildContext context, Animation animation,
            Animation secondaryAnimation) {
          return Align(
            child: ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Container(
                width: 80,
                height: 80,
                child: CupertinoActivityIndicator(),
              ),
            ),
          );
        }).then((v) {});
  }

  @override
  Widget build(BuildContext context) {
    otherLoginProvider =
        Provider.of<OtherLoginProvider>(context, listen: false);
    createStoreModel = Provider.of<CreateStoreModel>(context, listen: false);

    final double prefixIconWidth = ScreenUtil().setWidth(24);

    return Scaffold(
        backgroundColor: MyColours.color_ffffff,
        appBar: SystemAppBar(
          '',
          onTap: () {
            Navigator.pop(context);
          },
        ),
        body: Container(
          width: double.infinity,
          height: double.infinity,
          padding: EdgeInsets.symmetric(horizontal: ScreenUtil().setWidth(40)),
          child: SingleChildScrollView(
            controller: _scrollController,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                VEmptyView(30),
                Row(
                  children: [
                    // Image.asset(
                    //   "assets/logo.png",
                    //   width: ScreenUtil().setWidth(20 * 2),
                    //   height: ScreenUtil().setWidth(20 * 2),
                    // ),
                    // HEmptyView(16),
                    Text(
                      '账号密码登录',
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: ScreenUtil().setSp(32)),
                    )
                  ],
                ),
                VEmptyView(60),
                Text(
                  '输入商户编号',
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: ScreenUtil().setSp(26)),
                ),
                Container(
                    height: ScreenUtil().setWidth(84),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              color: MyColours.color_efefef,
                              width: ScreenUtil().setWidth(2))),
                    ),
                    child: TextField(
                      controller: _sellerCodeController,
                      focusNode: _sellerCodeFocusNode,
                      // autofocus: _sellerCodeController.text.isEmpty,
                      style: TextStyle(
                        color: MyColours.color_353535,
                        fontSize: ScreenUtil().setSp(28),
                      ),
                      // textAlignVertical: TextAlignVertical.center,
                      decoration: InputDecoration(
                        hintText: "例:001698",
                        hintStyle: TextStyle(
                          color: MyColours.color_c1c1c1,
                          fontSize: ScreenUtil().setSp(28),
                        ),
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.only(bottom: -5),
                      ),
                      onChanged: (_) {
                        setActivate();
                      },
                      keyboardType:
                          TextInputType.numberWithOptions(decimal: true),
                    )),
                VEmptyView(40),
                Text(
                  '输入员工号',
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: ScreenUtil().setSp(26)),
                ),
                Container(
                    height: ScreenUtil().setWidth(84),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              color: MyColours.color_efefef,
                              width: ScreenUtil().setWidth(2))),
                    ),
                    child: TextField(
                      controller: _accountController,
                      focusNode: _accountFocusNode,
                      style: TextStyle(
                          color: MyColours.color_353535,
                          fontSize: ScreenUtil().setSp(28)),
                      decoration: InputDecoration(
                        hintText: "例:0001",
                        hintStyle: TextStyle(
                            color: MyColours.color_c1c1c1,
                            fontSize: ScreenUtil().setSp(28)),
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.only(bottom: -5),
                      ),
                      onChanged: (_) {
                        setActivate();
                      },
                      keyboardType:
                          TextInputType.numberWithOptions(decimal: true),
                    )),
                VEmptyView(40),
                Text(
                  '输入员工密码',
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: ScreenUtil().setSp(26)),
                ),
                Container(
                    height: ScreenUtil().setWidth(84),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              color: MyColours.color_efefef,
                              width: ScreenUtil().setWidth(2))),
                    ),
                    child: TextField(
                      controller: _pwdController,
                      focusNode: _pwdFocusNode,
                      // autofocus: _sellerCodeController.text.isNotEmpty &&
                      //     _accountController.text.isNotEmpty &&
                      //     _pwdController.text.isEmpty,
                      style: TextStyle(
                          color: MyColours.color_353535,
                          fontSize: ScreenUtil().setSp(28)),
                      decoration: InputDecoration(
                        hintText: "请输入六位员工密码",
                        hintStyle: TextStyle(
                            color: MyColours.color_c1c1c1,
                            fontSize: ScreenUtil().setSp(28)),
                        border: InputBorder.none,
                        contentPadding: EdgeInsets.only(bottom: -5),
                      ),
                      onChanged: (_) {
                        setActivate();
                      },
                      obscureText: true,
                      keyboardType: TextInputType.text,
                    )),
                VEmptyView(40),
                Consumer<UserModel>(
                  builder:
                      (BuildContext context, UserModel value, Widget child) {
                    return GestureDetector(
                        onTap: () {
                          if (!isActivate) return;

                          if (App.sp.containsKey('unionid')) {
                            //微信登陆的时候,退出清空数据
                            App.sp.remove('openid');
                            App.sp.remove("unionid");
                          }
                          Map<String, String> params = {
                            "seller_code": "${_sellerCodeController.text}",
                            "alias": "ssahdahgdafgahjkhakjgh",
                            "account": "${_accountController.text}",
                            "password": "${_pwdController.text}"
                          };
                          value
                              .login(context, params, acclogin: true)
                              .then((value) {});
                        },
                        child: Center(
                            child: Container(
                          height: ScreenUtil().setWidth(84),
                          width: ScreenUtil().setWidth(350),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              color: isActivate
                                  ? MyColours.color_41444B
                                  : MyColours.color_F7F7F7,
                              borderRadius: BorderRadius.all(
                                  Radius.circular(ScreenUtil().setWidth(8)))),
                          child: Text(
                            "登录",
                            style: TextStyle(
                                color: isActivate
                                    ? MyColours.color_ffffff
                                    : MyColours.color_C9C9C9,
                                fontSize: ScreenUtil().setSp(34)),
                          ),
                        )));
                  },
                ),
              ],
            ),
          ),
        ));
  }
}