Skip to content

Commit f6de80a

Browse files
authored
⚡ Improve text delegate (#235)
1 parent 6bb3df3 commit f6de80a

File tree

8 files changed

+104
-67
lines changed

8 files changed

+104
-67
lines changed

example/lib/constants/picker_method.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ class PickMethod {
8181
specialItemPosition: SpecialItemPosition.prepend,
8282
specialItemBuilder: (BuildContext context) {
8383
return Semantics(
84-
label: AssetsPickerTextDelegate().sActionUseCameraHint,
84+
label: AssetPickerTextDelegate().sActionUseCameraHint,
8585
button: true,
86-
onTapHint: AssetsPickerTextDelegate().sActionUseCameraHint,
86+
onTapHint: AssetPickerTextDelegate().sActionUseCameraHint,
8787
child: GestureDetector(
8888
behavior: HitTestBehavior.opaque,
8989
onTap: () async {
@@ -122,9 +122,9 @@ class PickMethod {
122122
specialItemPosition: SpecialItemPosition.prepend,
123123
specialItemBuilder: (BuildContext context) {
124124
return Semantics(
125-
label: AssetsPickerTextDelegate().sActionUseCameraHint,
125+
label: AssetPickerTextDelegate().sActionUseCameraHint,
126126
button: true,
127-
onTapHint: AssetsPickerTextDelegate().sActionUseCameraHint,
127+
onTapHint: AssetPickerTextDelegate().sActionUseCameraHint,
128128
child: GestureDetector(
129129
behavior: HitTestBehavior.opaque,
130130
onTap: () async {
@@ -292,7 +292,7 @@ class PickMethod {
292292
context,
293293
maxAssets: maxAssetsCount,
294294
selectedAssets: assets,
295-
textDelegate: EnglishTextDelegate(),
295+
textDelegate: EnglishAssetPickerTextDelegate(),
296296
);
297297
},
298298
);

example/lib/customs/pickers/directory_file_asset_picker.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ class _DirectoryFileAssetPickerState extends State<DirectoryFileAssetPicker>
4545

4646
ThemeData get currentTheme => Theme.of(context);
4747

48-
Future<void> callPicker() async {
48+
Future<void> callPicker(BuildContext context) async {
4949
final FileAssetPickerProvider provider = FileAssetPickerProvider(
5050
selectedAssets: fileList,
5151
);
5252
final FileAssetPickerBuilder builder = FileAssetPickerBuilder(
5353
provider: provider,
54+
locale: Localizations.maybeLocaleOf(context),
5455
);
5556
final List<File>? result = await AssetPicker.pickAssetsWithDelegate(
5657
context,
@@ -279,7 +280,7 @@ class _DirectoryFileAssetPickerState extends State<DirectoryFileAssetPicker>
279280
'Put files into the path to see how this custom picker work.',
280281
),
281282
TextButton(
282-
onPressed: callPicker,
283+
onPressed: () => callPicker(context),
283284
child: const Text(
284285
'🎁 Call the Picker',
285286
style: TextStyle(fontSize: 22),
@@ -377,7 +378,12 @@ class FileAssetPickerBuilder
377378
extends AssetPickerBuilderDelegate<File, Directory> {
378379
FileAssetPickerBuilder({
379380
required FileAssetPickerProvider provider,
380-
}) : super(provider: provider, initialPermission: PermissionState.authorized);
381+
Locale? locale,
382+
}) : super(
383+
provider: provider,
384+
initialPermission: PermissionState.authorized,
385+
locale: locale,
386+
);
381387

382388
Duration get switchingPathDuration => kThemeAnimationDuration * 1.5;
383389

lib/src/constants/constants.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import 'dart:developer';
77
import 'package:flutter/foundation.dart';
88
import 'package:flutter/widgets.dart';
99

10-
import '../delegates/assets_picker_text_delegate.dart';
10+
import '../delegates/asset_picker_text_delegate.dart';
1111
import '../delegates/sort_path_delegate.dart';
1212

1313
class Constants {
1414
const Constants._();
1515

1616
static GlobalKey pickerKey = GlobalKey();
1717

18-
static AssetsPickerTextDelegate textDelegate = AssetsPickerTextDelegate();
18+
static AssetPickerTextDelegate textDelegate = AssetPickerTextDelegate();
1919
static SortPathDelegate<dynamic> sortPathDelegate = SortPathDelegate.common;
2020

2121
/// The last scroll position where the picker scrolled.

lib/src/delegates/asset_picker_builder_delegate.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import '../constants/colors.dart';
2020
import '../constants/constants.dart';
2121
import '../constants/enums.dart';
2222
import '../constants/extensions.dart';
23-
import '../delegates/assets_picker_text_delegate.dart';
23+
import '../delegates/asset_picker_text_delegate.dart';
2424
import '../provider/asset_picker_provider.dart';
2525
import '../widget/asset_picker.dart';
2626
import '../widget/asset_picker_viewer.dart';
@@ -57,8 +57,6 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
5757
required this.provider,
5858
required this.initialPermission,
5959
this.gridCount = 4,
60-
Color? themeColor,
61-
AssetsPickerTextDelegate? textDelegate,
6260
this.pickerTheme,
6361
this.specialItemPosition = SpecialItemPosition.none,
6462
this.specialItemBuilder,
@@ -67,13 +65,17 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
6765
this.keepScrollOffset = false,
6866
this.selectPredicate,
6967
this.shouldRevertGrid,
68+
Color? themeColor,
69+
AssetPickerTextDelegate? textDelegate,
70+
Locale? locale,
7071
}) : assert(
7172
pickerTheme == null || themeColor == null,
7273
'Theme and theme color cannot be set at the same time.',
7374
),
7475
themeColor =
7576
pickerTheme?.colorScheme.secondary ?? themeColor ?? C.themeColor {
76-
Constants.textDelegate = textDelegate ?? AssetsPickerTextDelegate();
77+
Constants.textDelegate =
78+
textDelegate ?? assetPickerTextDelegateFromLocale(locale);
7779
// Add the listener if [keepScrollOffset] is true.
7880
if (keepScrollOffset) {
7981
gridScrollController.addListener(keepScrollOffsetListener);
@@ -212,7 +214,7 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
212214

213215
bool get effectiveShouldRevertGrid => shouldRevertGrid ?? isAppleOS;
214216

215-
AssetsPickerTextDelegate get textDelegate => Constants.textDelegate;
217+
AssetPickerTextDelegate get textDelegate => Constants.textDelegate;
216218

217219
/// The listener to track the scroll position of the [gridScrollController]
218220
/// if [keepScrollOffset] is true.
@@ -685,8 +687,6 @@ class DefaultAssetPickerBuilderDelegate
685687
required DefaultAssetPickerProvider provider,
686688
required PermissionState initialPermission,
687689
int gridCount = 4,
688-
Color? themeColor,
689-
AssetsPickerTextDelegate? textDelegate,
690690
ThemeData? pickerTheme,
691691
SpecialItemPosition specialItemPosition = SpecialItemPosition.none,
692692
WidgetBuilder? specialItemBuilder,
@@ -698,6 +698,9 @@ class DefaultAssetPickerBuilderDelegate
698698
this.gridThumbSize = Constants.defaultGridThumbSize,
699699
this.previewThumbSize,
700700
this.specialPickerType,
701+
Color? themeColor,
702+
AssetPickerTextDelegate? textDelegate,
703+
Locale? locale,
701704
}) : assert(
702705
pickerTheme == null || themeColor == null,
703706
'Theme and theme color cannot be set at the same time.',
@@ -706,8 +709,6 @@ class DefaultAssetPickerBuilderDelegate
706709
provider: provider,
707710
initialPermission: initialPermission,
708711
gridCount: gridCount,
709-
themeColor: themeColor,
710-
textDelegate: textDelegate,
711712
pickerTheme: pickerTheme,
712713
specialItemPosition: specialItemPosition,
713714
specialItemBuilder: specialItemBuilder,
@@ -716,6 +717,9 @@ class DefaultAssetPickerBuilderDelegate
716717
keepScrollOffset: keepScrollOffset,
717718
selectPredicate: selectPredicate,
718719
shouldRevertGrid: shouldRevertGrid,
720+
themeColor: themeColor,
721+
textDelegate: textDelegate,
722+
locale: locale,
719723
);
720724

721725
/// Thumbnail size in the grid.

0 commit comments

Comments
 (0)