Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

fix: fix dropdown menu flick in popular page (#1322)

Predidit22e6fc267b26
diff --git a/lib/bean/widget/custom_dropdown_menu.dart b/lib/bean/widget/custom_dropdown_menu.dart
new file mode 100644
index 00000000..d3b93ddc
--- /dev/null
+++ b/lib/bean/widget/custom_dropdown_menu.dart
@@ -0,0 +1,114 @@
+import 'package:flutter/material.dart';
+
+/// A custom dropdown menu widget that provides smooth animations without flickering.
+///
+/// This widget was created to solve the visual flickering issue in Flutter's built-in
+/// [PopupMenuButton] where menu items are rendered before the animation completes,
+/// causing an uncoordinated visual effect.
+class CustomDropdownMenu extends StatelessWidget {
+ final Offset offset;
+ final Size buttonSize;
+ final Animation<double> animation;
+ final List<String> items;
+ final String Function(String) itemBuilder;
+ final double? maxHeight;
+ /// Minimum width constraint for the menu. Defaults to 140.
+ /// Note: If [maxWidth] is less than [minWidth], [minWidth] will be used as both min and max.
+ final double? minWidth;
+ /// Maximum width constraint for the menu. Defaults to 200.
+ /// Note: If this value is less than [minWidth], it will be adjusted to equal [minWidth].
+ final double? maxWidth;
+ final double gap;
+
+ const CustomDropdownMenu({
+ super.key,
+ required this.offset,
+ required this.buttonSize,
+ required this.animation,
+ required this.items,
+ required this.itemBuilder,
+ this.maxHeight,
+ this.minWidth,
+ this.maxWidth,
+ this.gap = 4,
+ });
+
+ @override
+ Widget build(BuildContext context) {
+ final theme = Theme.of(context);
+
+ // Ensure width constraints are valid (minWidth <= maxWidth)
+ final computedMinWidth = minWidth ?? 140;
+ final computedMaxWidth = maxWidth ?? 200;
+ final normalizedMinWidth = computedMinWidth;
+ final normalizedMaxWidth = computedMaxWidth < computedMinWidth
+ ? computedMinWidth
+ : computedMaxWidth;
+
+ return GestureDetector(
+ onTap: () => Navigator.pop(context),
+ behavior: HitTestBehavior.opaque,
+ child: Stack(
+ children: [
+ Positioned(
+ left: offset.dx,
+ top: offset.dy + buttonSize.height + gap,
+ child: Material(
+ elevation: 6,
+ borderRadius: BorderRadius.circular(8),
+ color: theme.colorScheme.surface,
+ surfaceTintColor: Colors.transparent,
+ shadowColor: Colors.black26,
+ child: AnimatedBuilder(
+ animation: animation,
+ builder: (context, child) {
+ final curvedValue =
+ Curves.easeOutCubic.transform(animation.value);
+ return ClipRect(
+ child: Align(
+ alignment: Alignment.topCenter,
+ heightFactor: curvedValue,
+ child: Opacity(
+ opacity: curvedValue,
+ child: child,
+ ),
+ ),
+ );
+ },
+ child: Container(
+ constraints: BoxConstraints(
+ maxHeight: maxHeight ?? 350,
+ minWidth: normalizedMinWidth,
+ maxWidth: normalizedMaxWidth,
+ ),
+ child: ListView.builder(
+ padding: const EdgeInsets.symmetric(vertical: 8),
+ shrinkWrap: true,
+ itemCount: items.length,
+ itemBuilder: (context, index) {
+ final itemValue = items[index];
+ final displayText = itemBuilder(itemValue);
+ return InkWell(
+ onTap: () => Navigator.pop(context, itemValue),
+ child: Padding(
+ padding: const EdgeInsets.symmetric(
+ horizontal: 16,
+ vertical: 12,
+ ),
+ child: Text(
+ displayText,
+ style: const TextStyle(fontSize: 14),
+ ),
+ ),
+ );
+ },
+ ),
+ ),
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}
diff --git a/lib/pages/popular/popular_page.dart b/lib/pages/popular/popular_page.dart
index 660411e4..41f92df2 100644
--- a/lib/pages/popular/popular_page.dart
+++ b/lib/pages/popular/popular_page.dart
@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:kazumi/bean/dialog/dialog_helper.dart';
import 'package:kazumi/bean/widget/error_widget.dart';
+import 'package:kazumi/bean/widget/custom_dropdown_menu.dart';
import 'package:kazumi/pages/popular/popular_controller.dart';
import 'package:kazumi/bean/card/bangumi_card.dart';
import 'package:kazumi/utils/constants.dart';
@@ -294,31 +295,36 @@ class _PopularPageState extends State<PopularPage>
}
Future<void> showTagMenu() async {
- // we have to calculate the position of the button manually here.
- // PopupMenuButton / DropdownButton cannot be used here because they can't set different font size in the button and the menu.
+ // Calculate the position of the button manually to position the dropdown menu.
+ // Using CustomDropdownMenu instead of PopupMenuButton to avoid flickering issues
+ // and to support different font sizes in the button and menu items.
final RenderBox renderBox =
selectorKey.currentContext!.findRenderObject() as RenderBox;
final Offset offset = renderBox.localToGlobal(Offset.zero);
final Size size = renderBox.size;
- final selected = await showMenu<String>(
- context: context,
- position: RelativeRect.fromLTRB(
- offset.dx, offset.dy + size.height, offset.dx + size.width, 0),
- items: [
- const PopupMenuItem<String>(
- value: '',
- child: Text('热门番组', style: TextStyle(fontSize: 14)),
- ),
- ...defaultAnimeTags.map(
- (t) => PopupMenuItem<String>(
- value: t,
- child: Text(t, style: const TextStyle(fontSize: 14)),
- ),
- ),
- ],
- elevation: 6,
- constraints: const BoxConstraints(maxHeight: 350),
+ final selected = await Navigator.push<String>(
+ context,
+ PageRouteBuilder(
+ opaque: false,
+ barrierDismissible: true,
+ barrierColor: Colors.transparent,
+ pageBuilder: (context, animation, secondaryAnimation) {
+ return CustomDropdownMenu(
+ offset: offset,
+ buttonSize: size,
+ animation: animation,
+ maxWidth: 80,
+ items: [
+ '',
+ ...defaultAnimeTags,
+ ],
+ itemBuilder: (item) => item.isEmpty ? '热门番组' : item,
+ );
+ },
+ transitionDuration: const Duration(milliseconds: 200),
+ reverseTransitionDuration: const Duration(milliseconds: 150),
+ ),
);
if (selected == null) return;