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
flutter form登录相关记录
最新推荐文章于 2023-02-16 17:43:51 发布