I have a form in Flutter with textformfield and dropdownbuttonformfield. While running my app hint is not working for me. hint not shown in dropdownbuttonformfield. It's showing kkk as initial value not showing Select City.
I'm using StatefulWidget here.
help me to solve this Problem. Thanks in Advance.
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'),
)
],
)),
);
}
Where i'm wrong?
The hint is Displayed if the value
is null.
so in your code - make - String _dropdownvalue = 'kkk';
change to - String _dropdownvalue;
only
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With