flutter Snackbar未显示并将用户推到下一页

bfnvny8b  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(98)

我为用户添加了一个snackbar,当所有字段都没有填写或注册表单部分填写时,它会显示出来。它不会显示snackbar,而是将用户带到下一个屏幕。我想知道它是如何做到这一点并通过if/else语句块的。如果有人能给予提示或展示我做错了什么,我将不胜感激。

final emailRegistered = signupEmailController.text.trim();
final usernameRegistered = signupNameController.text.trim();
final passwordRegistered = signupPasswordController.text.trim();
final retypePassRegistered = signupConfirmPasswordController.text.trim();

// final user = ParseUser.createUser()

// If form is valid then move forward
if (_formKey.currentState!.validate()) {

    final user = ParseUser.createUser(usernameRegistered, passwordRegistered, emailRegistered);

    Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => const InterestsPage()),
    );


  // ScaffoldMessenger.of(context).showSnackBar(
  //   const SnackBar(content: Text('Processing Data')),);

} else {
  // ScaffoldMessenger.of(context).showSnackBar(
  //   const SnackBar(content: Text('Field is unfilled', style: TextStyle(fontSize: 12),)),);

  if (usernameRegistered.isEmpty == true &&
      passwordRegistered.isEmpty == true &&
      retypePassRegistered.isEmpty == true && emailRegistered.isEmpty == true) {

    final materialBanner = MaterialBanner(
      /// need to set following properties for best effect of awesome_snackbar_content
      elevation: 0,
      backgroundColor: Colors.transparent,
      forceActionsBelow: true,
      content: AwesomeSnackbarContent(
        title: 'All fields are empty',
        message:
        'This is an example error message that will be shown in the body of materialBanner!',

        /// change contentType to ContentType.success, ContentType.warning or ContentType.help for variants
        contentType: ContentType.success,
        // to configure for material banner
        inMaterialBanner: true,
      ),
      actions: const [SizedBox.shrink()],
    );

    ScaffoldMessenger.of(context)
      ..hideCurrentMaterialBanner()
      ..showMaterialBanner(materialBanner);

  } else if (emailRegistered.isEmpty == true) {

    final materialBanner = MaterialBanner(
      /// need to set following properties for best effect of awesome_snackbar_content
      elevation: 0,
      backgroundColor: Colors.transparent,
      forceActionsBelow: true,
      content: AwesomeSnackbarContent(
        title: 'Email is empty!',
        message:
        'This is an example error message that will be shown in the body of materialBanner!',

        /// change contentType to ContentType.success, ContentType.warning or ContentType.help for variants
        contentType: ContentType.success,
        // to configure for material banner
        inMaterialBanner: true,
      ),
      actions: const [SizedBox.shrink()],
    );

    ScaffoldMessenger.of(context)
      ..hideCurrentMaterialBanner()
      ..showMaterialBanner(materialBanner);

  } else if (passwordRegistered.isEmpty == true) {

    const SnackBar(content: Text('Password is unfilled.'));

  } else if (passwordRegistered != retypePassRegistered) {

    const SnackBar(content: Text('Passwords do not match.'));

  } else if (usernameRegistered.isEmpty == true) {

    const SnackBar(content: Text('Username is unfilled'));

  }
}
knpiaxh1

knpiaxh11#

您忘记调用ScaffoldMessenger. of(context). showSnackBar方法来创建一个小吃店变量,然后在显示小吃店之后。
例如:

onPressed:(){
    var snackBar;
     if(){
          //your other stuff 
     } else if (passwordRegistered.isEmpty == true) {

    snackBar = SnackBar(content: Text('Password is unfilled.'));

  } else if (passwordRegistered != retypePassRegistered) {

    snackBar = SnackBar(content: Text('Passwords do not match.'));

  } else if (usernameRegistered.isEmpty == true) {

    snackBar = SnackBar(content: Text('Username is unfilled'));

  }

ScaffoldMessenger.of(context).showSnackBar(snackBar);//important point
}//completion on pressed

相关问题