Open DropDown below the selected value flutter - flutter

I need to to show dropdown below the selected value . How can we set Dropdown vertical offset in flutter like we do in android. is there a simple way to it without creating custom dropdown? image is attached and
Here is my drop down code:
Container(
width: percentWidth(77, context),
padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
child: DropdownButton<String>(
isExpanded: true,
value: state.selectedSubject,
underline: Container(
height: 1.0,
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.transparent,
width: 0.0))),
),
icon: Icon(Icons.arrow_drop_down),
iconSize: 0,
elevation: 16,
style: TextStyle(color: Colors.black, fontSize: 15),
onChanged: (String data) {
_userFeedbackBloc
.add(UserFeedbackEvent.onDropDownChanged(data));
},
items: spinnerItems.map<DropdownMenuItem<String>>(
(String newValue) {
return DropdownMenuItem<String>(
value: newValue,
child: Text(newValue),
);
}).toList(),
),
),

Ah, here use offset my friend.
offset: Offset(0, 100),
put that good Offset inside PopupMenuButton

You can use CustomDropDown
https://github.com/DhavalRKansara/CustomDropDown/blob/main/custom_drop_down.dart
Copy code from above line and past In dart file
Then you can use it like below
CustomDropdownButton(
value: _selectedCompany,
items: _dropdownMenuItems,
onChanged: onChangeDropdownItem,
),

Related

Hide the bottom line and change the style of the selected element Drowdown_button2

I need your help. I'm using dropdown_button2 from pub.dev but I've run into a few issues that I can't resolve. First mistake: how to change the text style of the selected element? I need the text that displays the selected item to be larger. The second mistake: you need to remove the bottom line, which is displayed under the selected element, when the element is not selected, there is no line, only the element is selected - the line appears. Below I have attached screenshots of what I have now and what I need to get in the end. I would appreciate your help.
dropdown
Widget build(BuildContext context) {
return Container(
width: 120,
decoration: BoxDecoration(
border: Border(
bottom: selectedValue != null
? const BorderSide(
color: constants.Colors.white,
)
: BorderSide.none,
),
),
child: DropdownButtonHideUnderline(
child: DropdownButton2(
hint: const Text(
'Select',
style: constants.Styles.bigBookTextStyleWhite,
),
items: widget.items
.map((item) => DropdownMenuItem<String>(
value: item,
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: constants.Colors.white.withOpacity(0.1),
width: 1,
),
),
),
child: Center(
child: Row(
children: [
if (item == selectedValue)
const SizedBox(
width: 0,
),
Expanded(
child: Text(
item,
style: constants.Styles.smallTextStyleWhite,
),
),
if (item == selectedValue)
const Icon(
Icons.check,
color: constants.Colors.purpleMain,
),
],
),
),
),
))
.toList(),
value: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value as String;
});
},
icon: SvgPicture.asset(constants.Assets.arrowDropdown),
iconSize: 21,
buttonHeight: 27,
buttonElevation: 1,
itemHeight: 47,
dropdownMaxHeight: 191,
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 Center(
child: Text(
item,
style: constants.Styles.smallTextStyleWhite,
),
);
}).toList();
},
),
),
);
}
at the moment I have
should be the result
To change text style of the selected element use selectedItemBuilder. It's responsible for how the selected item will be displayed on button.
The bottom line under the selected element is shown by the decoration parameter you're using in the main Container widget. Remove it and bottom line should be gone.

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

Flutter - How to Change Dropdown Item List Size and Alignment

I am using flutter DropdownButton for list out something and it worked well while selecting an option from the list the drop-down list appears full screen, I need to give some padding so that the user can cancel the list(hide).
The code:
ListView(children: <Widget>[
....
Container(
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
border: Border.all(
color: Colors.grey,
style: BorderStyle.solid,
width: 0.80),
),
alignment: Alignment.center,
child: DropdownButtonHideUnderline(
child: DropdownButton(
isExpanded: true,
hint: Text('Select Your Height'),
icon: Icon(Icons.arrow_downward),
iconSize: 20,
iconEnabledColor: AppColors.PRIMARY_COLOR,
items: _heights.map((dropDownHieghtItem) {
return DropdownMenuItem(
value:
dropDownHieghtItem['height_id'].toString(),
child: Text(dropDownHieghtItem['height_name']));
}).toList(),
onChanged: (String newselectedHieght) {
setState(() {
heightID = newselectedHieght.toString();
});
},
value: heightID)),
),......
])
instead of using Text widget inside DropdownMenuItem use Container and add custom TextStyle
like this:
_heights.map((dropDownHieghtItem) {
return DropdownMenuItem(
value:
dropDownHieghtItem['height_id'].toString(),
child: Container(
alignment: Alignment.center,
child: Text(dropDownHieghtItem['height_name'],style: TextStyle(fontSize: 20,color: Colors.red),textAlign: TextAlign.center,)
)
);
}).toList(),

how to change color of dropdown underline

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.

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