为什么在Flutter中未填充文本字段时不显示snackbar?

vojdkbi0  于 2023-01-10  发布在  Flutter
关注(0)|答案(1)|浏览(120)

我测试并尝试留下一些文本字段未填充。但他们没有显示任何类型的错误,特别是从snackbar。似乎不能找出问题。我尝试改变它了一点,并关闭snackbar文档以及(https://pub.dev/packages/awesome_snackbar_content/install)。但仍然不能找出问题。我希望得到任何帮助或提示。以下是代码片段:

class _SignUpPageState extends State<SignUpPage> {


// Focus nodes for email, password, and retyping passwords
  final FocusNode emailFocus = FocusNode();
  final FocusNode passwordFocus = FocusNode();
  final FocusNode retypePasswordFocus = FocusNode();

  // Bool values for obscuring text
  bool _obscureTextPassword = true;
  bool _obscureTextConfirmPassword = true;


// Manages the state of the form
  final _formKey = G

lobalKey<FormState>();

  // Refers to the name of the TextEditingController class
  TextEditingController _emailController = new TextEditingController();
  TextEditingController _passwordController = new TextEditingController();
  TextEditingController _retypePasswordController = new TextEditingController();


@override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    emailFocus.dispose();
    passwordFocus.dispose();
    retypePasswordFocus.dispose();
    super.dispose();
  }

  // Initializing these TextEditingController objects
  @override
  void initState() {
    super.initState();
    _emailController = TextEditingController();
    _passwordController = TextEditingController();
    _retypePasswordController = TextEditingController();
  }

  void _toggleSignUp() {
    setState(() {
      _obscureTextPassword = !_obscureTextPassword;
    });
  }

  void _toggleSignUpConfirm() {
    setState(() {
      _obscureTextConfirmPassword = !_obscureTextConfirmPassword;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Form(
        key: _formKey,
        child: Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [
                    Color.fromRGBO(73, 77, 235, 1.0),
                    Color.fromRGBO(75, 0, 130, 0.8)
                  ]),
        ),
        child: Padding(
          padding: const EdgeInsets.all(10.0),
          child: ListView(
            children: [
              SizedBox(height: 60,),
              Center(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text("Sign Up",
                    style: TextStyle(fontSize: 20, decoration: TextDecoration.none, color: Colors.white),),
                ),
              ),
              SizedBox(height: 40,),

              Column(
                children: [

                  Container(
                    height: 45,
                    width: 350,

                    padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
                    child: TextFormField(
                      keyboardType: TextInputType.emailAddress,
                      focusNode: emailFocus,
                      autofocus: true,
                      textInputAction: TextInputAction.next,
                      decoration: InputDecoration(
                        hintText: "Enter Your Email Address",
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        contentPadding: EdgeInsets.all(30),
                      ),
                      controller: _emailController,
                      validator: (String? value) {
                        if (value == null || value.isEmpty) {
                          return "Please enter some text";
                        }
                        return null;
                      },
                    ),
                  ),

                  SizedBox(height: 40,),

                  Container(
                    height: 45,
                    width: 350,

                    padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
                    child: TextFormField(
                      keyboardType: TextInputType.visiblePassword,
                      focusNode: passwordFocus,
                      obscureText: _obscureTextPassword,
                      textInputAction: TextInputAction.next,
                      decoration: InputDecoration(
                        hintText: "Password",
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        contentPadding: EdgeInsets.all(30),
                      ),
                      controller: _passwordController,
                    ),
                  ),

                  SizedBox(height: 40,),

                  Container(
                    height: 45,
                    width: 350,

                    padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
                    child: TextFormField(
                      keyboardType: TextInputType.visiblePassword,
                      obscureText: _obscureTextConfirmPassword,
                      textInputAction: TextInputAction.done,
                      decoration: InputDecoration(
                        hintText: "Retype Password",
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        contentPadding: EdgeInsets.all(30),
                      ),
                      controller: _retypePasswordController,
                    ),
                  ),

                ],
              ),

              SizedBox(
                height: 60,
              ),

              ElevatedButton(
                style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.black,
                    padding:
                    const EdgeInsets.symmetric(horizontal: 110, vertical: 25),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(20))),
                child: const Text(
                  'Next',
                  style: TextStyle(fontSize: 12),
                ),
                onPressed: () {

                  if (_formKey.currentState!.validate()) {

                    final snackBar = SnackBar(
                      /// need to set following properties for best effect of awesome_snackbar_content
                      elevation: 5,
                      behavior: SnackBarBehavior.floating,
                      backgroundColor: Colors.transparent,
                      content: AwesomeSnackbarContent(
                        title: 'On Snap!',
                        message:
                        'This is an example error message that will be shown in the body of snackbar!',

                        /// change contentType to ContentType.success, ContentType.warning or ContentType.help for variants
                        contentType: ContentType.failure,
                      ),
                    );

                  } else {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const TabNav()),
                    );
                  }

                },
              ),
            ],
          ),
        ),
    ),
  ),
);

 }

}
zrfyljdw

zrfyljdw1#

问题是你正在创建SnackBar但是你没有调用它,正如你在库的例子中看到的,你错过了下面的调用:

ScaffoldMessenger.of(context)
 ..hideCurrentSnackBar()
 ..showSnackBar(snackBar);

相关问题