I want to dropdownlist but i'm not able to do following things.
change the width of the dropdownbutton
make the dropdownlist to start at the dropdownbutton's height , not above it as default
adjust the height of the dropdownmenuitem
Want i want is
What i have is
The code goes like
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
// isDense: true,
// isExpanded: true,
itemHeight: null,
// menuMaxHeight: 10,
alignment: AlignmentDirectional.center,
elevation: 0,
value: selectedQuantity,
selectedItemBuilder: (BuildContext context) {
return _dropDownQuantities.map<Widget>((String item) {
return Container(
alignment: Alignment.center,
// color: Colors.green,
child: Text(
'Qty: $item',
style: TextStyle(fontSize: 14),
),
);
}).toList();
},
items: _dropDownQuantities.map((e) {
return DropdownMenuItem(
alignment: AlignmentDirectional.topStart,
child: Container(
child: Column(
children: [Container(child: Text(e))],
)),
value: e,
);
}).toList(),
hint: Text("Qty: 1 "),
onChanged: (value) {
setState(() {
selectedQuantity = value!;
});
}),
),
),
Use DropdownButton2 to achieve that.
Use buttonWidth property to change the width of the dropdownbutton.
Use offset property to change the position of the dropdown menu. You should add button's height to the dy offset to make it start at the dropdownbutton's height like this: Offset(0.0, "button's height")
Use itemHeight property to adjust the height of the dropdownmenuitem.
Disclaimer: I am the author of the package mentioned above.
Related
I'm new to flutter and currently I'm trying to create a reusable dropdown button. The problem now is I wanted to have 15 item in my dropdown item list and it's taking all my screen space. I wanted to limit them so at first it will only show around 5-6 item and the user can scroll down the dropdown List and show the rest of the item list.
How can I achieve this ?
Here's my current code for reusable dropdown Button:
Widget build(BuildContext context) {
return Container(
decoration: widget.boxDecor,
padding: widget.padding,
margin: widget.margin,
width: widget.width,
height: widget.height,
child: Center(
child: DropdownButton<String>(
value: widget.initialValue ?? dropdownValue,
icon: widget.iconDropdown,
isExpanded: true,
elevation: 16,
style: primaryColor400Style.copyWith(
fontSize: fontSize10,
),
underline: Container(
height: 2,
decoration: BoxDecoration(
color: Colors.transparent,
),
),
onChanged: (String? newValue) {
if (newValue != 'Personal') {
Provider.of<SelectedTeamProvider>(context, listen: false)
.setSelectedTeamNameForTicket(newValue!);
} else {
Provider.of<SelectedTeamProvider>(context, listen: false)
.setSelectedTeamNameForTicket('');
}
if (widget.onChanged != null) {
widget.onChanged!(newValue!);
}
setState(() {
dropdownValue = newValue!;
});
},
items: itemLists.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value.tr()),
);
}).toList(),
),
),
);
}
You can use menuMaxHeight to control the size of the menu.
child: DropdownButton<int>(
menuMaxHeight: 300, // <-- Adjust this value to get the right number of items
...
),
I want to any way set Full Width 100% in dart/flutter. but it is not set in dropdown list in flutter.
my source code below in details.
Container(
height: 50,
width: 500.0,
// width: MediaQuery.of(context).size.width,
child: Expanded(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
isExpanded: true,
value: _selectionRetailer,
style: Theme.of(context).textTheme.bodyText1,
// items: mRetailList.map((index) {
items: mRouteWiseRetailList.map((index) {
return DropdownMenuItem(
alignment: Alignment.center,
child: new Text(
index.retailerTitle + "_" + index.retailerAddress,
style: TextStyle(fontSize: 10),
),
value: index.retailerID,
);
}).toList(),
onChanged: (sal) {
setState(() {
mRetailer = sal;
_selectionRetailer = sal;
retailerID = sal;
print("SAVEDATADD" + "retailerID: $retailerID");
if (reasonID == "0") {
} else {
startTime = Fhelper.CurrentDateTime();
Fhelper.showToast(
"RetailerID: $retailerID, StartTime: $startTime, salesOfficerID: $salesOfficerID");
print("SAVEDATADD" +
"RetailerID: $retailerID, StartTime: $startTime, salesOfficerID: $salesOfficerID,entryBy : $entryBy");
clearAll();
}
});
},
),
),
),
),
),
how to achieve this full width in dropdown in flutter.
Remove the Expanded and Container and follow the given below code
DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
isExpanded: true,
value: dropdownvalue,
icon: Icon(Icons.keyboard_arrow_down),
items:items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items)
);
}
).toList(),
onChanged: (String newValue){
setState(() {
dropdownvalue = newValue;
});
},
),
),
)
How can I change the height of a DropdownButton in flutter.
I have tried to use Padding and SizedBox but none is realy working.
SizedBox just increases the container size while the DropdownButton is clamped to top left and therefore is not centered anymore.
Padding is ignored or moves the content outside of the button.
I do not want to change the size of the dropdown overlay but the button itself.
build(BuildContext context) {
return ThemeData(
data: ThemeData(canvasColor: Colors.white),
child: DropdownButton(
items: _items.map((item) => DropdownMenuItem(child: Text(item), value: item)).toList(),
isExpanded: true,
selectedItemBuilder: (_) {
return _items.map<Widget>((String lang) {
return Center(
widthFactor: 1,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Text(lang, style: TextStyle(color: Colors.black))
),
);
}).toList();
}
)
)
}
Wrap it in a Container, give a height, width as per your need and set isExpanded true in DropDownButton. Also change dropdownbutton text font size as per your need.
Container(
height: 50.0,
width: 200.0,
child: DropdownButton(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
isExpanded: true,
style: TextStyle(color: Colors.deepPurple, fontSize: 20.0),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
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(),
)
)
End product should look something like this,
Looks like the 'itemHeight' property for the DropdownButton class should do the trick. I tried it and it increased the height of my DropdownButton.
Here is some sample code I have from a previous project using the itemHeight:
DropdownButton<String>(
itemHeight: 100.0,
value: selectedCurrency,
items: dropdownItems,
onChanged: (value) {
setState(() {
selectedCurrency = value;
getData();
});
},
);
Note: Just make sure the value you provide isn't less than 48.0, since it will give an error.
Docs:
itemHeight property: https://api.flutter.dev/flutter/material/DropdownButton/itemHeight.html
Minimum itemHeight defined by 'kMinInteractiveDimension':
https://api.flutter.dev/flutter/material/kMinInteractiveDimension-constant.html
itemHeight: null,
You just need to leave the itemHeight with the null value.
It will make the height of the DropdownButton with the menu item's intrinsic height.
Use SizedBox Widget
SizedBox(
height: 30,
child:DropdownButton<String>(
value: selectedCurrency,
items: dropdownItems,
onChanged: (value) {},
))
The easiest of all methods is using DropDownButton2 which is built over DropDownButton
And just change the buttonHeight attribute
Not just buttonHeight , you can even change itemHeight , buttonwidth and many more customize
String? selectedValue;
List<String> items = [
'Item1',
'Item2',
'Item3',
'Item4',
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: DropdownButtonHideUnderline(
child: DropdownButton2(,
items: items
.map((item) =>
DropdownMenuItem<String>(
value: item,
child: Text(
item,
),
))
.toList(),
value: selectedValue,
onChanged: (value) {
setState(() {
});
},
buttonHeight: 40,
buttonWidth: 140,
itemHeight: 40,
),
),
),
);
}
checkout : DropDownButton2 for more examples.
Hope it helps !!
You need to use menuMaxHeight property of DropdownButton Widget.
child: DropdownButton(
menuMaxHeight: 500.0,
value: '1',
items: setTotalPages(),
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
)
Add this property menuMaxHeight: 200, to DropdownButton
I am new to flutter development. I am using the dropdown button of my application. When opening the drop-down menu, the text is getting cut in the popup dialog. Below I attached a screenshot with coding. Please guide me in fixing this issue.
DropdownButtonHideUnderline(
child: new DropdownButton(
isExpanded: true,
value: dropDownValue,
isDense: true,
//icon: Icon(Icons.keyboard_arrow_down, color: Colors.white,),
onChanged: (String newValue) {
setState(() {
dropDownValue = newValue;
state.didChange(newValue);
});
},
items: dropDownList.map((String value) {
return new DropdownMenuItem(
value: value,
child: new SizedBox(
width: MediaQuery.of(context).size.width / 1.4,
child: new Text(value,
softWrap: true,
style: TextStyle(color: Colors.white, fontSize: 18.0),),)
);
}).toList(),
),
),
);
Copying the DropdownMenuItem class as someone else suggested will not be enough as DropdownButton requires items to be of type List<DropdownMenuItem<T>>.
I have created the following widget which should help with your issue:
import 'package:flutter/material.dart';
/// Looks like a DropdownButton but has a few differences:
///
/// 1. Can be opened by a single tap even if the keyboard is showing (this might be a bug of the DropdownButton)
///
/// 2. The width of the overlay can be different than the width of the child
///
/// 3. The current selection is highlighted in the overlay
class CustomDropdown<T> extends PopupMenuButton<T> {
CustomDropdown({
Key key,
#required PopupMenuItemBuilder<T> itemBuilder,
#required T selectedValue,
PopupMenuItemSelected<T> onSelected,
PopupMenuCanceled onCanceled,
String tooltip,
double elevation = 8.0,
EdgeInsetsGeometry padding = const EdgeInsets.all(8.0),
Icon icon,
Offset offset = Offset.zero,
Widget child,
String placeholder = "Please select",
}) : super(
key: key,
itemBuilder: itemBuilder,
initialValue: selectedValue,
onSelected: onSelected,
onCanceled: onCanceled,
tooltip: tooltip,
elevation: elevation,
padding: padding,
icon: icon,
offset: offset,
child: child == null ? null : Stack(
children: <Widget>[
Builder(
builder: (BuildContext context) => Container(
height: 48,
alignment: AlignmentDirectional.centerStart,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
DefaultTextStyle(
style: selectedValue!= null ? Theme.of(context).textTheme.subhead
: Theme.of(context).textTheme.subhead.copyWith(color:
Theme.of(context).hintColor),
child: Expanded(child: selectedValue== null ? Text(placeholder) : child),
),
IconTheme(
data: IconThemeData(
color: Theme.of(context).brightness == Brightness.light
? Colors.grey.shade700 : Colors.white70,
),
child: const Icon(Icons.arrow_drop_down),
),
],
),
),
),
Positioned(
left: 0.0,
right: 0.0,
bottom: 8,
child: Container(
height: 1,
decoration: const BoxDecoration(
border: Border(bottom: BorderSide(color: Color(0xFFBDBDBD), width: 0.0)),
),
),
),
],
),
);
}
It actually extends PopupMenuButton as you can see, but I've made it look the same as the DropdownButton.
itemBuilder needs to return List<PopupMenuEntry<T>>, with each entry usually being a PopupMenuItem to which you can provide any child widget.
selectedValue is the currently selected value, which will be highlighted in the overlay. If it is null, a Text widget with the placeholder string is shown. If it is not null, the child widget is shown.
You should be able to disable the highlight by modifying this class to either call super() with an initialValue of null, or even better add a boolean to the constructor to control this from the outside.
The height of DropdownMenuItem is hardcoded to _kMenuItemHeight:
https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/dropdown.dart#L486
The only thing you can do is copy this entire file and adjust to your needs.
I need to change widget's textDirection according to the language selection. I have a variable for textDirection in my global translation class and add it to parent widgets. All are work fine except DropdownMenuItems. I tried with wrapping text widget by Directionality widget but still not working as expect.
Ex: when I change language which use rtl items should align to right which
is not happening currently.
return DropdownMenuItem(
child: Directionality(textDirection: Translations.textDirection ,
child: new Text(listItems,textDirection: Translations.textDirection,)
...
Also I tried adding textDirection after wrapped whole DropdownButton with a Container.
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
textDirection: Translations.textDirection,
children: <Widget>[
DropdownButton(
But still not work. What should I do to change the textDirection of DropdownMenuItems ?? Any help would be appreciate.
note: I can't add textDirection to MaterialApp. Need to change it when select different languages.
I Solved it
DropdownButton<String>(
hint: Text(
'City',
style: TextStyle(fontFamily: 'Cairo'),
),
isExpanded: true,
value: dropdownValue,
elevation: 4,
style: TextStyle(
color: Colors.deepPurple, fontFamily: 'Cairo'),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
debugPrint(cityInfo.indexOf(newValue).toString());
});
},
items:
cityInfo.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Container(
alignment: Alignment.centerRight,
child: Text(
value,
),
),
);
}).toList(),
),
For left to right text direction:
Text('MyText', textDirection: TextDirection.ltr,)
OR
For right to left text direction:
Text('MyText', textDirection: TextDirection.rtl)
DropdownButton<String>(
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Center(
child: Text(value),
),
);
}).toList(),
onChanged: (_) {},
),
Just wrap the DropDownMenuItem child Widget(Text) with a Center widget
Update: Sorry I miss understood it as how to center the widget.
You can add TextDirection.rtl or TextDirection.ltr to textDirection property
If it does not work try wrapping it a Row with mainAxisAlignment to end
use Directionality widget as aparent of the