ente/mobile/lib/ui/settings/theme_switch_widget.dart

97 lines
2.8 KiB
Dart
Raw Normal View History

2022-03-09 05:57:27 +00:00
import 'package:adaptive_theme/adaptive_theme.dart';
2022-03-09 02:48:41 +00:00
import 'package:flutter/material.dart';
import 'package:photos/ente_theme_data.dart';
import "package:photos/generated/l10n.dart";
2022-10-21 13:37:57 +00:00
import 'package:photos/theme/ente_theme.dart';
import 'package:photos/ui/components/captioned_text_widget.dart';
import 'package:photos/ui/components/expandable_menu_item_widget.dart';
2023-01-31 12:21:57 +00:00
import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart';
import 'package:photos/ui/settings/common_settings.dart';
2022-03-09 02:48:41 +00:00
class ThemeSwitchWidget extends StatefulWidget {
const ThemeSwitchWidget({Key? key}) : super(key: key);
2022-03-09 02:48:41 +00:00
@override
State<ThemeSwitchWidget> createState() => _ThemeSwitchWidgetState();
}
class _ThemeSwitchWidgetState extends State<ThemeSwitchWidget> {
AdaptiveThemeMode? currentThemeMode;
@override
void initState() {
super.initState();
AdaptiveTheme.getThemeMode().then(
(value) {
2022-10-04 11:48:12 +00:00
currentThemeMode = value ?? AdaptiveThemeMode.system;
debugPrint('theme value $value');
if (mounted) {
setState(() => {});
}
},
);
}
@override
void dispose() {
super.dispose();
}
2022-03-09 02:48:41 +00:00
@override
Widget build(BuildContext context) {
return ExpandableMenuItemWidget(
title: S.of(context).theme,
selectionOptionsWidget: _getSectionOptions(context),
leadingIcon: Theme.of(context).brightness == Brightness.light
? Icons.light_mode_outlined
: Icons.dark_mode_outlined,
);
}
Widget _getSectionOptions(BuildContext context) {
return Column(
children: [
sectionOptionSpacing,
2022-10-04 11:48:12 +00:00
_menuItem(context, AdaptiveThemeMode.light),
sectionOptionSpacing,
2022-10-04 11:48:12 +00:00
_menuItem(context, AdaptiveThemeMode.dark),
sectionOptionSpacing,
2022-10-04 11:48:12 +00:00
_menuItem(context, AdaptiveThemeMode.system),
sectionOptionSpacing,
],
2022-03-09 02:48:41 +00:00
);
}
2022-10-04 11:48:12 +00:00
Widget _menuItem(BuildContext context, AdaptiveThemeMode themeMode) {
late String themeName;
switch (themeMode) {
case AdaptiveThemeMode.light:
themeName = S.of(context).lightTheme;
break;
case AdaptiveThemeMode.dark:
themeName = S.of(context).darkTheme;
break;
case AdaptiveThemeMode.system:
themeName = S.of(context).systemTheme;
break;
}
2022-10-04 11:48:12 +00:00
return MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: themeName,
2022-10-04 11:48:12 +00:00
textStyle: Theme.of(context).colorScheme.enteTheme.textTheme.body,
),
2022-10-21 13:37:57 +00:00
pressedColor: getEnteColorScheme(context).fillFaint,
2022-10-22 06:26:47 +00:00
isExpandable: false,
2022-10-04 11:48:12 +00:00
trailingIcon: currentThemeMode == themeMode ? Icons.check : null,
trailingExtraMargin: 4,
2022-10-04 11:48:12 +00:00
onTap: () async {
AdaptiveTheme.of(context).setThemeMode(themeMode);
currentThemeMode = themeMode;
if (mounted) {
setState(() {});
}
},
);
}
2022-03-09 02:48:41 +00:00
}