failing to navigate to next screen after applying correct password into showdialog textfield in flutter

huangapple go评论65阅读模式
英文:

failing to navigate to next screen after applying correct password into showdialog textfield in flutter

问题

我正在创建一个简单的记事应用程序,其中我设置了一个锁定记事的选项。

在点击“LOCKNOTE”时会出现一个showDialog以输入密码,如果密码正确,应该跳转到下一个屏幕。我尝试过使用print('success'),在密码正确时它会显示成功,但不会跳转到下一个屏幕。看起来与上下文有关。

以下代码可以工作:Navigator.of(context).pop(),但不能推到下一个屏幕。

请帮助我解决这个问题...
提前感谢。

英文:

I am creating simple note app..where i have set an option of lock note...

a showDialog appears while tapping on LOCKNOTE for password, it should move to next screen on right password...i hve treid with print('success'),, its showing success on right password but not moving to next screen..looks like something related to context,

it work Navigator.of(context).pop() but not working push to next screen

help me to solve this...
Thanks in advance

return Expanded(
      child: ListView.builder(
          padding: EdgeInsets.zero,
          itemCount: provider.count_notes,
          itemBuilder: (context, index) {
            final note = provider.getAllNotes[index];
            return NoteWidget(
              onTap: () {
                if(note.isLocked==true)
                  {
                     showDialog(context: context, builder: (context){
                      return AlertDialog(
                        title: Text('Enter Password'),
                        content: TextField(
                          controller: txtnotepassword,
                        ),
                        actions: [
                          TextButton(onPressed: (){
                            if(txtnotepassword.text=='123')
                              {
                                print('Success');//shows in log but not moving to nextscreen
                                Navigator.of(context).pop();
                                Navigator.of(context)
                                    .push(MaterialPageRoute(builder: (context) {
                                  return EditNoteScreen(
                                    note: note,
                                  );
                                }));
                              }
                            else
                              {
                                showMessage(context,'Wrong Password');

                              }
                            Navigator.of(context).pop();
                        }, child: Text('Submit'))],
                      );
                    });

                    return;
                  }

                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
                  return EditNoteScreen(
                    note: note,
                  );
                }));
              },
              note: note,
            );
          }),
    );

答案1

得分: 1

我正确阅读了你的代码。在pop()之后执行push()永远不会生效,因为一旦你执行pop(),你的AlertDialog将被销毁,包括其中的函数,因此你的push()方法永远不会被调用。如果你想实时尝试,可以将你的打印语句移到pop()方法下面,它将不会被打印。

如果你想在点击提交后跳转到下一页,你应该这样做:

  1. 全局声明一个bool标志来跟踪是否点击了提交按钮。例如:
bool isSubmitted = false;
  1. 在你的TextButtononPressed函数内部,做如下操作:
if (txtnotepassword.text == '123') {
    isSubmitted = true;
} else {
    isSubmitted = false;
    showMessage(context, 'Wrong Password');
}
Navigator.pop(context);
  1. 在你的showDialog的结束括号后,添加一个.then()回调,如下所示:
showDialog(...你的对话框 UI 代码)
  .then((_) {
    if (isSubmitted) {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
           return EditNoteScreen(
              note: note,
           );
        }));
    }
});

希望这有所帮助 failing to navigate to next screen after applying correct password into showdialog textfield in flutter

英文:

I properly read your code. Doing the push() after the pop() will never work, because the instant you pop(), your AlertDialog will be destroyed, including the function inside it, so your push() method is never called. If you want to try it realtime, try moving your print statement below your pop() method, it will not be printed.

If you want to go to the next page after hitting Submit this is what you should do:

  1. Declare a bool flag globally to keep track of whether submit was tapped or not. Eg.

bool isSubmitted = false;

  1. Inside your TextButton's onPressed function, do this:
if (txtnotepassword.text == '123') {
    isSubmitted = true;
} else {
    isSubmitted = false;
    showMessage(context, 'Wrong Password');
}
Navigator.pop(context);
  1. After your showDialog's ending paranthesis, add a .then() callback like this:
showDialog(...your dialog UI code here)
  .then((_) {
    if (isSubmitted) {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
           return EditNoteScreen(
              note: note,
           );
        }));
    }
});

Hope this helps failing to navigate to next screen after applying correct password into showdialog textfield in flutter

huangapple
  • 本文由 发表于 2023年7月10日 18:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定