1

如果底部工作表出现在屏幕上,我希望禁用后退按钮。当我使用 will pop 范围时,它向我显示 Future(动态不能分配给 Widget)的错误。我该如何解决这个问题 1. 如果屏幕底部出现我希望禁用后退按钮。当我使用 will pop 范围时,它向我显示 Future(动态不能分配给 Widget)的错误。我该如何解决这个问题 1. 如果屏幕底部出现我希望禁用后退按钮。当我使用 will pop 范围时,它向我显示 Future(动态不能分配给 Widget)的错误。我该如何解决这个问题

 if (_registerModel.status == "success") {
                            saveUserIDToSharedPreference(
                                "userID", _registerModel.id);
                            String displayToast =
                                "Registration successful \n OTP is ${_registerModel.otp}";
                            Fluttertoast.showToast(
                              msg: displayToast,
                              toastLength: Toast.LENGTH_LONG,
                              gravity: ToastGravity.BOTTOM,
                              timeInSecForIosWeb: 1,
                            );
                            buildShowEnterOTPModalBottomSheet(
                                context,
                                size,
                                email,
                                password,
                                _registerModel.id,
                                _registerModel.otp);
                          }
Future buildShowEnterOTPModalBottomSheet(
      BuildContext context, Size size, String email, int id) {
    VerifyOtpModel _verifyOtpModel;
    final TextEditingController otpController = TextEditingController();
    return showModalBottomSheet(
        isDismissible: false,
        enableDrag: false,
        isScrollControlled: true,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(top: Radius.circular(20.0)),
        ),
        context: context,
        builder: (context) {
          return AnimatedPadding(
            duration: Duration(milliseconds: 150),
            curve: Curves.easeOut,
            padding: EdgeInsets.only(
                bottom: MediaQuery.of(context).viewInsets.bottom),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.fromLTRB(0, 24.0, 0, 18.0),
                  child: Container(
                    width: 80.0,
                    height: 80.0,
                    child: Image.asset(
                      "assets/images/send_email.png",
                      fit: BoxFit.fitHeight,
                      height: 80,
                      width: 80,
                    ),
                  ),
                ),
                Text(
                  'Verify your email ID',
                  style: TextStyle(
                      color: Color(0xFF707070),
                      fontFamily: 'simply_rounded',
                      fontSize: 16.0),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(32.0, 8.0, 32.0, 16.0),
                  child: Text(
                    "Please enter 4 digit code send to\n" + email,
                    style: TextStyle(
                      color: const Color(0xFF707070),
                      fontSize: 14.0,
                      fontWeight: FontWeight.w500,
                      fontFamily: 'montserrat_medium',
                    ),
                    textAlign: TextAlign.center,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(32.0, 0, 32.0, 24.0),
                  child: Card(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(15.0),
                    ),
                    color: Colors.white,
                    elevation: 20,
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(32.0, 0, 32.0, 8.0),
                      child: OTPTextField(
                        margin: EdgeInsets.only(bottom: 10.0),
                        length: 4,
                        width: MediaQuery.of(context).size.width,
                        fieldWidth: 40,
                        style: TextStyle(
                          fontSize: 17,
                        ),
                        textFieldAlignment: MainAxisAlignment.spaceAround,
                        fieldStyle: FieldStyle.underline,
                        onCompleted: (pin) {
                          print("Completed: " + pin);
                          otpController.text = pin;
                        },
                        onChanged: (pin) {
                          print("Changed: " + pin);
                          otpController.text = pin;
                        },
                      ),
                    ),
                  ),
                ),
                Text(
                  "resend code",
                  style: TextStyle(
                    color: Color(0xFF1FC27F),
                    fontSize: 14.0,
                    fontFamily: 'montserrat_medium',
                    decoration: TextDecoration.underline,
                  ),
                  textAlign: TextAlign.center,
                ),
                InkWell(
                  onTap: () async {
                    if (otpController.text.length <= 3) {
                      Fluttertoast.showToast(
                        msg: "Invalid OTP!",
                        toastLength: Toast.LENGTH_LONG,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                      );
                    } else {
                      showLoaderDialog(context);
                      final String otp = otpController.text;
                      developer.log('verify otp' + 'values',
                          error: "otp " + otp + " id " + id.toString());
                      VerifyOtpModel verifyOtpModel =
                          await APIServices().verifyOtp(otp, id);
                      setState(() {
                        _verifyOtpModel = verifyOtpModel;
                        Navigator.pop(context);
                        Navigator.pop(context);
                      });
                      if (_verifyOtpModel.status == "success") {
                        String displayToast = "Verification successful !";
                        Fluttertoast.showToast(
                          msg: displayToast,
                          toastLength: Toast.LENGTH_LONG,
                          gravity: ToastGravity.BOTTOM,
                          timeInSecForIosWeb: 1,
                        );
                        buildShowSuccessOTPModalBottomSheet(context, size);
                      }
                      if (_verifyOtpModel.status == null) {
                        String failureToast = "Verification Failed !";
                        Fluttertoast.showToast(
                          msg: failureToast,
                          toastLength: Toast.LENGTH_LONG,
                          gravity: ToastGravity.BOTTOM,
                          timeInSecForIosWeb: 1,
                        );
                        buildShowFailureOTPModalBottomSheet(context, size);
                      }
                    }
                  },
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(0, 16.0, 0, 24.0),
                    child: Container(
                      height: 55.0,
                      width: size.width * 0.6,
                      decoration: kInputBoxDecorationGreenStyle,
                      child: Align(
                        alignment: Alignment.center,
                        child: Text(
                          "Verify and register".toUpperCase(),
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            color: Colors.white,
                            fontFamily: 'simply_rounded',
                            fontSize: 17,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          );
        });
  }
4

2 回答 2

3

尝试这个

   Future bottomSheet() async {
    return showModalBottomSheet(
      isDismissible: false,
      enableDrag: false,
      isScrollControlled: true,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(20.0)),
      ),
      context: context,
      builder: (context) {
        return WillPopScope(
          onWillPop: () async => false,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text('text 1'),
              Text('text 2'),
              Text('text 3'),
            ],
          ),
        );
      },
    );
  }
于 2021-06-02T12:26:22.723 回答
1

这有效

    showModalBottomSheet( 
     context: context,
     builder: (context) {
      return WillPopScope(
        onWillPop: () async => false,
        child: child,
      );
     },
    )

问题:Future(Dynamic) 不能分配给 Widget

 Scaffold(
   body: showModalBottomSheet(
    context,
  ),

解决方案

Scaffold(
  body: ElevatedButton(
    child: Text("Show"),
    onPressed: () => showModalBottomSheet(context),
  ),
)
于 2021-06-04T06:09:57.147 回答