Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
cf7e7e45
Unverified
Commit
cf7e7e45
authored
Sep 14, 2019
by
Shi-Hao Hong
Committed by
GitHub
Sep 14, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement DropdownButton.selectedItemBuilder (#40461)
* Implement DropdownButton.selectedItemBuilder
parent
64300123
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
2 deletions
+107
-2
dropdown.dart
packages/flutter/lib/src/material/dropdown.dart
+62
-1
dropdown_test.dart
packages/flutter/test/material/dropdown_test.dart
+45
-1
No files found.
packages/flutter/lib/src/material/dropdown.dart
View file @
cf7e7e45
...
@@ -28,6 +28,8 @@ const EdgeInsets _kUnalignedButtonPadding = EdgeInsets.zero;
...
@@ -28,6 +28,8 @@ const EdgeInsets _kUnalignedButtonPadding = EdgeInsets.zero;
const
EdgeInsets
_kAlignedMenuMargin
=
EdgeInsets
.
zero
;
const
EdgeInsets
_kAlignedMenuMargin
=
EdgeInsets
.
zero
;
const
EdgeInsetsGeometry
_kUnalignedMenuMargin
=
EdgeInsetsDirectional
.
only
(
start:
16.0
,
end:
24.0
);
const
EdgeInsetsGeometry
_kUnalignedMenuMargin
=
EdgeInsetsDirectional
.
only
(
start:
16.0
,
end:
24.0
);
typedef
DropdownButtonBuilder
=
List
<
Widget
>
Function
(
BuildContext
context
);
class
_DropdownMenuPainter
extends
CustomPainter
{
class
_DropdownMenuPainter
extends
CustomPainter
{
_DropdownMenuPainter
({
_DropdownMenuPainter
({
this
.
color
,
this
.
color
,
...
@@ -604,6 +606,7 @@ class DropdownButton<T> extends StatefulWidget {
...
@@ -604,6 +606,7 @@ class DropdownButton<T> extends StatefulWidget {
DropdownButton
({
DropdownButton
({
Key
key
,
Key
key
,
@required
this
.
items
,
@required
this
.
items
,
this
.
selectedItemBuilder
,
this
.
value
,
this
.
value
,
this
.
hint
,
this
.
hint
,
this
.
disabledHint
,
this
.
disabledHint
,
...
@@ -655,6 +658,50 @@ class DropdownButton<T> extends StatefulWidget {
...
@@ -655,6 +658,50 @@ class DropdownButton<T> extends StatefulWidget {
/// {@endtemplate}
/// {@endtemplate}
final
ValueChanged
<
T
>
onChanged
;
final
ValueChanged
<
T
>
onChanged
;
/// A builder to customize the dropdown buttons corresponding to the
/// [DropdownMenuItem]s in [items].
///
/// When a [DropdownMenuItem] is selected, the widget that will be displayed
/// from the list corresponds to the [DropdownMenuItem] of the same index
/// in [items].
///
/// {@tool snippet --template=stateful_widget_scaffold}
///
/// This sample shows a `DropdownButton` with a button with [Text] that
/// corresponds to but is unique from [DropdownMenuItem].
///
/// ```dart
/// final List<String> items = <String>['1','2','3'];
/// String selectedItem = '1';
///
/// @override
/// Widget build(BuildContext context) {
/// return Padding(
/// padding: const EdgeInsets.symmetric(horizontal: 12.0),
/// child: DropdownButton<String>(
/// value: selectedItem,
/// onChanged: (String string) => setState(() => selectedItem = string),
/// selectedItemBuilder: (BuildContext context) {
/// return items.map((String item) {
/// return Text(item);
/// }).toList();
/// },
/// items: items.map((String item) {
/// return DropdownMenuItem<String>(
/// child: Text('Log $item'),
/// value: item,
/// );
/// }).toList(),
/// ),
/// );
/// }
/// ```
/// {@end-tool}
///
/// If this callback is null, the [DropdownMenuItem] from [items]
/// that matches [value] will be displayed.
final
DropdownButtonBuilder
selectedItemBuilder
;
/// The z-coordinate at which to place the menu when open.
/// The z-coordinate at which to place the menu when open.
///
///
/// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12,
/// The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12,
...
@@ -849,7 +896,21 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
...
@@ -849,7 +896,21 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
// The width of the button and the menu are defined by the widest
// The width of the button and the menu are defined by the widest
// item and the width of the hint.
// item and the width of the hint.
final
List
<
Widget
>
items
=
_enabled
?
List
<
Widget
>.
from
(
widget
.
items
)
:
<
Widget
>[];
List
<
Widget
>
items
;
if
(
_enabled
)
{
items
=
widget
.
selectedItemBuilder
==
null
?
List
<
Widget
>.
from
(
widget
.
items
)
:
widget
.
selectedItemBuilder
(
context
).
map
((
Widget
item
)
{
return
Container
(
height:
_kMenuItemHeight
,
alignment:
AlignmentDirectional
.
centerStart
,
child:
item
,
);
}).
toList
();
}
else
{
items
=
<
Widget
>[];
}
int
hintIndex
;
int
hintIndex
;
if
(
widget
.
hint
!=
null
||
(!
_enabled
&&
widget
.
disabledHint
!=
null
))
{
if
(
widget
.
hint
!=
null
||
(!
_enabled
&&
widget
.
disabledHint
!=
null
))
{
final
Widget
emplacedHint
=
_enabled
final
Widget
emplacedHint
=
_enabled
...
...
packages/flutter/test/material/dropdown_test.dart
View file @
cf7e7e45
...
@@ -1317,7 +1317,51 @@ void main() {
...
@@ -1317,7 +1317,51 @@ void main() {
expect
(
tester
.
widget
<
DecoratedBox
>(
decoratedBox
).
decoration
,
defaultDecoration
);
expect
(
tester
.
widget
<
DecoratedBox
>(
decoratedBox
).
decoration
,
defaultDecoration
);
});
});
testWidgets
(
'Dropdown form field with autovalidation test'
,
(
WidgetTester
tester
)
async
{
testWidgets
(
'DropdownButton selectedItemBuilder builds custom buttons'
,
(
WidgetTester
tester
)
async
{
const
List
<
String
>
items
=
<
String
>[
'One'
,
'Two'
,
'Three'
,
];
String
selectedItem
=
items
[
0
];
await
tester
.
pumpWidget
(
StatefulBuilder
(
builder:
(
BuildContext
context
,
StateSetter
setState
)
{
return
MaterialApp
(
home:
Scaffold
(
body:
DropdownButton
<
String
>(
value:
selectedItem
,
onChanged:
(
String
string
)
=>
setState
(()
=>
selectedItem
=
string
),
selectedItemBuilder:
(
BuildContext
context
)
{
int
index
=
0
;
return
items
.
map
((
String
string
)
{
index
+=
1
;
return
Text
(
'
$string
as an Arabic numeral:
$index
'
);
}).
toList
();
},
items:
items
.
map
((
String
string
)
{
return
DropdownMenuItem
<
String
>(
child:
Text
(
string
),
value:
string
,
);
}).
toList
(),
),
),
);
},
),
);
expect
(
find
.
text
(
'One as an Arabic numeral: 1'
),
findsOneWidget
);
await
tester
.
tap
(
find
.
text
(
'One as an Arabic numeral: 1'
));
await
tester
.
pumpAndSettle
();
await
tester
.
tap
(
find
.
text
(
'Two'
));
await
tester
.
pumpAndSettle
();
expect
(
find
.
text
(
'Two as an Arabic numeral: 2'
),
findsOneWidget
);
});
testWidgets
(
'Dropdown form field with autovalidation test'
,
(
WidgetTester
tester
)
async
{
String
value
=
'one'
;
String
value
=
'one'
;
int
_validateCalled
=
0
;
int
_validateCalled
=
0
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment