dart 保存导航中的页面状态

2skhul33  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(106)

尝试使用Navigation.push()在登录和忘记密码页面之间来回导航。
但忘记密码页面保持失去其状态(这里的倒计时计时器)。
1.首页关于App Home
1.当我输入电子邮件时,它会显示一条消息和一个计数器(从30到0)。Page State
1.我回到登录页面,回来的计时器和消息都不见了。State Gone
如何保存Navigation.push()页面的状态?
Forgot.dart

class ForgotPassword extends StatefulWidget {
  const ForgotPassword({Key? key}) : super(key: key);

  @override
  _ForgotPasswordState createState() => _ForgotPasswordState();
}

class _ForgotPasswordState extends State<ForgotPassword> {
  TextEditingController emailC = TextEditingController();
  final formkey = GlobalKey<FormState>();
  AuthMethods authMethods = AuthMethods();
  bool sent = false;
  Timer? _timer;
  int _start = 0;

  void startTimer() async {
    if (formkey.currentState!.validate() && _start == 0) {
      _start = 30;
      const oneSec = const Duration(seconds: 1);
      _timer = new Timer.periodic(
        oneSec,
        (Timer timer) {
          if (_start == 0) {
            setState(() {
              timer.cancel();
            });
          } else {
            setState(() {
              _start--;
            });
          }
        },
      );
      await authMethods.resetPassword(emailC.text);
    }
  }

  @override
  void dispose() {
    _timer!.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(... rest of code
bn31dyow

bn31dyow1#

body: PageView( 
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),
          children: [
            FirstScreen(),
            SecondScreen(),
          ],
        ),

将Navigator.push()/pop()替换为_pageController.goToPage(0或1)

相关问题