Skip to content

Commit 09493d0

Browse files
authored
⚡️ Add appBarPreferredSize (#469)
Fixes #468.
1 parent e4bf136 commit 09493d0

18 files changed

+194
-131
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ that can be found in the LICENSE file. -->
66

77
See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.
88

9+
## 8.6.2
10+
11+
### Improvements
12+
13+
- Add `appBarPreferredSize` in the picker delegate to help with padding calculations.
14+
- Improve the performance with `MediaQuery` callers.
15+
916
## 8.6.1
1017

1118
### Improvements

example/lib/constants/extensions.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ extension BuildContextExtension on BuildContext {
99
AppLocalizations get l10n => AppLocalizations.of(this)!;
1010
}
1111

12+
extension BrightnessExtension on Brightness {
13+
bool get isDark => this == Brightness.dark;
14+
15+
bool get isLight => this == Brightness.light;
16+
17+
Brightness get reverse =>
18+
this == Brightness.light ? Brightness.dark : Brightness.light;
19+
}
20+
1221
extension ColorExtension on Color {
1322
MaterialColor get swatch => Colors.primaries.firstWhere(
1423
(Color c) => c.value == value,

example/lib/customs/pickers/directory_file_asset_picker.dart

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class _DirectoryFileAssetPickerState extends State<DirectoryFileAssetPicker> {
6565
}
6666
}
6767

68-
Widget get selectedAssetsWidget {
68+
Widget selectedAssetsWidget(BuildContext context) {
6969
return AnimatedContainer(
7070
duration: kThemeChangeDuration,
7171
curve: Curves.easeInOut,
@@ -118,20 +118,21 @@ class _DirectoryFileAssetPickerState extends State<DirectoryFileAssetPicker> {
118118
),
119119
),
120120
),
121-
selectedAssetsListView,
121+
selectedAssetsListView(context),
122122
],
123123
),
124124
);
125125
}
126126

127-
Widget get selectedAssetsListView {
127+
Widget selectedAssetsListView(BuildContext context) {
128128
return Expanded(
129129
child: ListView.builder(
130+
shrinkWrap: true,
130131
physics: const BouncingScrollPhysics(),
131132
padding: const EdgeInsets.symmetric(horizontal: 8.0),
132133
scrollDirection: Axis.horizontal,
133134
itemCount: fileList.length,
134-
itemBuilder: (BuildContext _, int index) {
135+
itemBuilder: (_, int index) {
135136
return Padding(
136137
padding: const EdgeInsets.symmetric(
137138
horizontal: 8.0,
@@ -269,7 +270,7 @@ class _DirectoryFileAssetPickerState extends State<DirectoryFileAssetPicker> {
269270
),
270271
),
271272
),
272-
selectedAssetsWidget,
273+
selectedAssetsWidget(context),
273274
],
274275
),
275276
);
@@ -290,17 +291,15 @@ class FileAssetPickerProvider extends AssetPickerProvider<File, Directory> {
290291
Future<void> getPaths() async {
291292
currentAssets = <File>[];
292293
paths.clear();
293-
final Directory? directory = await getExternalStorageDirectory();
294-
if (directory != null) {
295-
final PathWrapper<Directory> wrapper = PathWrapper<Directory>(
296-
path: directory,
297-
thumbnailData: await getThumbnailFromPath(
298-
PathWrapper<Directory>(path: directory),
299-
),
300-
);
301-
paths.add(wrapper);
302-
currentPath = wrapper;
303-
}
294+
final Directory directory = await getApplicationDocumentsDirectory();
295+
final PathWrapper<Directory> wrapper = PathWrapper<Directory>(
296+
path: directory,
297+
thumbnailData: await getThumbnailFromPath(
298+
PathWrapper<Directory>(path: directory),
299+
),
300+
);
301+
paths.add(wrapper);
302+
currentPath = wrapper;
304303
}
305304

306305
@override
@@ -501,7 +500,7 @@ class FileAssetPickerBuilder
501500

502501
@override
503502
PreferredSizeWidget appBar(BuildContext context) {
504-
return AppBar(
503+
final AppBar appBar = AppBar(
505504
backgroundColor: theme.appBarTheme.backgroundColor,
506505
centerTitle: isAppleOS(context),
507506
title: pathEntitySelector(context),
@@ -513,6 +512,8 @@ class FileAssetPickerBuilder
513512
]
514513
: null,
515514
);
515+
appBarPreferredSize ??= appBar.preferredSize;
516+
return appBar;
516517
}
517518

518519
@override
@@ -555,7 +556,7 @@ class FileAssetPickerBuilder
555556
},
556557
),
557558
),
558-
appBar(context),
559+
Positioned.fill(bottom: null, child: appBar(context)),
559560
],
560561
);
561562
}
@@ -585,6 +586,7 @@ class FileAssetPickerBuilder
585586

586587
@override
587588
Widget assetsGridBuilder(BuildContext context) {
589+
appBarPreferredSize ??= appBar(context).preferredSize;
588590
int totalCount = provider.currentAssets.length;
589591
if (specialItemPosition != SpecialItemPosition.none) {
590592
totalCount += 1;
@@ -598,7 +600,7 @@ class FileAssetPickerBuilder
598600
final int row = (totalCount + placeholderCount) ~/ gridCount;
599601
final double dividedSpacing = itemSpacing / gridCount;
600602
final double topPadding =
601-
MediaQuery.of(context).padding.top + kToolbarHeight;
603+
MediaQuery.paddingOf(context).top + appBarPreferredSize!.height;
602604

603605
Widget sliverGrid(BuildContext ctx, List<File> assets) {
604606
return SliverGrid(
@@ -670,16 +672,16 @@ class FileAssetPickerBuilder
670672
if (isAppleOS(context))
671673
SliverToBoxAdapter(
672674
child: SizedBox(
673-
height:
674-
MediaQuery.of(context).padding.top + kToolbarHeight,
675+
height: MediaQuery.paddingOf(context).top +
676+
appBarPreferredSize!.height,
675677
),
676678
),
677679
sliverGrid(_, assets),
678680
// Ignore the gap when the [anchor] is not equal to 1.
679681
if (isAppleOS(context) && anchor == 1)
680682
SliverToBoxAdapter(
681683
child: SizedBox(
682-
height: MediaQuery.of(context).padding.bottom +
684+
height: MediaQuery.paddingOf(context).bottom +
683685
bottomSectionHeight,
684686
),
685687
),
@@ -822,8 +824,9 @@ class FileAssetPickerBuilder
822824

823825
@override
824826
Widget pathEntityListWidget(BuildContext context) {
827+
appBarPreferredSize ??= appBar(context).preferredSize;
825828
final double appBarHeight =
826-
kToolbarHeight + MediaQuery.paddingOf(context).top;
829+
appBarPreferredSize!.height + MediaQuery.paddingOf(context).top;
827830
final double maxHeight = MediaQuery.sizeOf(context).height * 0.825;
828831
return ValueListenableBuilder<bool>(
829832
valueListenable: isSwitchingPath,
@@ -1412,28 +1415,30 @@ class FileAssetPickerViewerBuilderDelegate
14121415
return Theme(
14131416
data: themeData,
14141417
child: AnnotatedRegion<SystemUiOverlayStyle>(
1415-
value: themeData.brightness == Brightness.dark
1418+
value: themeData.brightness.reverse == Brightness.dark
14161419
? SystemUiOverlayStyle.light
14171420
: SystemUiOverlayStyle.dark,
1418-
child: Material(
1419-
color: Colors.black,
1420-
child: Stack(
1421-
children: <Widget>[
1422-
Positioned.fill(
1423-
child: PageView.builder(
1424-
physics: const BouncingScrollPhysics(),
1425-
controller: _pageController,
1426-
itemCount: previewAssets.length,
1427-
itemBuilder: assetPageBuilder,
1428-
onPageChanged: (int index) {
1429-
currentIndex = index;
1430-
pageStreamController.add(index);
1431-
},
1421+
child: Builder(
1422+
builder: (BuildContext context) => Material(
1423+
color: Colors.black,
1424+
child: Stack(
1425+
children: <Widget>[
1426+
Positioned.fill(
1427+
child: PageView.builder(
1428+
physics: const BouncingScrollPhysics(),
1429+
controller: _pageController,
1430+
itemCount: previewAssets.length,
1431+
itemBuilder: assetPageBuilder,
1432+
onPageChanged: (int index) {
1433+
currentIndex = index;
1434+
pageStreamController.add(index);
1435+
},
1436+
),
14321437
),
1433-
),
1434-
appBar(context),
1435-
if (selectedAssets != null) bottomDetailBuilder(context),
1436-
],
1438+
appBar(context),
1439+
if (selectedAssets != null) bottomDetailBuilder(context),
1440+
],
1441+
),
14371442
),
14381443
),
14391444
),

0 commit comments

Comments
 (0)