Horizontal Scroll on DropdownButtonFormField in Flutter - flutter

I'm working on a form in flutter with a DropdownButtonFormField. When the value of the dropdown menu is too long, it shows an overflow error, how can i make it scroll horizontally the way a normal textfield does?
Here is the code for the DropdownButtonFormField, Thanks in advance.
InputDecorator(
decoration: InputDecoration(
labelStyle: textStyle,
errorStyle: TextStyle(color: Colors.redAccent, fontSize: 16.0),
hintText: 'select expense',
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5.0))),
isEmpty: _currentSelectedValue == null,
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: _currentSelectedValue,
isDense: true,
onChanged: (String newValue) {
setState(() {
_currentSelectedValue = newValue;
state.didChange(newValue);
});
},
items: _currencies.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
),

Set the isExpanded property of DropdownButton to true.

Related

How to customize a DropDownButton

How to customize the appearance of a DropDownButton?
I would like to do a few things to make the drop down looks like a Text Entry:
remove the bottom line inside the yellow box
indent "Use Email" so it left aligns with "Email Address"
make the drop down the same size as the text box (default size)
put the down arrow close to the right hand side of the drop down
add borders
Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.yellow[600],
borderRadius: BorderRadius.circular(4),
),
child: DropdownButton(
value: DropDownValue,
icon: const Icon(Icons.keyboard_arrow_down),
items: DropDownItems.map((String DropDownItems) {
return DropdownMenuItem(
value: DropDownItems,
child: Text(DropDownItems),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
DropDownValue = newValue!;
if (DropDownValue == 'Use Email') {
RegisterType = 'Email';
} else {
RegisterType = 'Mobile';
}
});
},
),
),
SizedBox(height: 10),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: RegisterType),
controller: _EmailAddressController,
),
Try this code:
Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.yellow[600],
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(4),
),
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
value: DropDownValue,
icon: const Icon(Icons.keyboard_arrow_down),
items: DropDownItems.map((String DropDownItems) {
return DropdownMenuItem(
value: DropDownItems,
child: Text(DropDownItems),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
DropDownValue = newValue!;
if (DropDownValue == 'Use Email') {
RegisterType = 'Email';
} else {
RegisterType = 'Mobile';
}
});
},
),
),
),
),
SizedBox(height: 10),
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: RegisterType),
controller: _EmailAddressController,
),

how can i disable/enable TextFormField based on a selected value from DropDowButton in flutter?

i'm working on a flutter application and i have created a DropDowButton menu with 2 items (private chat and phone number) and a TextFormField phonenum.
when phone number is selected, i want to enable the TextFormField phonenum. otherwise it's always disable.
is there a way to do this?
thanks in advance!
here's my cod:
Widget menu(){
return DropdownButtonFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'select a way to contact with the buyer',
labelStyle: TextStyle(
fontSize: 16,
color: Colors.grey,
fontFamily: 'Almarai'
),
),
items: <String>['phone number', 'private chat'].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
);
}
Widget phonenum(){
return
TextFormField(
maxLength: 10,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'phone number',
labelStyle: TextStyle(
fontSize: 16,
color: Colors.grey,
fontFamily: 'Almarai'
),
),
);
}
The TextFormField widget has an enabled property which you can use:
TextFormField(
enabled: dropdownvalue == 'phone number',
// ...
);
Try the full test code on DartPad
Screenshots
Disabled Field
Enabled Field

how to hide TextFormField based on a value of Radio Button OR DropdownButtonFormField in Flutter

I'm new to Flutter and Dart language and I'm trying so hard to learn it by myself.
So, my question is I have a DropdownButtonFormField with 2 values (private chat and phone number) :
Widget menu(){
return DropdownButtonFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'how to contact with the buyer?',
labelStyle: TextStyle(
fontSize: 16,
color: Colors.grey,
fontFamily: 'Almarai'
),
),
items: <String>['phone number', 'private chat'].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
dropdownvalue = newValue;
});
},
);
}
And I have this TextFormField that I want it to be hidden if the (private chat) option is selected :
Widget phonenum(){
return
TextFormField(
maxLength: 10,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'phone number',
labelStyle: TextStyle(
fontSize: 16,
color: Colors.grey,
fontFamily: 'Almarai'
),
),
);
}
How can I achieve that please? And also, how can I achieve it using RadioButton rather than DropdownButtonFormField ? Because I still didn't decide which one is more suitable with my project flow.
You can set an if else statement in front of the textfield widget.
Here for example it only shows Text "Mr ABC" if wishOnePerson is true. You can set a bool, and switch its value with setState(). When true show when false hide.
Column(
children: <Widget>[
Text('Good Morning'), // Always visible
if (wishOnePerson) Text(' Mr ABC'), // Only visible if condition is true
],
)
Widget phonenum(){
if(dropdownvalue= 'phone number'){
return TextFormField(
maxLength: 10,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'phone number',
labelStyle: TextStyle(
fontSize: 16,
color: Colors.grey,
fontFamily: 'Almarai'
),
),
);
}else{
return Container();
}
}

How to set initial default value in drop down flutter

I've one drop down and there some value inside the drop-down button and I need to by default selected value. you can seel below piece of snippet where you can find the drop-down value. I need it always there is by default selected value Normal. hope you understand the question.
FormBuilder(
autovalidate: autovalidate,
child: FormBuilderCustomField(
attribute: "Select Address",
validators: [
FormBuilderValidators.required(),
],
formField: FormField(
builder: (FormFieldState<dynamic> field) {
return InputDecorator(
decoration: InputDecoration(
errorText: field.errorText,
filled: false,
isDense: true,
border: InputBorder.none,
icon: Container(
width: 50.0,
height: 50.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20.0),
color: colorStyles['primary_light'],
),
child: Icon(
Icons.business_center,
color: colorStyles['primary'],
),
),
),
isEmpty: _typeValue == [],
child: new DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text("Service Type"),
isExpanded: true,
items: [
"normal",
"urgent",
"emergency",
].map((option) {
return DropdownMenuItem(
child: Text("$option"),
value: option,
);
}).toList(),
value: field.value,
onChanged: (value) {
field.didChange(value);
_serviceType = value;
},
),
),
);
},
)),
);
Just Asign on initState()
selectedDropDownValue = "normal";
In DropDown, asign selectedDropDownValue to value, and update on onChanged callback
new DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text("Service Type"),
isExpanded: true,
items: [
"normal",
"urgent",
"emergency",
].map((option) {
return DropdownMenuItem(
child: Text("$option"),
value: option,
);
}).toList(),
value: selectedDropDownValue, //asign the selected value
onChanged: (value) {
setState((){
selectedDropDownValue = value; //on selection, selectedDropDownValue i sUpdated
});
},
),
),
);

Flutter DropdownButton show label when option is selected

Can a Dropdown Button:
return DropdownButton<String>(
items: <String>['Foo', 'Bar'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {},
);
have something similar to a decoration user in a TextFormField:
TextFormField(
controller: _titleController,
decoration: InputDecoration(labelText: 'Input'),
validator: (String value) {
if (value != null && value.isEmpty) {
return 'Please enter some text';
}
},
style: Theme.of(context).textTheme.title,
),
When something is written in the above TextFormField the word Input shows. Like this:
Replace DropdownButton with DropdownButtonFormField:
https://api.flutter.dev/flutter/material/DropdownButtonFormField-class.html
Change DropdownButton to DropdownButtonFormField and add this decoration....
decoration: InputDecoration(
filled: true,
fillColor: Hexcolor('#ecedec'),
labelText: 'Occupation',
border: new CustomBorderTextFieldSkin().getSkin(),
),
Copy Paste and see magic
I have done flutter dropdown with material design
Padding(
padding: const EdgeInsets.all(9.0),
child: InputDecorator(
decoration: InputDecoration(
labelText: 'Priority',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0)),
contentPadding: EdgeInsets.all(10),
),
child: ButtonTheme(
materialTapTargetSize: MaterialTapTargetSize.padded,
child: DropdownButton<String>(
hint: const Text("Priority"),
isExpanded: true,
value: dropdownValue,
elevation: 16,
underline: DropdownButtonHideUnderline(
child: Container(),
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
),
),