始终跳过验证表单

时间:2020-07-21 20:30:44

标签: flutter dart

我正在尝试验证表单,但是它总是跳过它,并且在调用期望表单中的值的方法时应用崩溃。

我尝试了不同的方法,但是总是以崩溃告终,这可能与return Alert有关吗?​​

代码在这里:

Widget build(BuildContext context) {
    return Scaffold(
      drawer: NavDrawer(),
      appBar: AppBar(title: Text("fridgy"),
      ),
      floatingActionButton: FloatingActionButton(
      backgroundColor: Colors.blueGrey[200],
      onPressed: () {
        return Alert(context: context, 
                     title: "Add Food",
                      desc: "Please insert name and category",
                     content: Form(
                       key: _formkey,
                       child: Column(
                         //key: _formkey,
                        children: <Widget>[
                          Form(
                          child: TextFormField(
                            decoration: InputDecoration(labelText: "Food name"),
                            validator: (value){
                              if (value.isEmpty){
                                return 'empty';
                              }
                              return null;
                            },
                             onChanged: (String value){
                              foodName = value;
                            }
                          ),
                          ),
                          new DropdownButton<String>(
                            isDense: true,
                            items: <String>['Meat', 'Veggies', 'Dairy', 'Fruit', 'Miscellaneous'].map((String value) {
                              return new DropdownMenuItem<String>(
                                value: value,
                                child: new Text(value),
                              );
                            }).toList(),
                            onChanged: (String value) { foodCat = value;},
                          )
                        ],   
                       ),
                     ),
                     buttons: [
                       DialogButton(child: Text("Add to fridge"), 
                       color: Colors.blueGrey[200],
                       onPressed: () {
                         if(!_formkey.currentState.validate()){
                          print("AAA");
                         } else{
                           return createFood();                        
                         }
                           Navigator.pop(context);

1 个答案:

答案 0 :(得分:0)

您可以在下面复制粘贴运行完整代码
步骤1:使用Alert().show()而不是return Alert()
步骤2:删除重复的Form()
步骤3:DropdownButton需要value

DropdownButton<String>(
   value: foodCat, 

第4步:使用StatefulBuilder

StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
        return Form(

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _formkey = GlobalKey<FormState>();
  String foodName = "";
  String foodCat = null;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'You have pushed the button this many times:',
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
            backgroundColor: Colors.blueGrey[200],
            onPressed: () {
              Alert(
                  context: context,
                  title: "Add Food",
                  desc: "Please insert name and category",
                  content: StatefulBuilder(
                      builder: (BuildContext context, StateSetter setState) {
                    return Form(
                      key: _formkey,
                      child: Column(
                        children: <Widget>[
                          TextFormField(
                              decoration:
                                  InputDecoration(labelText: "Food name"),
                              validator: (value) {
                                if (value.isEmpty) {
                                  return 'empty';
                                }
                                return null;
                              },
                              onChanged: (String value) {
                                foodName = value;
                              }),
                          DropdownButton<String>(
                            value: foodCat,
                            isDense: true,
                            items: <String>[
                              'Meat',
                              'Veggies',
                              'Dairy',
                              'Fruit',
                              'Miscellaneous'
                            ].map((String value) {
                              return DropdownMenuItem<String>(
                                value: value,
                                child: Text(value),
                              );
                            }).toList(),
                            onChanged: (String value) {
                              foodCat = value;
                              setState(() {});
                            },
                          )
                        ],
                      ),
                    );
                  }),
                  buttons: [
                    DialogButton(
                        child: Text("Add to fridge"),
                        color: Colors.blueGrey[200],
                        onPressed: () {
                          if (!_formkey.currentState.validate()) {
                            print("AAA");
                          } else {
                            //return createFood();
                          }
                          Navigator.pop(context);
                        })
                  ]).show();
            }));
  }
}
相关问题