nc-photos/app/lib/theme.dart

214 lines
6.2 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:nc_photos/pref.dart';
2021-04-10 06:28:12 +02:00
class AppTheme extends StatelessWidget {
2021-07-23 22:05:57 +02:00
const AppTheme({
2021-09-15 08:58:06 +02:00
Key? key,
2021-07-23 22:05:57 +02:00
required this.child,
2022-07-22 18:48:10 +02:00
this.brightnessOverride,
2021-09-15 08:58:06 +02:00
}) : super(key: key);
2021-04-10 06:28:12 +02:00
2022-07-22 18:48:10 +02:00
factory AppTheme.light({
Key? key,
required Widget child,
}) =>
AppTheme(
key: key,
brightnessOverride: Brightness.light,
child: child,
);
factory AppTheme.dark({
Key? key,
required Widget child,
}) =>
AppTheme(
key: key,
brightnessOverride: Brightness.dark,
child: child,
);
2021-04-10 06:28:12 +02:00
@override
Widget build(BuildContext context) {
return Theme(
2022-07-22 18:48:10 +02:00
data: _buildThemeData(context),
child: DefaultTextStyle(
style: _buildTextStyle(context),
child: child,
),
2021-04-10 06:28:12 +02:00
);
}
2021-07-16 09:55:04 +02:00
static ThemeData buildThemeData(BuildContext context) {
final theme = Theme.of(context);
return theme.brightness == Brightness.light
2022-07-22 18:48:10 +02:00
? buildLightThemeData()
: buildDarkThemeData();
2021-04-10 06:28:12 +02:00
}
static AppBarTheme getContextualAppBarTheme(BuildContext context) {
final theme = Theme.of(context);
if (theme.brightness == Brightness.light) {
return theme.appBarTheme.copyWith(
backgroundColor: Colors.grey[800],
foregroundColor: Colors.white.withOpacity(.87),
2021-04-10 06:28:12 +02:00
);
} else {
return theme.appBarTheme.copyWith(
backgroundColor: Colors.grey[200],
foregroundColor: Colors.black87,
2021-04-10 06:28:12 +02:00
);
}
}
static Color getSelectionOverlayColor(BuildContext context) {
return Theme.of(context).brightness == Brightness.light
2021-07-23 22:05:57 +02:00
? primarySwatchLight[100]!.withOpacity(0.7)
: primarySwatchDark[700]!.withOpacity(0.7);
2021-04-10 06:28:12 +02:00
}
static Color getOverscrollIndicatorColor(BuildContext context) {
return Theme.of(context).brightness == Brightness.light
2021-07-23 22:05:57 +02:00
? Colors.grey[800]!
: Colors.grey[200]!;
2021-04-10 06:28:12 +02:00
}
static Color getRootPickerContentBoxColor(BuildContext context) {
2021-07-23 22:05:57 +02:00
return Colors.blue[200]!;
2021-04-10 06:28:12 +02:00
}
2021-04-17 10:59:16 +02:00
static Color getPrimaryTextColor(BuildContext context) {
return Theme.of(context).brightness == Brightness.light
2022-01-15 11:35:15 +01:00
? primaryTextColorLight
: primaryTextColorDark;
2021-04-17 10:59:16 +02:00
}
static Color getSecondaryTextColor(BuildContext context) {
return Theme.of(context).brightness == Brightness.light
? Colors.black.withOpacity(.6)
: Colors.white60;
}
2022-01-28 20:34:38 +01:00
static Color getPrimaryTextColorInverse(BuildContext context) =>
Theme.of(context).brightness == Brightness.light
? primaryTextColorDark
: primaryTextColorLight;
2021-04-17 11:04:46 +02:00
static Color getAppBarDarkModeSwitchColor(BuildContext context) {
return Colors.black87;
}
static Color getAppBarDarkModeSwitchTrackColor(BuildContext context) {
return Colors.white.withOpacity(.5);
}
2021-05-05 22:03:39 +02:00
static Color getListItemBackgroundColor(BuildContext context) {
return Theme.of(context).brightness == Brightness.light
? Colors.black26
: Colors.white12;
}
2021-10-17 12:16:23 +02:00
static Color getUnfocusedIconColor(BuildContext context) {
return Theme.of(context).brightness == Brightness.light
2022-07-12 22:11:27 +02:00
? unfocusedIconColorLight
: unfocusedIconColorDark;
2021-10-17 12:16:23 +02:00
}
2022-07-22 18:48:10 +02:00
static ThemeData buildLightThemeData() {
final theme = ThemeData(
brightness: Brightness.light,
primarySwatch: AppTheme.primarySwatchLight,
);
2021-07-16 09:55:04 +02:00
final appBarTheme = theme.appBarTheme.copyWith(
backgroundColor: theme.scaffoldBackgroundColor,
foregroundColor: theme.colorScheme.onSurface,
2021-07-16 09:55:04 +02:00
);
return theme.copyWith(appBarTheme: appBarTheme);
}
2022-07-22 18:48:10 +02:00
static ThemeData buildDarkThemeData() {
final Color background;
final Color popup;
2021-10-27 22:40:54 +02:00
if (Pref().isUseBlackInDarkThemeOr(false)) {
background = Colors.black;
popup = Colors.grey[900]!;
} else {
// in the material spec, black is suggested to be 0x121212, but the one
// used in flutter by default is 0x303030, why?
background = Colors.grey[850]!;
popup = Colors.grey[800]!;
}
2022-07-22 18:48:10 +02:00
final theme = ThemeData(
brightness: Brightness.dark,
primarySwatch: AppTheme.primarySwatchDark,
scaffoldBackgroundColor: background,
dialogBackgroundColor: popup,
bottomNavigationBarTheme: BottomNavigationBarThemeData(
backgroundColor: background,
),
popupMenuTheme: PopupMenuThemeData(
color: popup,
),
checkboxTheme: CheckboxThemeData(
fillColor: _CheckboxDarkColorProperty(),
),
);
2021-07-16 09:55:04 +02:00
final appBarTheme = theme.appBarTheme.copyWith(
backgroundColor: background,
foregroundColor: theme.colorScheme.onSurface,
2021-07-16 09:55:04 +02:00
);
2022-07-22 18:48:10 +02:00
return theme.copyWith(appBarTheme: appBarTheme);
}
ThemeData _buildThemeData(BuildContext context) {
return (brightnessOverride ?? Theme.of(context).brightness) ==
Brightness.light
? buildLightThemeData()
: buildDarkThemeData();
}
TextStyle _buildTextStyle(BuildContext context) {
return (brightnessOverride ?? Theme.of(context).brightness) ==
Brightness.light
? const TextStyle(color: AppTheme.primaryTextColorLight)
: TextStyle(color: AppTheme.primaryTextColorDark);
2021-07-16 09:55:04 +02:00
}
2021-04-10 06:28:12 +02:00
static const primarySwatchLight = Colors.blue;
static const primarySwatchDark = Colors.cyan;
2022-01-15 11:35:15 +01:00
static const primaryTextColorLight = Colors.black87;
static final primaryTextColorDark = Colors.white.withOpacity(.87);
2022-07-12 22:11:27 +02:00
static const unfocusedIconColorLight = Colors.black54;
static const unfocusedIconColorDark = Colors.white70;
2021-04-10 06:28:12 +02:00
static const widthLimitedContentMaxWidth = 550.0;
/// Make a TextButton look like a default FlatButton. See
/// https://flutter.dev/go/material-button-migration-guide
static final flatButtonStyle = TextButton.styleFrom(
primary: Colors.black87,
2021-09-15 08:58:06 +02:00
minimumSize: const Size(88, 36),
padding: const EdgeInsets.symmetric(horizontal: 16.0),
2021-04-10 06:28:12 +02:00
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2.0)),
),
);
final Widget child;
2022-07-22 18:48:10 +02:00
final Brightness? brightnessOverride;
2021-04-10 06:28:12 +02:00
}
2021-10-18 13:34:25 +02:00
class _CheckboxDarkColorProperty implements MaterialStateProperty<Color?> {
@override
resolve(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return Colors.cyanAccent[400];
} else {
return null;
}
}
}