From 7667a3f8eb61a3476adbe6f955f8abc434067f8c Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Thu, 1 Dec 2016 07:27:14 -0800 Subject: [PATCH] DropdownButton hint (#7114) --- .../lib/demo/buttons_demo.dart | 66 ++++++++++++------- .../flutter/lib/src/material/drop_down.dart | 35 +++++++--- .../flutter/test/material/drop_down_test.dart | 33 +++++++++- 3 files changed, 103 insertions(+), 31 deletions(-) diff --git a/examples/flutter_gallery/lib/demo/buttons_demo.dart b/examples/flutter_gallery/lib/demo/buttons_demo.dart index 133f9945ff..982b1202dc 100644 --- a/examples/flutter_gallery/lib/demo/buttons_demo.dart +++ b/examples/flutter_gallery/lib/demo/buttons_demo.dart @@ -132,7 +132,8 @@ class _ButtonsDemoState extends State { // https://en.wikipedia.org/wiki/Free_Four String dropdown1Value = 'Free'; - String dropdown2Value = 'Four'; + String dropdown2Value; + String dropdown3Value = 'Four'; Widget buildDropdownButton() { return new Padding( @@ -141,7 +142,7 @@ class _ButtonsDemoState extends State { mainAxisAlignment: MainAxisAlignment.start, children: [ new ListItem( - title: new Text('Scrollable dropdown:'), + title: new Text('Simple dropdown:'), trailing: new DropdownButton( value: dropdown1Value, onChanged: (String newValue) { @@ -149,6 +150,47 @@ class _ButtonsDemoState extends State { dropdown1Value = newValue; }); }, + items: ['One', 'Two', 'Free', 'Four'].map((String value) { + return new DropdownMenuItem( + value: value, + child: new Text(value), + ); + }).toList(), + ), + ), + new SizedBox( + height: 24.0, + ), + new ListItem( + title: new Text('Dropdown with a hint:'), + trailing: new DropdownButton( + value: dropdown2Value, + hint: new Text('Choose'), + onChanged: (String newValue) { + setState(() { + dropdown2Value = newValue; + }); + }, + items: ['One', 'Two', 'Free', 'Four'].map((String value) { + return new DropdownMenuItem( + value: value, + child: new Text(value), + ); + }).toList(), + ), + ), + new SizedBox( + height: 24.0, + ), + new ListItem( + title: new Text('Scrollable dropdown:'), + trailing: new DropdownButton( + value: dropdown3Value, + onChanged: (String newValue) { + setState(() { + dropdown3Value = newValue; + }); + }, items: [ 'One', 'Two', 'Free', 'Four', 'Can', 'I', 'Have', 'A', 'Little', 'Bit', 'More', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten' @@ -162,26 +204,6 @@ class _ButtonsDemoState extends State { .toList(), ), ), - new SizedBox( - height: 24.0, - ), - new ListItem( - title: new Text('Simple dropdown:'), - trailing: new DropdownButton( - value: dropdown2Value, - onChanged: (String newValue) { - setState(() { - dropdown2Value = newValue; - }); - }, - items: ['One', 'Two', 'Free', 'Four'].map((String value) { - return new DropdownMenuItem( - value: value, - child: new Text(value), - ); - }).toList(), - ), - ) ], ), ); diff --git a/packages/flutter/lib/src/material/drop_down.dart b/packages/flutter/lib/src/material/drop_down.dart index 6d8cde1578..96af3f5971 100644 --- a/packages/flutter/lib/src/material/drop_down.dart +++ b/packages/flutter/lib/src/material/drop_down.dart @@ -420,6 +420,7 @@ class DropdownButton extends StatefulWidget { Key key, @required this.items, this.value, + this.hint, @required this.onChanged, this.elevation: 8, this.style, @@ -439,6 +440,9 @@ class DropdownButton extends StatefulWidget { /// selected. final T value; + /// Displayed if [value] is null. + final Widget hint; + /// Called when the user selects an item. final ValueChanged onChanged; @@ -530,6 +534,21 @@ class _DropdownButtonState extends State> { @override Widget build(BuildContext context) { assert(debugCheckHasMaterial(context)); + + // The width of the button and the menu are defined by the widest + // item and the width of the hint. + final List items = new List.from(config.items); + int hintIndex; + if (config.hint != null) { + hintIndex = items.length; + items.add(new DefaultTextStyle( + style: _textStyle.copyWith(color: Theme.of(context).hintColor), + child: new IgnorePointer( + child: config.hint, + ), + )); + } + Widget result = new DefaultTextStyle( style: _textStyle, child: new SizedBox( @@ -538,12 +557,12 @@ class _DropdownButtonState extends State> { mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisSize: MainAxisSize.min, children: [ - // The button's size is defined by its largest menu item. If value is - // null then an item does not appear. + // If value is null (then _selectedIndex is null) then we display + // the hint or nothing at all. new IndexedStack( - index: _selectedIndex, + index: _selectedIndex ?? hintIndex, alignment: FractionalOffset.centerLeft, - children: config.items, + children: items, ), new Icon(Icons.arrow_drop_down, size: config.iconSize, @@ -568,10 +587,10 @@ class _DropdownButtonState extends State> { height: 1.0, decoration: const BoxDecoration( border: const Border(bottom: const BorderSide(color: const Color(0xFFBDBDBD), width: 0.0)) - ) - ) - ) - ] + ), + ), + ), + ], ); } diff --git a/packages/flutter/test/material/drop_down_test.dart b/packages/flutter/test/material/drop_down_test.dart index 21246cc762..6b574d6b0d 100644 --- a/packages/flutter/test/material/drop_down_test.dart +++ b/packages/flutter/test/material/drop_down_test.dart @@ -9,13 +9,20 @@ import 'package:flutter/material.dart'; final List menuItems = ['one', 'two', 'three', 'four']; -Widget buildFrame({ Key buttonKey, String value: 'two', ValueChanged onChanged, bool isDense: false }) { +Widget buildFrame({ + Key buttonKey, + String value: 'two', + ValueChanged onChanged, + bool isDense: false, + Widget hint, + }) { return new MaterialApp( home: new Material( child: new Center( child: new DropdownButton( key: buttonKey, value: value, + hint: hint, onChanged: onChanged, isDense: isDense, items: menuItems.map((String item) { @@ -317,4 +324,28 @@ void main() { expect(value, equals('one')); }); + testWidgets('Size of DropdownButton with null value and a hint', (WidgetTester tester) async { + Key buttonKey = new UniqueKey(); + String value; + + // The hint will define the dropdown's width + Widget build() => buildFrame(buttonKey: buttonKey, value: value, hint: new Text('onetwothree')); + + await tester.pumpWidget(build()); + expect(find.text('onetwothree'), findsOneWidget); + RenderBox buttonBoxHintValue = tester.renderObject(find.byKey(buttonKey)); + assert(buttonBoxHintValue.attached); + + + value = 'three'; + await tester.pumpWidget(build()); + RenderBox buttonBox = tester.renderObject(find.byKey(buttonKey)); + assert(buttonBox.attached); + + // A DropDown button with a null value and a hint should be the same size as a + // one with a non-null value. + expect(buttonBox.localToGlobal(Point.origin), equals(buttonBoxHintValue.localToGlobal(Point.origin))); + expect(buttonBox.size, equals(buttonBoxHintValue.size)); + }); + }