diff --git a/lib/main.dart b/lib/main.dart index a042e05..0d28e73 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ // Project imports: import 'package:aku_new_community_manager/provider/app_provider.dart'; +import 'package:aku_new_community_manager/provider/clock_timer_provider.dart'; import 'package:aku_new_community_manager/provider/data_provider.dart'; import 'package:aku_new_community_manager/provider/message_provider.dart'; import 'package:aku_new_community_manager/provider/user_provider.dart'; @@ -77,6 +78,7 @@ class MyApp extends StatelessWidget { ChangeNotifierProvider(create: (context) => AppProvider()), ChangeNotifierProvider(create: (context) => MessageProvider()), ChangeNotifierProvider(create: (context) => DataProvider()), + ChangeNotifierProvider(create: (context) => ClockTimerProvider()), ], child: GestureDetector( onTap: () { diff --git a/lib/new_ui/auth/code_message_page.dart b/lib/new_ui/auth/code_message_page.dart index 78b1933..a4a092b 100644 --- a/lib/new_ui/auth/code_message_page.dart +++ b/lib/new_ui/auth/code_message_page.dart @@ -1,10 +1,12 @@ import 'package:aku_new_community_manager/new_ui/auth/sign_func.dart'; +import 'package:aku_new_community_manager/provider/clock_timer_provider.dart'; import 'package:aku_new_community_manager/tools/user_tool.dart'; import 'package:aku_new_community_manager/ui/widgets/common/aku_scaffold.dart'; import 'package:bot_toast/bot_toast.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; +import 'package:provider/provider.dart'; import 'package:sms_autofill/sms_autofill.dart'; import 'package:velocity_x/velocity_x.dart'; @@ -24,20 +26,20 @@ class _CodeMessagePageState extends State { @override void initState() { - Future.delayed(Duration(milliseconds: 0), () async { - await SmsAutoFill().listenForCode(); - }); super.initState(); + SmsAutoFill().listenForCode(); } @override void dispose() { _controller.dispose(); + SmsAutoFill().unregisterListener(); super.dispose(); } @override Widget build(BuildContext context) { + ClockTimerProvider clockTimer = Provider.of(context); return AkuScaffold( title: '', body: SafeArea( @@ -115,14 +117,14 @@ class _CodeMessagePageState extends State { .communityModel!.id); if (base.success) { _errorMessage = null; - UserTool.appProvider.startTimer(); + clockTimer.startTimer(); } else { BotToast.showText(text: base.msg); } }, - child: (UserTool.appProvider.second >= 60 + child: (clockTimer.second >= 60 ? '重新发送验证码' - : '${UserTool.appProvider.second}秒后可重新发送') + : '${clockTimer.second}秒后可重新发送') .text .size(28.sp) .color(Color(0xFF5096F1)) diff --git a/lib/new_ui/auth/forgot_psd_page.dart b/lib/new_ui/auth/forgot_psd_page.dart index 4228912..fc6d058 100644 --- a/lib/new_ui/auth/forgot_psd_page.dart +++ b/lib/new_ui/auth/forgot_psd_page.dart @@ -1,6 +1,7 @@ import 'package:aku_new_community_manager/new_ui/auth/sign_func.dart'; import 'package:aku_new_community_manager/new_ui/auth/widget/login_button_widget.dart'; import 'package:aku_new_community_manager/new_ui/auth/widget/tel_textfield.dart'; +import 'package:aku_new_community_manager/provider/clock_timer_provider.dart'; import 'package:aku_new_community_manager/tools/user_tool.dart'; import 'package:aku_new_community_manager/ui/widgets/common/aku_scaffold.dart'; import 'package:bot_toast/bot_toast.dart'; @@ -8,6 +9,7 @@ import 'package:common_utils/common_utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; +import 'package:provider/provider.dart'; import 'package:velocity_x/velocity_x.dart'; import 'forgot_set_psd_page.dart'; @@ -30,6 +32,7 @@ class _ForgotPsdPageState extends State { @override Widget build(BuildContext context) { + ClockTimerProvider clockTimer = Provider.of(context); return AkuScaffold( title: '', backgroundColor: Colors.white, @@ -84,7 +87,7 @@ class _ForgotPsdPageState extends State { Get.to(() => ForgotSetPsdPage( tel: _telController.text, )); - UserTool.appProvider.startTimer(); + clockTimer.startTimer(); } else { BotToast.showText(text: base.msg); } diff --git a/lib/new_ui/auth/forgot_set_psd_page.dart b/lib/new_ui/auth/forgot_set_psd_page.dart index c536cc8..ec9669c 100644 --- a/lib/new_ui/auth/forgot_set_psd_page.dart +++ b/lib/new_ui/auth/forgot_set_psd_page.dart @@ -3,6 +3,7 @@ import 'package:aku_new_community_manager/new_ui/auth/psd_verify.dart'; import 'package:aku_new_community_manager/new_ui/auth/sign_func.dart'; import 'package:aku_new_community_manager/new_ui/auth/widget/login_button_widget.dart'; import 'package:aku_new_community_manager/new_ui/auth/widget/psd_textfield.dart'; +import 'package:aku_new_community_manager/provider/clock_timer_provider.dart'; import 'package:aku_new_community_manager/tools/user_tool.dart'; import 'package:aku_new_community_manager/ui/widgets/common/aku_scaffold.dart'; import 'package:bot_toast/bot_toast.dart'; @@ -10,6 +11,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; +import 'package:provider/provider.dart'; import 'package:velocity_x/velocity_x.dart'; class ForgotSetPsdPage extends StatefulWidget { @@ -52,6 +54,7 @@ class _ForgotSetPsdPageState extends State { @override Widget build(BuildContext context) { + ClockTimerProvider clockTimer = Provider.of(context); return AkuScaffold( title: '', backgroundColor: Colors.white, @@ -137,7 +140,7 @@ class _ForgotSetPsdPageState extends State { child: Row( mainAxisSize: MainAxisSize.min, children: [ - '|${UserTool.appProvider.second < 60 ? '${UserTool.appProvider.second}秒后重新获取' : '获取验证码'}' + '|${clockTimer.second < 60 ? '${clockTimer.second}秒后重新获取' : '获取验证码'}' .text .size(28.sp) .color(Color(0xFF5096F1)) diff --git a/lib/new_ui/auth/other_login_page.dart b/lib/new_ui/auth/other_login_page.dart index 66bc147..65f6076 100644 --- a/lib/new_ui/auth/other_login_page.dart +++ b/lib/new_ui/auth/other_login_page.dart @@ -3,6 +3,7 @@ import 'package:aku_new_community_manager/new_ui/auth/widget/login_button_widget import 'package:aku_new_community_manager/new_ui/auth/widget/psd_textfield.dart'; import 'package:aku_new_community_manager/new_ui/auth/widget/tel_textfield.dart'; import 'package:aku_new_community_manager/provider/app_provider.dart'; +import 'package:aku_new_community_manager/provider/clock_timer_provider.dart'; import 'package:aku_new_community_manager/tools/user_tool.dart'; import 'package:aku_new_community_manager/ui/widgets/common/aku_scaffold.dart'; import 'package:bot_toast/bot_toast.dart'; @@ -154,7 +155,7 @@ class _OtherLoginPageState extends State { } Widget _verificationView() { - AppProvider appProvider = Provider.of(context); + ClockTimerProvider clockTimer = Provider.of(context); return ListView( padding: EdgeInsets.symmetric(horizontal: 32.w), children: [ @@ -171,7 +172,7 @@ class _OtherLoginPageState extends State { ), 100.w.heightBox, LoginButtonWidget( - onTap: UserTool.appProvider.second < 60 + onTap: clockTimer.second < 60 ? () {} : () async { var check = checkInput(); @@ -184,13 +185,13 @@ class _OtherLoginPageState extends State { .communityModel!.id); if (base.success) { Get.to(() => CodeMessagePage(tel: _tel.text)); - appProvider.startTimer(); + clockTimer.startTimer(); } else { BotToast.showText(text: base.msg); } }, - text: appProvider.timerStart - ? '${appProvider.second}秒后重新获取' + text: clockTimer.timerStart + ? '${clockTimer.second}秒后重新获取' : '获取验证码'), 24.w.heightBox, Row( diff --git a/lib/provider/clock_timer_provider.dart b/lib/provider/clock_timer_provider.dart new file mode 100644 index 0000000..aa33dd8 --- /dev/null +++ b/lib/provider/clock_timer_provider.dart @@ -0,0 +1,30 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; + +class ClockTimerProvider extends ChangeNotifier{ + ///登录页验证码计时器 + int second = 60; + bool timerStart = false; + Timer? timer; + + void startTimer() { + timerStart = true; + timer = Timer.periodic(Duration(seconds: 1), (timer) { + if (second > 0) { + second--; + notifyListeners(); + } else { + stopTimer(); + } + }); + } + + void stopTimer() { + second = 60; + timerStart = false; + timer?.cancel(); + timer = null; + notifyListeners(); + } +} \ No newline at end of file