显示Snackbar在showModalBottomSheet上方,Flutter

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

Show Snackbar above to showModalBottomSheet Flutter

问题

我有一个showModalBottomSheet用于获取OTP验证码,如果用户输入错误的验证码,我想显示snackBar。所以我使用了这个方法,

showModalBottomSheet(
    isScrollControlled: true,
    isDismissible: false,
    context: context,
    builder: (context) {
      return SafeArea(
        child: Stack(
          alignment: Alignment.topCenter,
          clipBehavior: Clip.none,
          children: [
            Column(
              children: [
                Container(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 30, vertical: 0),
                  child: Column(children: [
                    Padding(
                      padding:
                          const EdgeInsets.symmetric(horizontal: 50),
                      child: TextField(
                        controller: codeController,
                        keyboardType: TextInputType.number,
                      ),
                    ),
                    const SizedBox(
                      height: 5,
                    ),
                    Padding(
                      padding:
                          const EdgeInsets.symmetric(horizontal: 50),
                      child: CustomButton(
                          onTap: () async {
                            if (codeController.text.length == 6) {
                              try {
                                PhoneAuthCredential credential =
                                    PhoneAuthProvider.credential(
                                        verificationId: verificationId,
                                        smsCode:
                                            codeController.text.trim());
                                await _auth
                                    .signInWithCredential(credential);
                                Navigator.of(context)
                                    .pushNamedAndRemoveUntil(
                                        '/home',
                                        (Route<dynamic> route) =>
                                            false);
                              } catch (e) {
                                // ---------------------------------------------- 
                                //这个snackbar 
                                showSnackBar(
                                     context,
                                     "Invalid verification code",
                                     "Please enter correct verification code");
                                }
                              }
                          },
                          text: "Continue",
                          height: 50),
                    )
                  ]),
                )
              ],
            )
          ],
        ),
      );
    });

SnackBar显示在showModalBottomSheet下面,所以我想要在不隐藏showModalBottomSheet的情况下将SnackBar显示在showModalBottomSheet上方。

英文:

I have showModalBottomSheet to get OTP code, If the user enter wrong code, I want to show snackBar. So, I used this method,

showModalBottomSheet(
        isScrollControlled: true,
        isDismissible: false,
        context: context,
        builder: (context) {
          return SafeArea(
            child: Stack(
              alignment: Alignment.topCenter,
              clipBehavior: Clip.none,
              children: [
                Column(
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 30, vertical: 0),
                      child: Column(children: [
                        Padding(
                          padding:
                              const EdgeInsets.symmetric(horizontal: 50),
                          child: TextField(
                            controller: codeController,
                            keyboardType: TextInputType.number,
                          ),
                        ),
                        const SizedBox(
                          height: 5,
                        ),
                        Padding(
                          padding:
                              const EdgeInsets.symmetric(horizontal: 50),
                          child: CustomButton(
                              onTap: () async {
                                if (codeController.text.length == 6) {
                                  try {
                                    PhoneAuthCredential credential =
                                        PhoneAuthProvider.credential(
                                            verificationId: verificationId,
                                            smsCode:
                                                codeController.text.trim());
                                    await _auth
                                        .signInWithCredential(credential);
                                    Navigator.of(context)
                                        .pushNamedAndRemoveUntil(
                                            &#39;/home&#39;,
                                            (Route&lt;dynamic&gt; route) =&gt;
                                                false);
                                  } catch (e) {
                                  // ---------------------------------------------- 
                                  //this snackbar 
                                     showSnackBar(
                                           context,
                                           &quot;Invalid verification code&quot;,
                                          &quot;Please enter correct verification code&quot;);
                                    }
                                  }
                              },
                              text: &quot;Continue&quot;,
                              height: 50),
                        )
                      ]),
                    )
                  ],
                )
              ],
            ),
          );
        });

The snackBar showing under the showModalBottomSheet, So I want to show the snackBar above to showModalBottomSheet without hiding showModalBottomSheet.

答案1

得分: 1

你可以使用另一个 Scaffold 来从 showModalBottomSheet 中获取新的 ScaffoldMessenger,而不是顶层。

showModalBottomSheet(
    isScrollControlled: true,
    isDismissible: false,
    context: context,
    builder: (context) {
      return SafeArea(
        child: Scaffold( // 这里使用另一个 Scaffold
          body: Stack(
            alignment: Alignment.topCenter,
            clipBehavior: Clip.none,
            children: [
              Column(
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(
                        horizontal: 30, vertical: 0),
                    child: Column(
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            const SnackBar snackBar = SnackBar(
                                content: Text(
                                    "嘿,我在底部弹出窗口上方"));
                            ScaffoldMessenger.of(context)
                                .showSnackBar(snackBar);
                          },
                          child: Text("显示 SnackBar"),
                        )
                      ],
                    ),
                  )
                ],
              )
            ],
          ),
        ),
      );
    });
英文:

You can use another Scaffold to get new ScaffoldMessenger from showModalBottomSheet instead of top level.

showModalBottomSheet(
    isScrollControlled: true,
    isDismissible: false,
    context: context,
    builder: (context) {
      return SafeArea(
        child: Scaffold( //here another scaffold
          body: Stack(
            alignment: Alignment.topCenter,
            clipBehavior: Clip.none,
            children: [
              Column(
                children: [
                  Container(
                    padding: const EdgeInsets.symmetric(
                        horizontal: 30, vertical: 0),
                    child: Column(
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            const SnackBar snackBar = SnackBar(
                                content: Text(
                                    &quot;Hey, I am above BottomSheet&quot;));
                            ScaffoldMessenger.of(context)
                                .showSnackBar(snackBar);
                          },
                          child: Text(&quot;show SnackBar&quot;),
                        )
                      ],
                    ),
                  )
                ],
              )
            ],
          ),
        ),
      );
    });

huangapple
  • 本文由 发表于 2023年1月9日 02:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050334.html
匿名

发表评论

匿名网友

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

确定