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
85e5b210
Unverified
Commit
85e5b210
authored
Mar 30, 2020
by
Shi-Hao Hong
Committed by
GitHub
Mar 30, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement DropdownMenuItem.onTap (#53368)
* Implement DropdownMenuItem.onTap
parent
c7a6e300
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
96 additions
and
1 deletion
+96
-1
dropdown.dart
packages/flutter/lib/src/material/dropdown.dart
+11
-1
dropdown_test.dart
packages/flutter/test/material/dropdown_test.dart
+85
-0
No files found.
packages/flutter/lib/src/material/dropdown.dart
View file @
85e5b210
...
@@ -145,9 +145,15 @@ class _DropdownMenuItemButtonState<T> extends State<_DropdownMenuItemButton<T>>
...
@@ -145,9 +145,15 @@ class _DropdownMenuItemButtonState<T> extends State<_DropdownMenuItemButton<T>>
}
}
void
_handleOnTap
()
{
void
_handleOnTap
()
{
final
DropdownMenuItem
<
T
>
dropdownMenuItem
=
widget
.
route
.
items
[
widget
.
itemIndex
].
item
;
if
(
dropdownMenuItem
.
onTap
!=
null
)
{
dropdownMenuItem
.
onTap
();
}
Navigator
.
pop
(
Navigator
.
pop
(
context
,
context
,
_DropdownRouteResult
<
T
>(
widget
.
route
.
items
[
widget
.
itemIndex
].
i
tem
.
value
),
_DropdownRouteResult
<
T
>(
dropdownMenuI
tem
.
value
),
);
);
}
}
...
@@ -656,11 +662,15 @@ class DropdownMenuItem<T> extends _DropdownMenuItemContainer {
...
@@ -656,11 +662,15 @@ class DropdownMenuItem<T> extends _DropdownMenuItemContainer {
/// The [child] argument is required.
/// The [child] argument is required.
const
DropdownMenuItem
({
const
DropdownMenuItem
({
Key
key
,
Key
key
,
this
.
onTap
,
this
.
value
,
this
.
value
,
@required
Widget
child
,
@required
Widget
child
,
})
:
assert
(
child
!=
null
),
})
:
assert
(
child
!=
null
),
super
(
key:
key
,
child:
child
);
super
(
key:
key
,
child:
child
);
/// Called when the dropdown menu item is tapped.
final
VoidCallback
onTap
;
/// The value to return if the user selects this menu item.
/// The value to return if the user selects this menu item.
///
///
/// Eventually returned in a call to [DropdownButton.onChanged].
/// Eventually returned in a call to [DropdownButton.onChanged].
...
...
packages/flutter/test/material/dropdown_test.dart
View file @
85e5b210
...
@@ -2361,4 +2361,89 @@ void main() {
...
@@ -2361,4 +2361,89 @@ void main() {
expect
(
value
,
equals
(
'two'
));
expect
(
value
,
equals
(
'two'
));
expect
(
dropdownButtonTapCounter
,
2
);
// Should not change.
expect
(
dropdownButtonTapCounter
,
2
);
// Should not change.
});
});
testWidgets
(
'DropdownMenuItem onTap callback is called when defined'
,
(
WidgetTester
tester
)
async
{
String
value
=
'one'
;
final
List
<
int
>
menuItemTapCounters
=
<
int
>[
0
,
0
,
0
,
0
];
void
onChanged
(
String
newValue
)
{
value
=
newValue
;
}
final
List
<
VoidCallback
>
onTapCallbacks
=
<
VoidCallback
>[
()
{
menuItemTapCounters
[
0
]
+=
1
;
},
()
{
menuItemTapCounters
[
1
]
+=
1
;
},
()
{
menuItemTapCounters
[
2
]
+=
1
;
},
()
{
menuItemTapCounters
[
3
]
+=
1
;
},
];
int
currentIndex
=
-
1
;
await
tester
.
pumpWidget
(
TestApp
(
textDirection:
TextDirection
.
ltr
,
child:
Material
(
child:
RepaintBoundary
(
child:
DropdownButton
<
String
>(
value:
value
,
onChanged:
onChanged
,
items:
menuItems
.
map
<
DropdownMenuItem
<
String
>>((
String
item
)
{
currentIndex
+=
1
;
return
DropdownMenuItem
<
String
>(
value:
item
,
onTap:
onTapCallbacks
[
currentIndex
],
child:
Text
(
item
),
);
}).
toList
(),
),
),
),
),
);
// Tap dropdown button.
await
tester
.
tap
(
find
.
text
(
'one'
));
await
tester
.
pumpAndSettle
();
expect
(
value
,
equals
(
'one'
));
// Counters should still be zero.
expect
(
menuItemTapCounters
,
<
int
>[
0
,
0
,
0
,
0
]);
// Tap dropdown menu item.
await
tester
.
tap
(
find
.
text
(
'three'
).
last
);
await
tester
.
pumpAndSettle
();
// Should update the counter for the third item (second index).
expect
(
value
,
equals
(
'three'
));
expect
(
menuItemTapCounters
,
<
int
>[
0
,
0
,
1
,
0
]);
// Tap dropdown button again.
await
tester
.
tap
(
find
.
text
(
'three'
));
await
tester
.
pumpAndSettle
();
// Should not change.
expect
(
value
,
equals
(
'three'
));
expect
(
menuItemTapCounters
,
<
int
>[
0
,
0
,
1
,
0
]);
// Tap dropdown menu item.
await
tester
.
tap
(
find
.
text
(
'two'
).
last
);
await
tester
.
pumpAndSettle
();
// Should update the counter for the second item (first index).
expect
(
value
,
equals
(
'two'
));
expect
(
menuItemTapCounters
,
<
int
>[
0
,
1
,
1
,
0
]);
// Tap dropdown button again.
await
tester
.
tap
(
find
.
text
(
'two'
));
await
tester
.
pumpAndSettle
();
// Should not change.
expect
(
value
,
equals
(
'two'
));
expect
(
menuItemTapCounters
,
<
int
>[
0
,
1
,
1
,
0
]);
// Tap the already selected menu item
await
tester
.
tap
(
find
.
text
(
'two'
).
last
);
await
tester
.
pumpAndSettle
();
// Should update the counter for the second item (first index), even
// though it was already selected.
expect
(
value
,
equals
(
'two'
));
expect
(
menuItemTapCounters
,
<
int
>[
0
,
2
,
1
,
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