1、使用form进行表单接收
将登录的用户信息写入本地文件,在main文件中进行判断是否具有用户信息即可实现登录
final TextEditingController _controller1 = TextEditingController(); //账号控制器
final TextEditingController _controller2 = TextEditingController(); //密码控制器
final TextEditingController _controller3 = TextEditingController(); // 验证码控制器
final _formKey = GlobalKey<FormState>();
Form(
key: _formKey,
child: Column(
children: [
InputMessageWidget(
controller: _controller1,
topPadding: 30.0,
title: "账号",
hintText: "这里输入你的账号",
prefixIcon: Icons.person_outline,
codeImage: null,
validator: (value) {
if (value!.isEmpty) {
return '账号不能为空';
}
return null;
},
),
InputMessageWidget(
obscureText: true,
controller: _controller2,
title: "密码",
hintText: "这里输入你的密码",
prefixIcon: Icons.lock_outline,
codeImage: null,
validator: (value) {
if (value!.isEmpty) {
return '密码不能为空';
} else if (value!.length < 8 ||
value.length > 12) {
return '密码长度必须为8-12位';
} else if (!RegExp(
r'^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,12}$')
.hasMatch(value)) {
return '密码必须包含字母和数字';
}
return null;
},
),
InputMessageWidget(
controller: _controller3,
title: "验证码",
hintText: "这里输入验证码",
prefixIcon:
Icons.phone_iphone_outlined,
codeImage: model.randomStr,
personModel: model,
codeInput: true,
validator: (value) {
if (value!.isEmpty) {
return '验证码不能为空';
}
return null;
},
),
Padding(
padding: EdgeInsets.only(
top: 150.h,
left: 24.w,
right: 24.w),
child: Container(
height: 48.h,
width: size.width,
decoration:
const BoxDecoration(
borderRadius:
BorderRadius.all(
Radius
.circular(
4))),
child: ElevatedButton(
style: ElevatedButton
.styleFrom(
backgroundColor:
AppColor
.f446dff),
onPressed: () async {
String text1 =
_controller1.text;
String text2 =
_controller2.text;
String text3 =
_controller3.text;
model.username = text1;
model.code = text3;
model.password = text2;
if (_formKey.currentState!
.validate()) {
_formKey.currentState
?.save();
// TODO: 提交表单
await model.doLogin();
if (model
.loginSuccess) {
Navigator.pushNamedAndRemoveUntil(context, '/home', (route) => false);
}
}
},
child: const Text("登录"),
),
)),
],
),
),
2、请求完成进行缓存登录
1.读取文件
import 'package:shared_preferences/shared_preferences.dart'; //使用工具类完成保存和读取一些简单的配置信息
class LocalStorage {
static final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
static Future<String?> getString(String key) async {
final prefs = await _prefs;
return prefs.getString(key);
}
static Future<bool> setString(String key, String value) async {
final prefs = await _prefs;
return prefs.setString(key, value);
}
static Future<int?> getInt(String key) async {
final prefs = await _prefs;
return prefs.getInt(key);
}
static Future<bool> setInt(String key, int value) async {
final prefs = await _prefs;
return prefs.setInt(key, value);
}
static Future<bool?> getBool(String key) async {
final prefs = await _prefs;
return prefs.getBool(key);
}
static Future<bool> setBool(String key, bool value) async {
final prefs = await _prefs;
return prefs.setBool(key, value);
}
cleanCache() async {
final prefs = await _prefs;
prefs.clear();
}
removeCache(String key)async{
final prefs = await _prefs;
prefs.remove(key);
}
}
2.键名配置文件
class AppConst {
static const String keyLoginToken = "login_token";
static const String keyRefreshToken = "refresh_token";
static const String keyExpiredIn = "token_expired_in";
static const String keyServerUrl = 'server_url';
static const String userInfo = 'user_info';
static const String appVersion = 'app_version';
static const String serverUrlProd = 'https://www.bphc.com.cn';
static const String serverUrlTest = 'http://10.39.65.60:15123';
static const double lagerTextSize = 30.0;
static const double bigTextSize = 23.0;
static const double textSize20 = 20.0;
static const double normalTextSize = 18.0;
static const double middleTextWhiteSize = 16.0;
static const double smallTextSize = 14.0;
static const double minTextSize = 12.0;
3、user用户信息文件
class CoreUser {
CoreUserDto? _userDto;
CoreUserDto? get userDto => _userDto;
void set userDto(CoreUserDto? value) {
_userDto = value;
}
static CoreUser? _instance;
CoreUser._internal();
/// 工厂构造方法,这里使用命名构造函数方式进行声明
factory CoreUser.me() => _getInstance();
// 获取单例内部方法
static _getInstance() {
// 只能有一个实例
if (_instance == null) {
_instance = CoreUser._internal();
}
return _instance!;
}
Future<CoreUserDto?> init() async {
String? userInfoStr = await LocalStorage.getString(AppConst.userInfo);
if (userInfoStr != null) {
_userDto = CoreUserDto.fromJson(jsonDecode(userInfoStr));
}
return _userDto;
}
save2Local(CoreUserDto userDto) async {
_userDto = userDto;
await LocalStorage.setString(AppConst.userInfo, jsonEncode(userDto.toJson()));
}
}
}