Flutter: Width of DropDown menu independent of Button size - flutter

I have two DropDownButtons in one Row.
DropdownButtonHideUnderline(
child: DropdownButton(
isExpanded: true,
value: item,
items: widget.items.map((item) {
return DropdownMenuItem(
value: item,
child: Text(
item.toString(),
);
}).toList(),
onChanged: widget.onChanged,
selectedItemBuilder: (context) {
return widget.items.map((item) {
return Align(
child: Text(
item.toString(),
),
alignment: Alignment.centerLeft,
);
}).toList();
}
)
)
)
Is there a way to stretch the width of the menu instead of keeping it the size of the button?
Edit:
Example: Dart pad (Thanks to #Diwyansh; Dropdown only uses half the width of the UI, not entire)

I've fixed this and added more customization in DropdownButton2. By default dropdown menu will take button width but you can change it too if you want. Dropdownbutton2 is based on Flutter's core DropdownButton with more options you can customize to your needs.

Related

Flutter DropdownButtonFormField List of items symmetrical

I would like to show DropdownButtonFormField's List of item in the middle of screen. It's never aligned to the center. DropdownButtonFormField is centered already. Spacing on the left and right side are different.
You should be try this
Add DropdownButtonFormField() inside Padding() Widget
Padding (
padding:EdgeInsets.all(16.0),
child:DropdownButtonFormField(),
),
Use this
DropdownButton(
isExpanded: true
value: fieldSelected,
items: items.map<DropdownMenuItem<String>>((var value) {
return DropdownMenuItem<String>(
value: value,
child: Center(
child: Text(
"Your text",
textAlign: TextAlign.center,
),
),
);
}).toList(),
),

How to add a title to a drop down menu in Flutter Web?

I'm currently working with Flutter web and I'm trying to achieve the following:
DropdownMenu
I can't set a title for the dropdown menu, only add the title as an item in the list.
I tried what Firas Krifa proposed and I got the following result:
Result in small web size
Result in 4K size
Normal web size(FULL HD)
I don't know why in the small size the dropdownmenu appears at the left of the dropdownbutton and the 4k size appears at the right.
Also on the normal size it appears on the title, while what I want is that it drops right under the title.
Try this
new DropdownButton<String>(
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {},
)
The widgets DropdownButtonHideUnderline and Theme will help control how the dropdown looks in the title of the AppBar
https://material.io/guidelines/layout/structure.html#structure-app-bar
new AppBar(
title: new Theme(
child: new DropdownButtonHideUnderline(
child: new DropdownButton<String>(
value: _value,
items: <DropdownMenuItem<String>>[
new DropdownMenuItem(
child: new Text('My Page'),
value: 'one',
),
],
onChanged: (String value) {
setState(() => _value = value);
},
),
),
data: new ThemeData.dark(),
),
),

Flutter Wrap widget align: left inside ExpansionPanel

This is strange, the Wrap widget, containing FilterChips, is centering inside it's parent a ExpansionPanel. I've tried wrapping the Wrap Widget in a Flexible widget, no reaction, I've tried wrapping in an Expanded widget, no reaction.
There doesn't appear to be a property on the Expansion panel to left align body: Widgets. It's important because some of the ExpansionPanels are aligning to the left, when the Wrap widget has been forced to full width, but others, like pictured, center. Ultimately I'm going to wrap all the children widgets in the ExpansionPanel in a Padding widget, but I need the child Wrap widgets aligning left first.
bool travelSack;
ExpansionPanelRadio backpackingPanel = ExpansionPanelRadio(
value: "Backpacking",
headerBuilder: (BuildContext context, bool isExpanded) {
return ListTile(
leading: FaIcon(
FontAwesomeIcons.globeEurope,
size: 19,
),
title: Text("Backpacking"),
);
},
body: Expanded(child:
Wrap(spacing: 4, children: [
FilterChip(
showCheckmark: false,
label: Text('Travel rucksack'),
labelStyle: TextStyle(
color: travelSack ? Colors.black : Colors.white,
),
selected: travelSack,
onSelected: (bool selected) {
setState(() {
travelSack = !travelSack;
});
},
selectedColor: Colors.green.shade500,
backgroundColor: Colors.grey.shade500,
),
])
);
I'm Scoobied, help appreciated.
I solved this by nesting the Wrap widget inside an Align widget:
Align(
alignment: Alignment.topLeft,
child: Wrap(....
I do not like my solution, I'm wary of so much nestings effect on performance and sprawling code harms legibility, so if you have a better solution I'm all eyes. x Sam.
Wrap has an alignment property that you can use to align the children.
https://api.flutter.dev/flutter/widgets/Wrap/Wrap.html
Wrap(
...
alignment: WrapAlignment.center,
children: [],
),
I had the same problem and resolve with SizedBox:
ExpansionPanelRadio backpackingPanel = ExpansionPanelRadio(
value: "Backpacking",
headerBuilder: (BuildContext context, bool isExpanded) {},
body: SizedBox(
width: double.infinity, // Use when direction: Axis.horizontal
height: double.infinity, // Use when direction: Axis.vertical
child: Wrap(
spacing: 4,
children: [
FilterChip(
showCheckmark: false,
label: Text('Travel rucksack'),
labelStyle: TextStyle(
color: travelSack ? Colors.black : Colors.white,
),
selected: travelSack,
onSelected: (bool selected) {
setState(() {
travelSack = !travelSack;
});
},
selectedColor: Colors.green.shade500,
backgroundColor: Colors.grey.shade500,
),
],
),
),
);
Remember to not use width and height SizedBox property at same time, only one for each Wrap.direction.
Reference

Control position of validation label

When using Form validator the default position of the message label is on the bottom, is there a way to put it on the top of the Text field?
You can not change position on validation label in TextFormField
But you can build your own TextFormField by using FormField widget and custumize its children to look like a normal TextFormField this is a working example made with FormField
FormField(
// initialValue: 0,
autovalidate: true,
validator: normalValidator,
builder: (state) {
state.validate();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
state.errorText!=null? Text(state.errorText, style: TextStyle(color: Colors.red, fontSize: 12),):Container(),
DropdownButton(
isExpanded: true,
value: state.value,
items: _choices,
onChanged: (v) {
state.didChange(v);
setState((){});
}),
],
);
}),
You can now customize it to sweet your needs. Find more about FormField here on official doc

How can I limit to only five items in the dropdown but still be able to scroll down?

We have a list with 20+ elements that we have to show in a dropdown. The problem is that when we deploy the dropdown, the list covers all the screen. We want to reduce the number of shown elements to about 5 or 6 and be able to scroll down the rest of the list.
We would also like to know if it's possible that the dropDownMenuItems deploy under the dropDownButton.
We have tryed doing ".toList().sublist(0,5)," but this way didnĀ“t allow us to scroll.
Widget _dropdown() {
return new Container(
width: MediaQuery.of(context).size.width * 35 / 100,
height: MediaQuery.of(context).size.height * 6 / 100,
decoration: BoxDecoration(
backgroundBlendMode: BlendMode.darken,
),
child: Theme(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: new DropdownButton(
hint: Text(
'HINT TEXT',
),
value: selected_item,
onChanged: (newValue) {
setState(
() {
selected_item = newValue;
},
);
},
isDense: false,
isExpanded: true,
items: data.map((location) {
return new DropdownMenuItem<String>(
child: new Container(
child: Align(
alignment: Alignment.centerLeft,
child: new Text(
location.toUpperCase(),
),
),
),
value: location,
);
}).toList(),
),
),
),
),
);
}
Look into this custom DropDown Button. It is the normal Dropdown Button with the possibility to give height to the widget. If you set height = 5* 48.0 (which is the _menuItemHeight) you should only see 5 elements.