Skip to content

Commit a4ed06a

Browse files
committed
⚡️ Get rid of explicit *ThemeData? constructors as much as possible
1 parent 74267bb commit a4ed06a

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

example/lib/main.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import 'package:flutter/material.dart';
66
import 'package:flutter/services.dart';
77
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
8-
import 'package:wechat_assets_picker_demo/l10n/gen/app_localizations.dart';
98

109
import 'constants/extensions.dart';
10+
import 'l10n/gen/app_localizations.dart';
1111
import 'pages/splash_page.dart';
1212

1313
const Color themeColor = Color(0xff00bc56);
@@ -28,10 +28,11 @@ class MyApp extends StatelessWidget {
2828
const MyApp({super.key});
2929

3030
ThemeData _buildTheme(Brightness brightness) {
31-
return ThemeData(
32-
brightness: brightness,
33-
primarySwatch: themeColor.swatch,
34-
textSelectionTheme: const TextSelectionThemeData(cursorColor: themeColor),
31+
return ThemeData.from(
32+
colorScheme: ColorScheme.fromSwatch(
33+
primarySwatch: themeColor.swatch,
34+
brightness: brightness,
35+
),
3536
);
3637
}
3738

lib/src/delegates/asset_picker_delegate.dart

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -245,108 +245,111 @@ class AssetPickerDelegate {
245245
ThemeData themeData(Color? themeColor, {bool light = false}) {
246246
themeColor ??= defaultThemeColorWeChat;
247247
if (light) {
248-
return ThemeData.light().copyWith(
248+
final base = ThemeData.light();
249+
return base.copyWith(
249250
primaryColor: Colors.grey[50],
250251
primaryColorLight: Colors.grey[50],
251252
primaryColorDark: Colors.grey[50],
252253
canvasColor: Colors.grey[100],
253254
scaffoldBackgroundColor: Colors.grey[50],
254255
cardColor: Colors.grey[50],
255256
highlightColor: Colors.transparent,
256-
textSelectionTheme: TextSelectionThemeData(
257+
textSelectionTheme: base.textSelectionTheme.copyWith(
257258
cursorColor: themeColor,
258259
selectionColor: themeColor.withAlpha(100),
259260
selectionHandleColor: themeColor,
260261
),
262+
// ignore: deprecated_member_use
261263
indicatorColor: themeColor,
262-
appBarTheme: AppBarTheme(
264+
appBarTheme: base.appBarTheme.copyWith(
263265
backgroundColor: Colors.grey[100],
264266
systemOverlayStyle: const SystemUiOverlayStyle(
265267
statusBarBrightness: Brightness.light,
266268
statusBarIconBrightness: Brightness.dark,
267269
),
268-
iconTheme: IconThemeData(color: Colors.grey[900]),
270+
iconTheme:
271+
base.appBarTheme.iconTheme?.copyWith(color: Colors.grey[900]) ??
272+
IconThemeData(color: Colors.grey[900]),
269273
elevation: 0,
270274
),
271-
bottomAppBarTheme: BottomAppBarTheme(
275+
bottomAppBarTheme: base.bottomAppBarTheme.copyWith(
272276
color: Colors.grey[100],
273277
),
274-
buttonTheme: ButtonThemeData(buttonColor: themeColor),
275-
iconTheme: IconThemeData(color: Colors.grey[900]),
276-
checkboxTheme: CheckboxThemeData(
277-
checkColor: MaterialStateProperty.all(Colors.black),
278-
fillColor: MaterialStateProperty.resolveWith((states) {
279-
if (states.contains(MaterialState.selected)) {
278+
buttonTheme: base.buttonTheme.copyWith(buttonColor: themeColor),
279+
iconTheme: base.iconTheme.copyWith(color: Colors.grey[900]),
280+
checkboxTheme: base.checkboxTheme.copyWith(
281+
checkColor: WidgetStateProperty.all(Colors.black),
282+
fillColor: WidgetStateProperty.resolveWith((states) {
283+
if (states.contains(WidgetState.selected)) {
280284
return themeColor;
281285
}
282286
return null;
283287
}),
284288
side: const BorderSide(color: Colors.black),
285289
),
286-
colorScheme: ColorScheme(
290+
colorScheme: base.colorScheme.copyWith(
287291
primary: Colors.grey[50]!,
288292
secondary: themeColor,
289-
background: Colors.grey[50]!,
290293
surface: Colors.grey[50]!,
291294
brightness: Brightness.light,
292295
error: const Color(0xffcf6679),
293296
onPrimary: Colors.white,
294297
onSecondary: Colors.grey[100]!,
295298
onSurface: Colors.black,
296-
onBackground: Colors.black,
297299
onError: Colors.white,
298300
),
299301
);
300302
}
301-
return ThemeData.dark().copyWith(
303+
final base = ThemeData.dark();
304+
return base.copyWith(
302305
primaryColor: Colors.grey[900],
303306
primaryColorLight: Colors.grey[900],
304307
primaryColorDark: Colors.grey[900],
305308
canvasColor: Colors.grey[850],
306309
scaffoldBackgroundColor: Colors.grey[900],
307310
cardColor: Colors.grey[900],
308311
highlightColor: Colors.transparent,
309-
textSelectionTheme: TextSelectionThemeData(
312+
textSelectionTheme: base.textSelectionTheme.copyWith(
310313
cursorColor: themeColor,
311314
selectionColor: themeColor.withAlpha(100),
312315
selectionHandleColor: themeColor,
313316
),
317+
// ignore: deprecated_member_use
314318
indicatorColor: themeColor,
315-
appBarTheme: AppBarTheme(
319+
appBarTheme: base.appBarTheme.copyWith(
316320
backgroundColor: Colors.grey[850],
317321
systemOverlayStyle: const SystemUiOverlayStyle(
318322
statusBarBrightness: Brightness.dark,
319323
statusBarIconBrightness: Brightness.light,
320324
),
321-
iconTheme: const IconThemeData(color: Colors.white),
325+
iconTheme: base.appBarTheme.iconTheme?.copyWith(color: Colors.white) ??
326+
const IconThemeData(color: Colors.white),
322327
elevation: 0,
323328
),
324-
bottomAppBarTheme: BottomAppBarTheme(
329+
bottomAppBarTheme: base.bottomAppBarTheme.copyWith(
325330
color: Colors.grey[850],
326331
),
327-
buttonTheme: ButtonThemeData(buttonColor: themeColor),
328-
iconTheme: const IconThemeData(color: Colors.white),
329-
checkboxTheme: CheckboxThemeData(
330-
checkColor: MaterialStateProperty.all(Colors.white),
331-
fillColor: MaterialStateProperty.resolveWith((states) {
332-
if (states.contains(MaterialState.selected)) {
332+
buttonTheme: base.buttonTheme.copyWith(buttonColor: themeColor),
333+
iconTheme: base.iconTheme.copyWith(color: Colors.white),
334+
checkboxTheme: base.checkboxTheme.copyWith(
335+
checkColor: WidgetStateProperty.all(Colors.white),
336+
fillColor: WidgetStateProperty.resolveWith((states) {
337+
if (states.contains(WidgetState.selected)) {
333338
return themeColor;
334339
}
335340
return null;
336341
}),
337342
side: const BorderSide(color: Colors.white),
338343
),
339-
colorScheme: ColorScheme(
344+
colorScheme: base.colorScheme.copyWith(
340345
primary: Colors.grey[900]!,
341346
secondary: themeColor,
342-
background: Colors.grey[900]!,
343347
surface: Colors.grey[900]!,
344348
brightness: Brightness.dark,
345349
error: const Color(0xffcf6679),
346350
onPrimary: Colors.black,
347351
onSecondary: Colors.grey[850]!,
348352
onSurface: Colors.white,
349-
onBackground: Colors.white,
350353
onError: Colors.black,
351354
),
352355
);

0 commit comments

Comments
 (0)