login_page.dart 8.58 KB
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:one_poem/common/permission_request_widget.dart';
import 'package:one_poem/common/protocol_model.dart';
import 'package:one_poem/home/webview_page.dart';
import 'package:one_poem/login/widgets/my_text_field.dart';
import 'package:one_poem/res/constant.dart';
import 'package:one_poem/res/resources.dart';
import 'package:one_poem/routers/fluro_navigator.dart';
import 'package:one_poem/routers/routers.dart';
import 'package:one_poem/util/change_notifier_manage.dart';
import 'package:one_poem/util/other_utils.dart';
import 'package:one_poem/widgets/my_app_bar.dart';
import 'package:one_poem/widgets/my_button.dart';
import 'package:one_poem/widgets/my_scroll_view.dart';

import '../login_router.dart';

import 'package:flutter_gen/gen_l10n/one_poem_localizations.dart';
import 'package:one_poem/extension/int_extension.dart';

import 'package:permission_handler/permission_handler.dart';

/// design/1注册登录/index.html
class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage>
    with ChangeNotifierMixin<LoginPage>, ProtocolModel {
  //定义一个controller
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  final FocusNode _nodeText2 = FocusNode();
  bool _clickable = false;
  bool isLogin = false;

  @override
  Map<ChangeNotifier, List<VoidCallback>?>? changeNotifier() {
    final List<VoidCallback> callbacks = <VoidCallback>[_verify];
    return <ChangeNotifier, List<VoidCallback>?>{
      _nameController: callbacks,
      _passwordController: callbacks,
      _nodeText1: null,
      _nodeText2: null,
    };
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((_) {
      /// 显示状态栏和导航栏
      SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
          overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
    });
    _nameController.text = SpUtil.getString(Constant.phone).nullSafe;

    List<String> _list = [
      "为您更好的体验应用,所以需要获取您的手机文件存储权限,以保存您的一些偏好设置",
      "您已拒绝权限,所以无法保存您的一些偏好设置,将无法使用APP",
      "您已拒绝权限,请在设置中心中同意APP的权限请求",
      "其他错误"
    ];

    Future.delayed(Duration.zero, () {
      NavigatorUtils.pushPageByFade(
          context: context,
          //目标页面
          targetPage: PermissionRequestWidget(
            //所需要申请的权限
            permission: Permission.camera,
            //显示关闭应用按钮
            isCloseApp: true,
            //提示文案
            permissionList: _list,
          ),
          //权限申请结果
          dismissCallBack: (value) {
            showPrivacyPage();
          });
    });
  }

  void showPrivacyPage() async {
    bool isAgreement = await showProtocolFunction(context);
    if (isAgreement) {
      // next();
    } else {
      SystemChannels.platform.invokeMethod("SystemNavigator.pop");
    }
  }

  void _verify() {
    final String name = _nameController.text;
    final String password = _passwordController.text;
    bool clickable = true;
    if (name.isEmpty || name.length < 11) {
      clickable = false;
    }
    if (password.isEmpty || password.length < 6) {
      clickable = false;
    }

    /// 状态不一样再刷新,避免不必要的setState
    if (clickable != _clickable) {
      setState(() {
        _clickable = clickable;
      });
    }
  }

  void _login() {
    isLogin = true;
    setState(() {});
    Future.delayed(const Duration(seconds: 2), () {
      //TODO 接入接口后获得user token则表明登录成功
      SpUtil.putString(Constant.userToken, "this is user token!");
      NavigatorUtils.push(context, Routes.home, clearStack: true);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        isBack: false,
        isTransparent: true,
        onPressed: () {
          NavigatorUtils.push(context, LoginRouter.smsLoginPage);
        },
      ),
      body: Stack(
        children: [
          MyScrollView(
            keyboardConfig: Utils.getKeyboardActionsConfig(context, <FocusNode>[
              _nodeText1,
              _nodeText2,
            ]),
            padding: EdgeInsets.only(
              left: 16.px,
              right: 16.px,
              top: 20.px,
            ),
            children: _buildBody,
          ),
          isLogin
              ? const Center(
                  child: CupertinoActivityIndicator(
                    radius: 16.0,
                  ),
                )
              : Container(),
        ],
      ),
    );
  }

  List<Widget> get _buildBody => <Widget>[
        Text(
          OnePoemLocalizations.of(context).passwordLogin,
          style: TextStyles.textBold26,
        ),
        Gaps.vGap16,
        MyTextField(
          key: const Key('phone'),
          focusNode: _nodeText1,
          controller: _nameController,
          maxLength: 11,
          keyboardType: TextInputType.phone,
          hintText: OnePoemLocalizations.of(context).inputUsernameHint,
        ),
        Gaps.vGap8,
        MyTextField(
          key: const Key('password'),
          keyName: 'password',
          focusNode: _nodeText2,
          isInputPwd: true,
          controller: _passwordController,
          keyboardType: TextInputType.visiblePassword,
          hintText: OnePoemLocalizations.of(context).inputPasswordHint,
        ),
        Gaps.vGap24,
        Text.rich(
          TextSpan(
              text: '登录即代表同意并阅读',
              style: const TextStyle(fontSize: 12, color: Color(0xFF999999)),
              children: [
                TextSpan(
                  text: '《用户协议》',
                  style: TextStyle(color: Theme.of(context).primaryColor),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      Navigator.of(context)
                          .push(MaterialPageRoute(builder: (context) {
                        return const WebViewPage(
                          title: '《用户协议》',
                          url: Constant.protocolUrl,
                        );
                      }));
                    },
                ),
                const TextSpan(text: ' & '),
                TextSpan(
                  text: '《隐私政策》',
                  style: TextStyle(color: Theme.of(context).primaryColor),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (context) {
                            return const WebViewPage(
                              title: '《隐私政策》',
                              url: Constant.privacyUrl,
                            );
                          },
                        ),
                      );
                    },
                ),
              ]),
        ),
        Gaps.vGap16,
        MyButton(
          key: const Key('login'),
          onPressed: _clickable ? _login : null,
          text: OnePoemLocalizations.of(context).login,
        ),
        Container(
          height: 40.px,
          alignment: Alignment.centerRight,
          child: GestureDetector(
            child: Text(
              OnePoemLocalizations.of(context).forgotPasswordLink,
              key: const Key('forgotPassword'),
              style: Theme.of(context).textTheme.subtitle2,
            ),
            onTap: () =>
                NavigatorUtils.push(context, LoginRouter.resetPasswordPage),
          ),
        ),
        Gaps.vGap16,
        Container(
            alignment: Alignment.center,
            child: GestureDetector(
              child: Text(
                OnePoemLocalizations.of(context).noAccountRegisterLink,
                key: const Key('noAccountRegister'),
                style: TextStyle(color: Theme.of(context).primaryColor),
              ),
              onTap: () =>
                  NavigatorUtils.push(context, LoginRouter.registerPage),
            ))
      ];
}