123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
- import 'package:lszlgl/base/base_state.dart';
- import 'package:lszlgl/main.dart';
- import 'package:lszlgl/network/my_api.dart';
- import 'package:lszlgl/widget/button.dart';
- /// 修改密码
- class ChangePwdPage extends StatefulWidget {
- final bool startHome;
- const ChangePwdPage({
- Key? key,
- bool? startHome,
- }) : startHome = startHome ?? false,
- super(key: key);
- @override
- State<ChangePwdPage> createState() => _ChangePwdPageState();
- }
- class _ChangePwdPageState extends BaseState<ChangePwdPage> {
- late TextEditingController oldPwdCtrl;
- late TextEditingController newPwdCtrl;
- late TextEditingController confirmPwdCtrl;
- final ValueNotifier<String?> oldError = null.notifier();
- final ValueNotifier<String?> newError = null.notifier();
- final ValueNotifier<String?> confirmError = null.notifier();
- void onChange() async {
- List<bool> verifyList = await Future.wait([verifyOldText(), verifyNewText(), verifyConfirmText()]);
- if (!verifyList.firstWhere((element) => !element, orElse: () => true)) {
- return;
- }
- MyNavigator.showLoading(msg: '密码修改中...');
- try {
- var rsp = await MyApi.get().updatePassword({
- 'oldPassword': oldPwdCtrl.text,
- 'newPassword': newPwdCtrl.text,
- });
- if (rsp.data ?? false) {
- MyNavigator.showToast('修改成功');
- if (widget.startHome) {
- // 进入主页
- MyRouter.startMain(popAll: true);
- } else {
- MyNavigator.pop();
- }
- }
- } catch (e) {
- logger.e(e);
- }
- MyNavigator.dismissLoading();
- }
- Future<bool> verifyOldText() async {
- var text = oldPwdCtrl.text;
- if (text.isEmpty) {
- oldError.value = '请输入旧密码';
- return false;
- }
- int length = text.length;
- if (length < 8) {
- oldError.value = '密码长度要大于8位';
- return false;
- }
- oldError.value = null;
- return true;
- }
- Future<bool> verifyNewText() async {
- var text = newPwdCtrl.text;
- if (text.isEmpty) {
- newError.value = '请输入新密码';
- return false;
- }
- int length = text.length;
- if (length < 8) {
- newError.value = '密码长度要大于8位';
- return false;
- }
- var regex = RegExp(r'\d');
- if (!regex.hasMatch(text)) {
- newError.value = '密码要包含数字';
- return false;
- }
- regex = RegExp(r'[a-zA-Z]');
- if (!regex.hasMatch(text)) {
- newError.value = '密码要包含大写字母或小写字母';
- return false;
- }
- regex = RegExp(r'[!\"#$%&()*+,-./:;<=>?@\]\[^_`{|}~]');
- if (!regex.hasMatch(text)) {
- newError.value = '密码要包含特殊字符';
- return false;
- }
- newError.value = null;
- return true;
- }
- Future<bool> verifyConfirmText() async {
- var text = confirmPwdCtrl.text;
- if (text.isEmpty) {
- confirmError.value = '请输入确认密码';
- return false;
- }
- if (text != newPwdCtrl.text) {
- confirmError.value = '两次输入密码不一致';
- return false;
- }
- confirmError.value = null;
- return true;
- }
- @override
- void initState() {
- super.initState();
- oldPwdCtrl = TextEditingController();
- newPwdCtrl = TextEditingController();
- confirmPwdCtrl = TextEditingController();
- }
- @override
- void dispose() {
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return myScaffold(
- child: KeyboardDismissOnTap(
- dismissOnCapturedTaps: true,
- child: buildBody(),
- ),
- );
- }
- Widget buildBody() {
- return Column(
- children: [
- myAppBar(title: '修改密码'),
- buildList(),
- const SizedBox(height: 32),
- MyButton(
- '确认修改',
- onTap: onChange,
- gradient: const LinearGradient(colors: [Color(0xFF3BD2E5), Color(0xFF247AF8)]),
- alignment: Alignment.center,
- minHeight: 40,
- margin: const EdgeInsets.symmetric(horizontal: 24),
- ),
- ],
- );
- }
- Widget buildList() {
- var formatters = [FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z0-9!\"#$%&()*+,-./:;<=>?@\]\[^_`{|}~]'))];
- return Container(
- margin: const EdgeInsets.symmetric(horizontal: 12),
- padding: const EdgeInsets.fromLTRB(16, 24, 16, 8),
- clipBehavior: Clip.hardEdge,
- decoration: const BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(10)),
- color: Colors.white,
- ),
- child: Column(
- children: [
- oldError.builder(
- (error) => buildEdit(
- ctrl: oldPwdCtrl,
- labelText: '旧密码',
- hint: '请输入',
- formatters: formatters,
- inputType: TextInputType.visiblePassword,
- obscureText: true,
- errorText: error,
- onChanged: (value) => verifyOldText(),
- ),
- ),
- const SizedBox(height: 12),
- newError.builder(
- (error) => buildEdit(
- ctrl: newPwdCtrl,
- labelText: '新密码',
- hint: '密码长度8-20位,需包含英文、数字及字符',
- formatters: formatters,
- inputType: TextInputType.visiblePassword,
- obscureText: true,
- maxLength: 20,
- onChanged: (value) => verifyNewText(),
- errorText: error,
- ),
- ),
- const SizedBox(height: 12),
- confirmError.builder(
- (error) => buildEdit(
- ctrl: confirmPwdCtrl,
- labelText: '确认密码',
- hint: '密码长度8-20位,需包含英文、数字及字符',
- formatters: formatters,
- inputType: TextInputType.visiblePassword,
- obscureText: true,
- maxLength: 20,
- onChanged: (value) => verifyConfirmText(),
- errorText: error,
- ),
- ),
- ],
- ),
- );
- }
- Widget buildEdit({
- required TextEditingController ctrl,
- String? hint,
- String? labelText,
- TextInputAction action = TextInputAction.next,
- bool obscureText = false,
- ValueChanged? onSubmit,
- ValueChanged<String>? onChanged,
- List<TextInputFormatter>? formatters,
- int? maxLength,
- String? errorText,
- TextInputType? inputType,
- }) {
- return TextField(
- controller: ctrl,
- decoration: InputDecoration(
- prefixIconConstraints: const BoxConstraints(),
- border: const OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(100))),
- enabledBorder: const OutlineInputBorder(
- borderSide: BorderSide(color: Color(0xFFE6E6E6), width: 1),
- borderRadius: BorderRadius.all(Radius.circular(100)),
- ),
- labelText: labelText,
- hintText: hint,
- hintStyle: const TextStyle(color: Color(0xFFBBBBBB)),
- isDense: true,
- contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
- errorText: errorText,
- ),
- maxLength: maxLength,
- style: const TextStyle(fontSize: 14),
- textInputAction: action,
- obscureText: obscureText,
- onSubmitted: onSubmit,
- onChanged: onChanged,
- inputFormatters: formatters,
- );
- }
- }
|