flutter remove DropdownButton left padding - flutter

I've struggled to remove flutter's DropdownButton left padding so it can match the TextField below. Anyone can help?
Here's my code:
DropdownButton<MenuModel>(
itemHeight: itemHeight,
isExpanded: true,
underline: null,
value: currentValue,
hint: Text(placeholder, style: hintStyle),
style: textStyle,
disabledHint: Text(currentValue?.label ?? placeholder, style: textStyle),
onChanged: widget.modalMode ? null : _onChanged,
items: widget.listMenu.map<DropdownMenuItem<MenuModel>>((MenuModel val) {
return DropdownMenuItem<MenuModel>(
child: Text(val.label),
value: val,
);
}).toList(),
)

finally I've found the problem. So it happen because I wrapped the DropdownButton with ButtonTheme(alignedDropdown: true)
ButtonTheme(
alignedDropdown: true, // it's the culprit
child: DropdownButton<MenuModel>(
itemHeight: itemHeight,
isExpanded: true,
underline: null,
value: currentValue,
hint: Text(placeholder, style: hintStyle),
style: textStyle,
disabledHint: Text(currentValue?.label ?? placeholder, style: textStyle),
onChanged: widget.modalMode ? null : _onChanged,
items: widget.listMenu.map<DropdownMenuItem<MenuModel>>((MenuModel val) {
return DropdownMenuItem<MenuModel>(
child: Text(val.label),
value: val,
);
}).toList(),
),
)
Thanks for the help.

Try below code hope its helpful to you. refer Dropdown here. just change my widget to your widgets
Create variable and List of dropdown:
var selectedCategory;
List categoryDropDownList = [
'One',
'Two',
'Three',
'Four',
];
Your Widget:
DropdownButton(
isDense: true,
isExpanded: true,
hint: Text(
'Select Category',
style: TextStyle(
color: Colors.black,
fontSize: 15,
),
textAlign: TextAlign.center,
),
value: selectedCategory,
items: categoryDropDownList.map<DropdownMenuItem<String>>(
(value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
value.toString(),
style: TextStyle(
fontSize: 17,
),
),
);
},
).toList(),
onChanged: (var value) {
setState(
() {
selectedCategory = value;
},
);
},
),
Your result Screen->

I have tried to use the same style as you are using and managed to get that done with the following code. I hope that works for you. I am attaching the screenshot as well of the output.
Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
DropdownButton<String>(
itemHeight: 50,
isExpanded: true,
underline: null,
value: dropdownValue,
hint: const Text('Honorific', style: TextStyle(color: Colors.grey)),
icon: const Icon(Icons.arrow_drop_down),
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['Honorific']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
TextFormField(
controller: controller,
validator: (value) {
if (value!.isEmpty || !value.contains('#')) {
return 'Please enter a valid email address';
}
return null;
},
textInputAction: TextInputAction.next,
keyboardType: TextInputType.emailAddress,
),
],
),
),
output

Related

Flutter post to API from Textfield and DropdownButton

I have the following form to post data to api. the form has textfields and dropdownbuttons as follows. How can I map to the API and post to api?
Container(
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
child: Form(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
TextFormField(
decoration: InputDecoration(hintText: 'Job Title'),
controller: titleController,
onChanged: (val) {},
),
SizedBox(height: 20.0),
TextFormField(
decoration: InputDecoration(hintText: 'Company'),
controller: companyController,
onChanged: (val) {},
),
DropdownButton(
value: currentValue,
icon: const Icon(Icons.keyboard_arrow_down),
items: <String>[
"1",
"2",
"3",
].map((String i) {
return DropdownMenuItem(
value: i,
child: Text(i),
);
}).toList(),
onChanged: onChangedCallback,
),
SizedBox(height: 20.0),
DropdownSearch<String>(
mode: Mode.MENU,
showSearchBox: false,
showClearButton: true,
items: ["Aviation", "Arts", "Business"],
dropdownSearchDecoration: InputDecoration(
labelText: "Category",
hintText: "Select Job Category",
),
onChanged: print,
),
SizedBox(height: 20.0),
TextFormField(
decoration: InputDecoration(hintText: 'Location'),
onChanged: (val) {},
),
SizedBox(height: 20.0),
TextFormField(
maxLines: 3,
decoration: InputDecoration(hintText: 'Description'),
onChanged: (val) {},
),
SizedBox(height: 20.0),
TextFormField(
decoration: InputDecoration(hintText: 'Salary'),
onChanged: (val) {},
),
SizedBox(height: 30.0),
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Color.fromARGB(255, 252, 114, 2)),
onPressed: () {
},
child: Text(
"Add",
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
)),
],
),
),
),
),
I want the textfields to get data and post to api while dropdownbuttons have an item and post the item with the corresponding item name on the api? (Like from the following dropdown buttton DropdownButton( value: currentValue, icon: const Icon(Icons.keyboard_arrow_down), items: <String>[ "1", "2", "3", ].map((String i) { return DropdownMenuItem( value: i, child: Text(i), ); }).toList(), onChanged: onChangedCallback, ),
if I select 1, it will post 1 to the api and the same for category. How can this be implmented? )
First of all you need to use an HTTP client package like Dio or tttp
Then you will need to make an api call using this package whenever you click the button.
So an example using Dio package would be like that
import 'package:dio/dio.dart';
void postHttp() async {
try {
var response = await Dio().post('your-api-link',data: {'title': "job title field", 'currentValue': 'current value field'});
print(response);
} catch (e) {
print(e);
}
}
And you will find more info in the package documentation.
Quick hint:
try to test your api using postman first to make sure it works correctly

how to use dropdownbutton with same value?

There are few same values in the dropdown button , when i tap on that it show the error ,is there any way to use the use the dropdown with same values .I have tried using the
value :
DropdownButtonHideUnderline(
child: DropdownButton2(
iconEnabledColor: primaryColor,
selectedItemHighlightColor: primaryColor,
hint: Text(
'User type',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: _user_type
.map((item) => DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
))
.toList(),
value: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value as String;
});
},
buttonHeight: 40,
// buttonWidth: doubl,
itemHeight: 40,
),
still getting the error
Value of every DropdownMenuItem should be unique. In order to make use of list which have repetitive values, you should have a unique identifier for each.
You can create a model:
class Model {
int id;
String value;
Model(this.id, this.value);
}
You can create list with repetitive values:
List<Model> list = [
Model(0, "a"),
Model(1, "a"),
Model(2, "b"),
Model(3, "b"),
Model(4, "c"),
Model(5, "c"),
];
…and you can use the list like this in your DropdownButton:
DropdownButton(
value: _selectedValue,
items: list
.map((value) => DropdownMenuItem(
value: value.id, child: Text(value.value)))
.toList(),
onChanged: (value) {
_selectedValue = value as int;
setState(() {});
},
)
Why don't you try adding a .toSet() before the .map().
The .toSet() should filter your list to unique values.
DropdownButtonHideUnderline(
child: DropdownButton2(
iconEnabledColor: primaryColor,
selectedItemHighlightColor: primaryColor,
hint: Text(
'User type',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: _user_type
.toSet()
.map((item) => DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
))
.toList(),
value: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value as String;
});
},
buttonHeight: 40,
// buttonWidth: doubl,
itemHeight: 40,
),
Try below code, I have used dropdown_button2 package
Declare your variables and dropdown data
String selectedValue;
List<String> items = [
'User 1',
'User 2',
'User 3',
'User 4',
'User 5',
'User 6',
];
Your Widget:
DropdownButtonHideUnderline(
child: DropdownButton2(
isExpanded: true,
hint: Text(
'User type',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: items
.map((item) => DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
color: Colors.black,
),
overflow: TextOverflow.ellipsis,
),
))
.toList(),
value: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value as String;
});
},
buttonHeight: 50,
buttonWidth: 160,
buttonPadding: EdgeInsets.all(8),
buttonDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(14),
border: Border.all(
color: Colors.black26,
),
),
itemHeight: 40,
),
),
Your Dropdown button result->
Your Dropdown data result->
You can refer my answer here, here, here also for same using dropdown_below package

Dropdown button not expanding

I'm trying to build a form with a DropdownButtonFormField with 4 items but i don't know why it doesn't want to expand.
Added isDense : true but it wasn't because of it being too small
Here's the button code
new Container(
child: DropdownButtonFormField(
isDense: false,
hint: Text('Ecole'),
onSaved: (value) {
this._data.ecole = value.toString();
},
items: ['HEI', 'ISEN', 'ISA', 'all'].map((String value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList()),
),
1
Try this
String dropdownValue = 'HEI';
DropdownButton<String>(
value: dropdownValue,
icon: const Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: const TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>['HEI', 'ISEN', 'ISA', 'all']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),

RadioListTile inside Listview.builder index in flutter

I want to set the index of RadioListTile inside the listview.builder but i can't do that how to do it which I want to get the value of this index here is my code
int _value=0;
List valueList=[];
ListView.Builder(itemBuilder:(context,index){
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: RadioListTile<int>(
dense: true,
activeColor: colorsCheck(),
contentPadding:
EdgeInsets.symmetric(horizontal: 1.0),
title: Text(
'Present',
style: TextStyle(
color: Colors.white, fontSize: 12.0),
),
value: 1,
groupValue: _value,
onChanged: (value) {
setState(() {
_value=value;
valueList.add(value);
print('radio button value present ${valueList.toList().toString()}');
});
},
),
),
Expanded(
child: RadioListTile<int>(
contentPadding:
EdgeInsets.symmetric(horizontal: 1.0),
dense: true,
activeColor: colorsCheck(),
title: Text(
'Absent',
style: TextStyle(
color: Colors.white, fontSize: 12.0),
),
value: 2,
groupValue: _value,
onChanged: (value) {
setState(() {
_value=value;
valueList.add(value);
print('radio button value absent ${valueList.toList().toString()}');
});
},
),
),
Expanded(
child: RadioListTile<int>(
dense: true,
activeColor: colorsCheck(),
contentPadding:
EdgeInsets.symmetric(horizontal: 1.0),
title: Text(
'Leave',
style: TextStyle(
color: Colors.white, fontSize: 12.0),
),
value: 3,
groupValue:_value,
onChanged: (value) {
setState(() {
_value=value;
valueList.add(value);
print('radio button value leave ${valueList.toList().toString()}');
});
},
),
),
],
),
}
Row is inside card where 3 radio button is set (present, absent, leave) but when I click it changes all item builder value. I just want to change the value of that radio inside the card which it clicks. other not. here is an image ...
look inside the picture where the leave radio button is set only top value but it set all.
set index in radiotilelist inside listview.builder
currentStatus = List<int>.filled(data.length, initialValue);
groupValue: currentStatus[index];
currentStatus[index] = newStatus

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(),
),
),
),
),