From adac36a09199be69c989208f85636a76c724bd11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E5=A8=87?= Date: Mon, 4 Mar 2024 10:06:49 +0800 Subject: [PATCH] favor combination of ListView + Row instead of GridView --- lib/pages/android_file_picker_page.dart | 44 ++++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/pages/android_file_picker_page.dart b/lib/pages/android_file_picker_page.dart index ac4aba8..ea7bc4b 100644 --- a/lib/pages/android_file_picker_page.dart +++ b/lib/pages/android_file_picker_page.dart @@ -82,19 +82,13 @@ class _AndroidFilePickerState extends State { separatorBuilder: (_, __) => const Divider(), padding: const EdgeInsets.symmetric(horizontal: 2, vertical: 0), itemCount: entities.length, - itemBuilder: (context, index) => - itemBuilder(context, index, setStateS, entities), + itemBuilder: (context, index) => itemBuilder(context, index, setStateS, entities), ), - desktop: GridView.builder( - gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( - crossAxisCount: MediaQuery.of(context).size.width ~/ 250, // Number of columns - childAspectRatio: 4, - ), - physics: const NeverScrollableScrollPhysics(), - shrinkWrap: true, + desktop: gridBuilder( + context: contextS, + crossAxisCount: MediaQuery.of(context).size.width ~/ 250, // Number of columns itemCount: entities.length, - itemBuilder: (context, index) => - itemBuilder(context, index, setStateS, entities), + itemBuilder: (context, index) => itemBuilder(context, index, setStateS, entities), ), ), ); @@ -113,12 +107,38 @@ class _AndroidFilePickerState extends State { ); } - Widget itemBuilder( + Widget gridBuilder({ + required BuildContext context, + required int itemCount, + required int crossAxisCount, + required NullableIndexedWidgetBuilder itemBuilder, + }) { + int rows = (itemCount / crossAxisCount).ceil(); + return ListView.separated( + itemCount: rows, + shrinkWrap: true, + separatorBuilder: (_, __) => const Divider(), + itemBuilder: (context, rowIndex) => Row( + children: List.generate( + crossAxisCount, + (index) => Expanded( + child: itemBuilder.call(context, rowIndex * crossAxisCount + index) ?? Container(), + ), + ), + ), + ); + } + + Widget? itemBuilder( BuildContext context, int index, void Function(void Function()) setStateS, List entities, ) { + if (index >= entities.length) { + return null; + } + FileSystemEntity entity = entities[index]; String basename = FileManager.basename( entity,