flutter form登录相关记录

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:oktoast/oktoast.dart';

class Login extends StatefulWidget {
   
  const Login({
   
    Key key,
    @required this.controller,
    this.maxLength: 11,
    this.maxCodeLength: 6,
    this.getVCode,
    this.countdown: 10,
  }) : super(key: key);

  final TextEditingController controller;
  final int maxLength;
  final int maxCodeLength;
  final Function getVCode;

  /// 倒计时的秒数,默认60秒。
  final int countdown;
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
   
  final loginKey = GlobalKey<FormState>(); // 创建from key
  String phone = '', code, errorTile = '';
  bool check = false; // 复选框值
  bool autovalidate = false; // 设置动态表单验证功能
  bool isGetCode = true; // 是否可以获取验证码
  String _text = '获取验证码';
  // 倒计时的计时器。
  Timer _timer;
  // 当前倒计时的秒数。
  int _seconds = 10;

  void submitLogin() {
   
    _showToast('请填写手机号');
    //点击登录 如果校验通过 提交
    if (loginKey.currentState.validate() && check) {
   
      loginKey.currentState.save(); //保存表单数据
      debugPrint('phone===>$phone');
      debugPrint('code===>$code');

      Scaffold.of(context).showSnackBar(SnackBar(content: Text('正在登录...')));
    } else {
   
      String text = check ? '' : '请勾选信息';
      setState(() {
   
        // 动态修改 是否校验
        autovalidate = true;
        errorTile = text;
      });
    }
  }

  String isChinaPhoneLegal(String str) {
   
    if (str.isEmpty) {
   
      return "手机号不能为空";
    } else if (!new RegExp(
            '^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$')
        .hasMatch(str)) {
   
      return "手机号格式不正确";
    }
    return null;
  }

  String isCode(String str) {
   
    if (str.isEmpty) {
   
      return "验证码不能为空";
    } else if (str.length < 4) {
   
      return "验证码长度不够";
    } else if (!new RegExp(r'\d+').hasMatch(str)) {
   
      return "验证码格式不正确";
    }
    return null;
  }

  void _startTimer() {
   
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
   
      _seconds--;
      setState(() {
   
        isGetCode = false;
        _text = '已发送(${_seconds}s)';
      });
      if (_seconds == 0) {
   
        setState(() {
   
          _text = '重新获取';
          isGetCode = true;
          _seconds = widget.countdown;
        });
        _cancelTimer();
      }
    });
  }

//获取验证码弹框提示
  /* _openAlertDialog() {
    showDialog(
        context: context,
        // barrierDismissible: false,// 点击空白不关闭遮罩
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('提示'),
            content: Text('请输入正确的手机号'),
            actions: <Widget>[
              FlatButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('确定'))
            ],
          );
        });
  }*/
  // 提示框
  void _showToast(String desc) {
   
    // 3.1 use showToast method
    showToast(desc,
        duration: Duration(milliseconds: 2000),
        position: ToastPosition.center,
        backgroundColor: Colors.black.withOpacity(0.7),
        radius: 3.0,
        textStyle: TextStyle(fontSize: 18.0, color: Colors.white));
  }

//说明提示
  _openAlertDialog() {
   
//   _openServerDialog() {
   
    showDialog(
        context: context,
        barrierDismissible: false, // 点击空白不关闭遮罩
        builder: (BuildContext context) {
   
          return WillPopScope(
              child: SimpleDialog(
                title: Text(
                  "服务协议和隐私政策",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontWeight: FontWeight.bold
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值