Repository navigation
Handling multi-line items in DropdownButton and DropdownMenuItem #9211
Copy link
Copy link
Closed
Labels
c: datalossCan result in the user losing their stateCan result in the user losing their statecustomer: quill (g3)frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packages
Milestone
Description
Internal: b/140830819
Issue
The following screenshot demonstrates the issue.
If the dropdown is open, the items are correctly displayed as - for example - two line items, but if the dropdown is closed, only the first line will be visible, and the dropdown indicator won't be visible.
Steps to Reproduce
See the following test project:
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: new Text('Test project'),
),
body: new Padding(
padding: new EdgeInsets.all(16.0),
child: new TestStatefulWidget(),
),
),
));
}
class TestStatefulWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => new TestStatefulWidgetState();
}
class TestStatefulWidgetState extends State<TestStatefulWidget> {
static const List<String> shortItems = const [
'These',
'items',
'are',
'short',
'enough',
];
static const List<String> longItems = const [
'These items will probably be longer than what fits on the screen correctly',
'Because of this, the icon indicator on the right side will not show',
'Instead, in development mode, a red indicator will display, showing something is wrong',
];
String shortSpinnerValue = shortItems[0];
String longSpinnerValue = longItems[0];
@override
Widget build(BuildContext context) {
return new Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
new DropdownButton(
value: shortSpinnerValue,
onChanged: (string) => setState(() => shortSpinnerValue = string),
items: shortItems.map((string) {
return new DropdownMenuItem(
child: new Text(string),
value: string,
);
}).toList(),
),
new DropdownButton(
value: longSpinnerValue,
onChanged: (string) => setState(() => longSpinnerValue = string),
items: longItems.map((string) {
return new DropdownMenuItem(
child: new Text(string),
value: string,
);
}).toList(),
),
],
);
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
c: datalossCan result in the user losing their stateCan result in the user losing their statecustomer: quill (g3)frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packages
Activity
abarth commentedon Apr 5, 2017
Would you expect it to wrap or to have an ellipsis?
hundeva commentedon Apr 5, 2017
I'd expect an ellipsis, as in some corner cases, the selectable options may be very long (three, or even more lines is possible).
eseidelGoogle commentedon Apr 12, 2017
If you just pass
overflow: TextOverflow.elipsisto you Text call in you DropDownMenuItem I think you'll get the effect you want?hundeva commentedon Apr 12, 2017
Unfortunately, that won't work. If I add
overflow: TextOverflow.elipsisto the text fields, the following thing happens:The modified test project:
hundeva commentedon Sep 20, 2017
Hi,
Your english is fine, don't worry :)
The problem with that solution is, it doesn't really adapt to different screen sizes, where I would like it to fit the whole width for example. Of course, I could work around that like, the width of the Container is = width of the screen - width of the dropdown button, but that is a lot of boilerplate, which I would like to avoid.
Thanks!
maryx commentedon Oct 23, 2017
I have the same question, except that I would like to show the full line, with word wrap, in the expanded state (since the user may not necessarily know what the line says if it ends in ellipses), but show ellipses in the collapsed state.
Currently, with this setup:
I get this overlapping of text, where the wrapping of the words doesn't change the height of each dropdown menu item. Ideally, each DropdownMenuItem should space according to its text wrap:

With a selected item, I get this. Ideally, I would be able to set the

overflowto ellipses only for the currently-selected dropdown item in collapsed view. DropdownButton only lets me set a TextStyle though:brianegan commentedon Oct 31, 2017
I think this may be a legit problem with the
DropDownButtonitself, and might have a simple fix... Happy to submit a PR if you think this is a good solution.After looking at the
buildmethod of the_DropdownButtonState, it has anIndexedStackandIconcontained in aRow. However, since theIndexedStackis not wrapped in anExpanded, it is allowed to grow indefinitely. Therefore, even if you useTextOverflow.ellipseon your Text Widget, it will continue to Overflow.I extracted the
DropDownButtoninto aMyDropdownButtonthat wraps theIndexedStackwith anExpandedto test this theory. The Screenshots and code are below:Long Text Without Fix (No Expanded)
Long Text With Fix
Code with Fix
maryx commentedon Oct 31, 2017
@brianegan You should submit a PR, it would help my current app a lot :). I'm not sure if it's the best solution but the reviewers will give feedback.
What does a dropdown item look like in that state? Is the text also truncated. or is the item height expanded?
I am blocked by this issue for an app.
67 remaining items
malviyaritesh commentedon Sep 12, 2019
One can create and use custom widget in place of
DropdownButtonFormFielduntilisExpandedis officially supported.https://gist.github.com/malviyaritesh/04b94f72c5c2047ea4bd06aaf0a99fc7
git-xbits commentedon Sep 12, 2019
You have a exemple to this project? I have problems with List of objects
malviyaritesh commentedon Sep 13, 2019
@git-xbits If you know to use
DropdownButtonFormField, thenMultilineDropdownButtonFormFieldis just a replacement of it with added properties likeisExpandedandisDense.To use it, you just need to replace
DropdownButtonFormFieldwithMultilineDropdownButtonFormFieldand provide properties likeisExpanded: true&isDense: false.Here's a basic example (incomplete to work) showing its uses:
HansMuller commentedon Sep 18, 2019
This version of the original example demonstrates how one can use the new selectedItemBuilder feature to customize the way the dropdown's selected item is displayed - and avoid the overflow. It also shows how one can hew to the Material Design spec by limiting dropdown items to two lines of text. Note also that the dropdown button's
isExpandedparameter is true in the second dropdown, to force the dropdown's button to fit within the available horizontal space.HansMuller commentedon Sep 18, 2019
I'm going to close this issue because the original problem has been addressed: #9211 (comment)
Dropdown menus are still limited because their items are fixed height. That came up in some of the feedback, for example #9211 (comment), #9211 (comment), #9211 (comment).
I've opened a separate issue for the fixed height limitation: #40773
miguelpruivo commentedon Oct 18, 2019
@HansMuller this doesn't seem to be available for
DropdownButtonFormField. Am I missing something?HansMuller commentedon Oct 18, 2019
@miguelpruivo - you are correct, #43021
shihaohong commentedon Oct 18, 2019
The
DropdownButtonFormField.selectedItemBuilder's equivalent has also been created as well: #42481darkstarx commentedon Oct 12, 2020
Вот понаписали тонны слов. Лучше бы просто исправили. Стыд программистам Google!!!
GSPrakashNaidu commentedon Jun 29, 2021
Thank you @maryx :), It worked for me.
github-actions commentedon Jul 30, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.