how to change color of dropdown underline - flutter

in my code Container(height: 1, color: UtilColors.grey), not giving expeceted output
Container(
margin: EdgeInsets.only(left: 52, right: 48),
child: DropdownButton<String>(
isExpanded: true,
//Container(height: 1, color: UtilColors.grey),
value: _selectedUser,
items: _userTypes.map((String value) {
return new DropdownMenuItem<String>(value: value, child: new Text(value));
}).toList(),
/* decoration: InputDecoration(contentPadding: EdgeInsets.only(left: 15), suffixIcon: IconButton(onPressed: () {
// _userTypes.map((String value){return new DropdownMenuItem<String>(value: value, child: new Text(value));}).toList();
}, icon: Icon(null),)),*/
icon: Icon(Icons.keyboard_arrow_down),
hint: Text(UtilString.userType),
onChanged: (value) => setState(() => _selectedUser = value),
),
),

finally got the solution with DropdownButton -
underline: Container( height: 2,
color: Colors.deepPurpleAccent,),
this will change dropdown underline color.

Related

How to indent between DropdownButton and DropdownMenuItem in flutter

I am using DropdownButton and I am facing the following error. When opening the DropdownMenuItem list, I do not have an indent from the button itself. That is, I need to get the padding between the button (DropdownButton) and the dropdown list (DropdownMenuItem) so that there is a distance. But so far I haven't been able to do it. How can you make an indent between them?
code
#override
Widget build(BuildContext context) {
return SizedBox(
width: widget.width,
child: DropdownButtonHideUnderline(
child: DropdownButton2(
items: List.generate(
widget.items.length,
(index) => DropdownMenuItem<String>(
value: widget.items[index],
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.white.withOpacity(0.1),
width: 1,
),
),
),
child: StatefulBuilder(
builder: (context, setStateSB) => GFCheckboxListTile(
value: _selectedTitles.contains(widget.items[index]),
onChanged: (bool selected) {
_onItemSelect(selected, index);
setStateSB(() {});
},
selected: selected,
title: Text(
widget.items[index],
style: constants.Styles.smallTextStyleWhite,
),
padding: const EdgeInsets.only(top: 12, bottom: 13),
margin: const EdgeInsets.only(right: 0, left: 0),
size: 22,
activeBgColor: constants.Colors.greyCheckbox,
activeBorderColor: constants.Colors.greyXMiddle,
inactiveBgColor: constants.Colors.greyCheckbox,
activeIcon: SvgPicture.asset(constants.Assets.checkboxIcon),
inactiveBorderColor: constants.Colors.greyXMiddle,
type: type,
),
),
),
),
),
hint: _selectedTitles.length > 1
? const Text('Selecte EV',
style: constants.Styles.bigBookTextStyleWhite)
: Text(_selectedTitles.join().toString(),
style: constants.Styles.bigBookTextStyleWhite),
value: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value as String;
});
},
icon: SvgPicture.asset(constants.Assets.arrowDropdown),
iconSize: 21,
buttonHeight: 27,
itemHeight: 47,
dropdownMaxHeight: 185,
dropdownWidth: 140,
dropdownDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: constants.Colors.purpleMain,
),
color: constants.Colors.greyDark),
selectedItemBuilder: (context) {
return widget.items.map(
(item) {
return Row(
children: [
widget.icon ?? const SizedBox(),
const SizedBox(width: 8),
Text(
item,
style: constants.Styles.bigBookTextStyleWhite,
),
],
);
},
).toList();
},
),
),
);
}
}
In the dropdown_button2 documentation, there is a property for moving the dropdown menu Offset. You can see it here https://pub.dev/documentation/dropdown_button2/latest/dropdown_button2/DropdownButton2/offset.html
On that property you just need to set an Offset, which is composed of an X and Y values.
In your case it would look something like this:
DropdownButton2(
offset: Offset(0,10),
...
),

Remove default padding of DropDownButton in flutter?

I want to achieve a result like this where my dropdown button has very less padding.
This is what I currently have
so far I have tried adjusting height of dropDownButton, wrapping with a container, assigning padding but nothing works
here's my code
Container(
child: Center(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
dropdownColor: AppColors().white,
value: value,
style: TextStyles().medium12,
icon: Icon(
Icons.keyboard_arrow_down,
color: AppColors().green,
),
onChanged: (newValue) {
setState(() {
value = newValue!;
});
},
items: <String>[
AppStrings().getString().english_language,
AppStrings().getString().arabic_language,
AppStrings().getString().urdu_language,
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
value,
),
);
}).toList(),
),
),
),
),
padding: EdgeInsets.only(
left: 10.w,
right: 10.w,
),
decoration: BoxDecoration(
border: Border.all(color: AppColors().green, width: 6.w),
borderRadius: BorderRadius.all(
Radius.circular(25.r) // <--- border radius here
),
),
);
can someone please help me? thankyou so much
just set dropdownbutton isDense: true

Keep a value selected as default in dropdownmenu flutter

I have dropdown menu with main category also subcategory.in my Worker registration i am calling this dropdown with API,so if i open worker registration page i need to select Worker as default also change the subcategory dropdown based on the default value in this case worker,in main category there is only 4 items returns
1:Worker
2:Shop
3.rental equipment
4.contractor
These are the 4 main category items.so when i open worker registration page i need worker as default in dropdown.the problem is if i initialize the value as default the subcategory drop down doesn't work
Flexible(
flex: 0,
child: Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 15),
child: Container(
width: double.infinity,
height: 45,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.blueGrey)),
child: DropdownButton(
isExpanded: true,
itemHeight: 50,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40,
underline: SizedBox(),
hint: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Category",
style: TextStyle(fontSize: 15, color: Colors.black54),
),
),
value: _selectedmenu,
onChanged: (NewValues) {
_selectedmenu = NewValues;
setState(() {});
},
items: home_model.map((menu) {
return DropdownMenuItem(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: language != "ml"
? Text(menu.english)
: Text(menu.malayalam),
),
value: menu.id,
);
}).toList(),
),
),
),
),
Set the onChanged of the DropDownButton to:
onChanged: (value) {
setState(() {
_selectedmenu = value;
});
},
And initialize _selectedmenu with the id of the Worker item, e.g.
var _selectedmenu = 'worker';
Initialized the _selectedmenu with default worker id and removed the onchanged and DropdownMenuItem and added to Text with worker with condition because i have this localization integrated into it.By passing initialized _selectedmenu to the subcategory api and called the api in setState in the main class
String _selectedmenu = "5ef1b61930def32b3cbe7890";
Flexible(
flex: 0,
child: Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 15),
child: Container(
width: double.infinity,
height: 45,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
border: Border.all(color: Colors.blueGrey)),
child: DropdownButton(
isExpanded: true,
itemHeight: 50,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40,
underline: SizedBox(),
hint: Padding(
padding: const EdgeInsets.all(8.0),
child: language != "ml"
? Text(
"Worker",
style:
TextStyle(fontSize: 15, color: Colors.black),
)
: Text(
"ജോലിക്കാർ",
style:
TextStyle(fontSize: 15, color: Colors.black),
),
),
value: _selectedmenu,
),
),
),
),

How to add underline in items DropdownButton

How can I add the underline for DropdownButton items, I have tried underline like below but it didn't work
DropdownButton<String>(
isExpanded: true,
hint: Text("Status"),
value: value.selected,
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
items: items
.map((key, value) {
return MapEntry(
key,
DropdownMenuItem<String>(
value: key,
child: Text(value),
));
})
.values
.toList(),
onChanged: (String val) {
value.selected = val;
},
)
SizedBox(
width: 100.0,
child: DropdownButtonFormField<int>(
decoration: InputDecoration(
border: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.white))),
value: 2,
items: <DropdownMenuItem<int>>[
DropdownMenuItem<int>(
value: 1,
child: Text("Owner"),
),
DropdownMenuItem<int>(
value: 2,
child: Text("Member"),
),
],
),

Custom Dropdown Button and MenuItems Flutter

I am trying to build my own drop down button with separated and condensed menu items, like the image below:
here's the code I have tried so far, I got the drop down width to match the container but the items can't be customized so far and always the height starts from above the button and is not occupying the width of the container:
body: Container(
margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
child: Container(
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
border: Border.all(color: Colors.brown, width: 1.0)),
padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
isExpanded: true,
isDense: true,
value: selection,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.brown,
),
iconSize: 40,
underline: Container(
height: 1,
color: Colors.transparent,
),
onChanged: (String val) => setState(() => selection = val),
items: settingsOptions.map((option) {
return DropdownMenuItem(
value: option,
child: Text(option),
);
}).toList(),
),
),
)
),
),
This is the output from the code:
How do I customize the items width, height and add dividers like in the first image? Thanks
This is an example modify as you like !
DropdownButton(
isExpanded: true,
isDense: true,
value: selection,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.brown,
),
iconSize: 40,
underline: Container(
height: 1,
color: Colors.transparent,
),
onChanged: (String val) => setState(() => selection = val),
items: sampleList.map((option) {
return DropdownMenuItem(
value: option,
child: Container(
width:double.infinity,
alignment:Alignment.centerLeft,
padding: const EdgeInsets.fromLTRB(0,8.0,0,6.0),
child:Text(option),
decoration:BoxDecoration(
border:Border(top:BorderSide(color:Colors.grey,width:1))
)
),
);
}).toList(),
selectedItemBuilder:(con){
return sampleList.map((m){
return Text(m,);
}).toList();
}
)
I came across a flutter Library called dropdown_below in pub.dev. It has additional methods that can allow you customize dropdownMenuItem to your preferred layout.
DropdownBelow(
value: category,
// isDense: true,
itemWidth: MediaQuery.of(context).size.width * 0.92,
itemTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.black),
boxTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.grey.shade600),
boxPadding: EdgeInsets.fromLTRB(13, 12, 0, 12),
boxWidth: MediaQuery.of(context).size.width,
boxHeight: 60,
hint: Text('choose video'),
items: video.data.value.videos
.map((e) => DropdownMenuItem(
onTap: () => e.title,
value: e.title ?? category,
child: Container(
width: double.infinity,
decoration: BoxDecoration(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
e.title ?? '$category',
overflow: TextOverflow.ellipsis,
),
),
),
))
.toList(),
onChanged: (video) {
context.read(videoProvider).cateroryOnChange(video);
},
),
Library Link: https://pub.dev/packages/dropdown_below
The problem with the DropdownButton is that the menu will open randomly based on the selected index and other things. Also, You can't edit its code by just trying to pass offset as the logic code of the paint work based on that and trying to hardcode the selectedItemOffset to a value won't work perfectly fine.
So use DropDownButton2 , which is built just over this .
Package: DropDownButton2
Credits: Ahmed Elsayed
For reference: Refer this link