【问题标题】:how to add a textfield value based on dropdown list in flutter如何根据flutter中的下拉列表添加文本字段值
【发布时间】:2020-04-17 09:57:24
【问题描述】:

我还是 Flutter 的新手。在这里我想从下拉列表中选择值到类别表单字段。但是在尝试定义时出现错误

child: DropdownButtonHideUnderline 

在Textformfield里面。我试图找到一个解决方案,但我找不到它,我无法自己编程。我希望你能帮助我。还有其他方法可以存档吗?

我的代码在这里,提前感谢您的指导。

class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

 final AuthService _auth = AuthService();
 final _formKey = GlobalKey<FormState>();
 String error = '';
 bool loading = false;
 String name = '';
 String nickname = '';
 String city = '';


 @override
 Widget build(BuildContext context) {
   return Container(
    child: Scaffold(
    backgroundColor: Colors.brown[50],
    appBar: AppBar(
      title: Text('Brew Crew'),
      backgroundColor: Colors.brown[400],
      elevation: 0.0,
      actions: <Widget>[
        FlatButton.icon(
          icon: Icon(Icons.person),
          label: Text('logout'),
          onPressed: () async {
            await _auth.signOut();
          },
        ),
      ],
    ),
    body: Container(
      padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
      child: Form(
        key: _formKey,
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              SizedBox(height: 20.0),
              TextFormField(
                decoration: textInputDecoration.copyWith(hintText: 'Name'),
                validator: (val) => val.isEmpty ? 'Enter your name' : null,
                onChanged: (val) {
                  setState(() => name = val);
                },
              ),
              SizedBox(height: 20.0),
              TextFormField(
                decoration: textInputDecoration.copyWith(hintText: 'NickName'),
            onChanged: (val) {
              setState(() => nickname = val);
            },
              ),
              SizedBox(height: 20.0),
              TextFormField(
                decoration: textInputDecoration.copyWith(hintText: 'City'),
                validator: (val) => val.isEmpty ? 'Enter your city' : null,
                onChanged: (val) {
                  setState(() => city = val);
                },
              ),
              SizedBox(height: 20.0),
              TextFormField(
                decoration: textInputDecoration.copyWith(hintText: 'Category'),
                validator: (val) => val.isEmpty ? 'Please select a category' : null,
                onChanged: (val) {
                  setState(() => nickname = val);
                },
              ),
              SizedBox(height: 20.0),
              RaisedButton(
                  color: Colors.pink[400],
                  child: Text(
                    'Submit',
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () async {

                  }
              ),
              SizedBox(height: 12.0),
              Text(
                error,
                style: TextStyle(color: Colors.red, fontSize: 14.0),
              )
            ],
          ),
        ),
      ),
    ),
  ),
);
}
}

【问题讨论】:

    标签: forms flutter dropdown


    【解决方案1】:

    这里的问题是你不需要TextFormField,你需要一个DropdownButton Widget。

                      DropdownButton(
                        items: <DropdownMenuItem>[
                          DropdownMenuItem(
                            child: Text("Category I"),
                          ),
                          DropdownMenuItem(
                            child: Text("Category II"),
                          ),
                        ],
                        onChanged: (value) {
    
                        },
                      ),
    

    我为此为您创建了一个代码笔: https://codepen.io/md-weber/pen/zYvqaGv

    【讨论】:

      猜你喜欢
      • 2015-08-14
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多