Skip to content

Commit de0fa14

Browse files
authored
修复 下拉菜单 有时无法正常关闭的bug
1 parent 8d6f33c commit de0fa14

File tree

1 file changed

+79
-64
lines changed

1 file changed

+79
-64
lines changed

lib/pages/popular/popular_page.dart

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ class _PopularPageState extends State<PopularPage>
3030
final MenuController _menuController = MenuController();
3131

3232
bool _showBackToTop = false;
33-
3433
bool _isDropdownOpen = false;
3534

35+
bool _ignoreNextTap = false;
36+
3637
@override
3738
bool get wantKeepAlive => true;
3839

@@ -239,78 +240,92 @@ class _PopularPageState extends State<PopularPage>
239240

240241
return MouseRegion(
241242
cursor: SystemMouseCursors.click,
242-
child: GestureDetector(
243-
behavior: HitTestBehavior.opaque,
244-
onTap: () {
243+
child: Listener(
244+
onPointerDown: (_) {
245245
if (_menuController.isOpen) {
246-
_menuController.close();
246+
_ignoreNextTap = true;
247247
} else {
248-
_menuController.open();
248+
_ignoreNextTap = false;
249249
}
250250
},
251-
child: Row(
252-
mainAxisSize: MainAxisSize.min,
253-
children: [
254-
Text(
255-
isTrend
256-
? '热门番组'
257-
: popularController.currentTag,
258-
style: theme.textTheme.headlineMedium!
259-
.copyWith(
260-
fontWeight: fontWeight,
261-
fontSize: fontSize,
262-
),
263-
),
264-
const SizedBox(width: 4),
251+
child: GestureDetector(
252+
behavior: HitTestBehavior.opaque,
253+
onTap: () {
254+
if (_ignoreNextTap) {
255+
_ignoreNextTap = false;
265256

266-
MenuAnchor(
267-
controller: _menuController,
268-
alignmentOffset: const Offset(0, 0),
269-
style: MenuStyle(
270-
maximumSize: WidgetStateProperty.all(const Size(200, 400)),
271-
visualDensity: VisualDensity.compact,
257+
if (_menuController.isOpen) {
258+
_menuController.close();
259+
}
260+
return;
261+
}
262+
// 正常打开
263+
_menuController.open();
264+
},
265+
child: Row(
266+
mainAxisSize: MainAxisSize.min,
267+
children: [
268+
Text(
269+
isTrend
270+
? '热门番组'
271+
: popularController.currentTag,
272+
style: theme.textTheme.headlineMedium!
273+
.copyWith(
274+
fontWeight: fontWeight,
275+
fontSize: fontSize,
276+
),
272277
),
273-
onOpen: () {
274-
if (mounted) setState(() => _isDropdownOpen = true);
275-
},
276-
onClose: () {
277-
if (mounted) setState(() => _isDropdownOpen = false);
278-
},
279-
builder: (context, controller, child) {
280-
return AnimatedRotation(
281-
turns: _isDropdownOpen ? 0.5 : 0.0,
282-
duration: const Duration(milliseconds: 200),
283-
child: Icon(Icons.keyboard_arrow_down,
284-
size: fontSize,
285-
color: theme.iconTheme.color),
286-
);
287-
},
288-
menuChildren: [
289-
'',
290-
...defaultAnimeTags
291-
].map((tag) {
292-
final isSelected =
293-
tag == popularController.currentTag;
294-
final primaryColor = theme.colorScheme.primary;
278+
const SizedBox(width: 4),
295279

296-
return MenuItemButton(
297-
onPressed: () => _handleTagSelection(tag),
298-
child: Container(
299-
constraints:
300-
const BoxConstraints(minWidth: 100),
301-
child: Text(
302-
tag.isEmpty ? '热门番组' : tag,
303-
style: TextStyle(
304-
color: isSelected ? primaryColor : null,
305-
fontWeight:
306-
isSelected ? FontWeight.bold : null,
280+
MenuAnchor(
281+
controller: _menuController,
282+
alignmentOffset: const Offset(0, 0),
283+
style: MenuStyle(
284+
maximumSize: WidgetStateProperty.all(const Size(200, 400)),
285+
visualDensity: VisualDensity.compact,
286+
),
287+
onOpen: () {
288+
if (mounted) setState(() => _isDropdownOpen = true);
289+
},
290+
onClose: () {
291+
if (mounted) setState(() => _isDropdownOpen = false);
292+
},
293+
builder: (context, controller, child) {
294+
return AnimatedRotation(
295+
turns: _isDropdownOpen ? 0.5 : 0.0,
296+
duration: const Duration(milliseconds: 200),
297+
child: Icon(Icons.keyboard_arrow_down,
298+
size: fontSize,
299+
color: theme.iconTheme.color),
300+
);
301+
},
302+
menuChildren: [
303+
'',
304+
...defaultAnimeTags
305+
].map((tag) {
306+
final isSelected =
307+
tag == popularController.currentTag;
308+
final primaryColor = theme.colorScheme.primary;
309+
310+
return MenuItemButton(
311+
onPressed: () => _handleTagSelection(tag),
312+
child: Container(
313+
constraints:
314+
const BoxConstraints(minWidth: 100),
315+
child: Text(
316+
tag.isEmpty ? '热门番组' : tag,
317+
style: TextStyle(
318+
color: isSelected ? primaryColor : null,
319+
fontWeight:
320+
isSelected ? FontWeight.bold : null,
321+
),
307322
),
308323
),
309-
),
310-
);
311-
}).toList(),
312-
),
313-
],
324+
);
325+
}).toList(),
326+
),
327+
],
328+
),
314329
),
315330
),
316331
);

0 commit comments

Comments
 (0)