提示不适用于Flutter中的DropdownButtonFormField

时间:2019-08-09 13:02:46

标签: flutter drop-down-menu dart dropdown

我在 Flutter 中有一个带有textformfield和dropdownbuttonformfield的表单。在运行我的应用程序提示对我不起作用。 提示未显示在dropdownbuttonform字段中。它显示 kkk 作为初始值,而不显示选择城市

我在这里使用StatefulWidget。

帮助我解决此问题。 预先感谢。

class _AddTripState extends State<AddTrip> {
  var formKey = GlobalKey<FormState>();
  TextEditingController nameController = TextEditingController();  
  TextEditingController numberController = TextEditingController();
  TextEditingController datecontroller = TextEditingController();
  final format = DateFormat("yyyy-MM-dd");
  DateTime _dateTime;
  List<String> name = ['kkk', 'rrr'];
  String _dropdownvalue = 'kkk';

@override
Widget build(BuildContext context) {
  return Scaffold(
  appBar: AppBar(
    title: Text("Add Trip"),
  ),
  body: Form(
      key: formKey,
      child: ListView(
        children: <Widget>[
          Text(
            'Name',
            textAlign: TextAlign.left,
          ),
          TextFormField(
            decoration: _inputDecoration(),
            keyboardType: TextInputType.text,
            controller: nameController,
            validator: textValidator,
          ),
          Text(
            'Number',
            textAlign: TextAlign.left,
          ),
          TextFormField(
            decoration: _inputDecoration(),
            controller: numberController,
            keyboardType: TextInputType.number,
            validator: numberValidator,
          ),
          Text(
            'Date',
            textAlign: TextAlign.left,
          ),
          TextFormField(
            readOnly: true,
            controller: datecontroller,
            validator: dateValidator,
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              errorBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.amber)),
              errorStyle: TextStyle(color: Colors.amber),
              suffixIcon: GestureDetector(
                child: Icon(
                  Icons.date_range,
                ),
                onTap: () {
                  showDatePicker(
                          context: context,
                          initialDate: DateTime.now(),
                          firstDate: DateTime(2018),
                          lastDate: DateTime(2020))
                      .then((value) {
                    setState(() {
                      datecontroller.text =
                          DateFormat("yyyy-MM-dd").format(value);
                    });
                  });
                },
              ),
            ),
          ),
          DropdownButtonFormField<String>(
            hint: Text('Select City'),
            validator: _cityValidator,
            decoration: InputDecoration(border: OutlineInputBorder()),
            items: name.map((value) {
              return DropdownMenuItem<String>(
                  child: Text(value), value: value);
            }).toList(),
            value: _dropdownvalue,
            onChanged: (newValue) {
              setState(() {
                _dropdownvalue = newValue;
              });
            },
          ),
          RaisedButton(
            onPressed: submit,
            child: Text('Add Trip'),
          )
        ],
      )),
);
}

我错了吗?

2 个答案:

答案 0 :(得分:1)

如果value为空,则显示提示。

因此在您的代码中-将-String _dropdownvalue = 'kkk';更改为-仅String _dropdownvalue;

答案 1 :(得分:0)

为此您需要使用 InputDecoration:

 decoration: InputDecoration(labelText:'Select City'),