feat: save brightness settings
This commit is contained in:
parent
cd38d96a91
commit
dd093bc7f7
|
@ -144,7 +144,7 @@ class _HomeState extends State<Home> {
|
|||
final theme = Provider.of<ThemeModel>(context);
|
||||
|
||||
final themData = ThemeData(
|
||||
brightness: theme.brightness,
|
||||
brightness: theme.brightnessEnum,
|
||||
primaryColor: Colors.white,
|
||||
);
|
||||
|
||||
|
@ -162,7 +162,7 @@ class _HomeState extends State<Home> {
|
|||
case AppThemeType.cupertino:
|
||||
return CupertinoApp(
|
||||
theme: CupertinoThemeData(
|
||||
brightness: theme.brightness,
|
||||
brightness: theme.brightnessEnum,
|
||||
primaryColor: theme.palette.primary,
|
||||
),
|
||||
home: CupertinoTabScaffold(
|
||||
|
|
|
@ -20,6 +20,17 @@ class AppThemeType {
|
|||
static const values = [AppThemeType.material, AppThemeType.cupertino];
|
||||
}
|
||||
|
||||
class AppBrightnessType {
|
||||
// static const followSystem = 0;
|
||||
static const light = 1;
|
||||
static const dark = 2;
|
||||
static const values = [
|
||||
// AppBrightnessType.followSystem,
|
||||
AppBrightnessType.light,
|
||||
AppBrightnessType.dark
|
||||
];
|
||||
}
|
||||
|
||||
class PickerItem<T> {
|
||||
final T value;
|
||||
final String text;
|
||||
|
@ -86,25 +97,38 @@ class Palette {
|
|||
}
|
||||
|
||||
class ThemeModel with ChangeNotifier {
|
||||
static const storageKey = 'theme';
|
||||
static const kTheme = 'theme';
|
||||
static const kBrightness = 'brightness';
|
||||
|
||||
int _theme;
|
||||
int get theme => _theme;
|
||||
bool get ready => _theme != null;
|
||||
|
||||
Brightness _brightness = Brightness.light;
|
||||
Brightness get brightness => _brightness;
|
||||
Future<void> toggleBrightness() async {
|
||||
// TODO: Save
|
||||
_brightness =
|
||||
_brightness == Brightness.dark ? Brightness.light : Brightness.dark;
|
||||
int _brightnessValue = AppBrightnessType.light;
|
||||
int get brighnessValue => _brightnessValue;
|
||||
Brightness get brightnessEnum {
|
||||
switch (_brightnessValue) {
|
||||
case AppBrightnessType.light:
|
||||
return Brightness.light;
|
||||
case AppBrightnessType.dark:
|
||||
return Brightness.dark;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setBrightness(int v) async {
|
||||
_brightnessValue = v;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(kBrightness, v);
|
||||
Fimber.d('write brightness: $v');
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
final router = Router();
|
||||
|
||||
Palette get palette {
|
||||
switch (brightness) {
|
||||
switch (brightnessEnum) {
|
||||
case Brightness.light:
|
||||
return Palette(
|
||||
primary: Colors.blueAccent.shade700,
|
||||
|
@ -131,9 +155,8 @@ class ThemeModel with ChangeNotifier {
|
|||
}
|
||||
|
||||
Future<void> init() async {
|
||||
var prefs = await SharedPreferences.getInstance();
|
||||
|
||||
int v = prefs.getInt(storageKey);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final v = prefs.getInt(kTheme);
|
||||
Fimber.d('read theme: $v');
|
||||
if (AppThemeType.values.contains(v)) {
|
||||
_theme = v;
|
||||
|
@ -142,17 +165,20 @@ class ThemeModel with ChangeNotifier {
|
|||
} else {
|
||||
_theme = AppThemeType.material;
|
||||
}
|
||||
final b = prefs.getInt(kBrightness);
|
||||
Fimber.d('read brightness: $b');
|
||||
if (AppBrightnessType.values.contains(b)) {
|
||||
_brightnessValue = b;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setTheme(int v) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
|
||||
_theme = v;
|
||||
await prefs.setInt(storageKey, v);
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(kTheme, v);
|
||||
Fimber.d('write theme: $v');
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ class CommonScaffold extends StatelessWidget {
|
|||
default:
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
brightness: theme.brightness,
|
||||
brightness: theme.brightnessEnum,
|
||||
title: title,
|
||||
actions: [
|
||||
if (action != null) action,
|
||||
|
|
|
@ -53,17 +53,17 @@ class NewsScreenState extends State<NewsScreen> {
|
|||
Widget build(context) {
|
||||
return ListStatefulScaffold<GithubEvent, int>(
|
||||
title: AppBarTitle('News'),
|
||||
actionBuilder: () {
|
||||
return ActionEntry(
|
||||
iconData:
|
||||
Provider.of<ThemeModel>(context).brightness == Brightness.dark
|
||||
? Ionicons.md_sunny
|
||||
: Ionicons.md_moon,
|
||||
onTap: () {
|
||||
Provider.of<ThemeModel>(context).toggleBrightness();
|
||||
},
|
||||
);
|
||||
},
|
||||
// actionBuilder: () {
|
||||
// return ActionEntry(
|
||||
// iconData:
|
||||
// Provider.of<ThemeModel>(context).brightness == Brightness.dark
|
||||
// ? Ionicons.md_sunny
|
||||
// : Ionicons.md_moon,
|
||||
// onTap: () {
|
||||
// Provider.of<ThemeModel>(context).toggleBrightness();
|
||||
// },
|
||||
// );
|
||||
// },
|
||||
itemBuilder: (payload) => EventItem(payload),
|
||||
onRefresh: fetchEvents,
|
||||
onLoadMore: (page) => fetchEvents(page),
|
||||
|
|
|
@ -169,7 +169,7 @@ class ObjectScreen extends StatelessWidget {
|
|||
child: HighlightView(
|
||||
text,
|
||||
language: _language,
|
||||
theme: themeMap[theme.brightness == Brightness.dark
|
||||
theme: themeMap[theme.brightnessEnum == Brightness.dark
|
||||
? codeProvider.themeDark
|
||||
: codeProvider.theme],
|
||||
padding: CommonStyle.padding,
|
||||
|
|
|
@ -4,8 +4,9 @@ import 'package:git_touch/models/theme.dart';
|
|||
import 'package:git_touch/scaffolds/single.dart';
|
||||
import 'package:git_touch/utils/utils.dart';
|
||||
import 'package:git_touch/widgets/app_bar_title.dart';
|
||||
import 'package:git_touch/widgets/table_view.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../widgets/table_view.dart';
|
||||
import 'package:tuple/tuple.dart';
|
||||
|
||||
final settingsRouter = RouterScreen(
|
||||
'/settings',
|
||||
|
@ -13,15 +14,15 @@ final settingsRouter = RouterScreen(
|
|||
);
|
||||
|
||||
class SettingsScreen extends StatelessWidget {
|
||||
Widget _buildRightWidget(bool checked) {
|
||||
Widget _buildRightWidget(BuildContext context, bool checked) {
|
||||
final theme = Provider.of<ThemeModel>(context);
|
||||
if (!checked) return null;
|
||||
return Icon(Octicons.check, color: CupertinoColors.activeBlue, size: 24);
|
||||
return Icon(Icons.check, color: theme.palette.primary, size: 24);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var themeProvider = Provider.of<ThemeModel>(context);
|
||||
|
||||
final theme = Provider.of<ThemeModel>(context);
|
||||
return SingleScaffold(
|
||||
title: AppBarTitle('Settings'),
|
||||
body: Column(
|
||||
|
@ -40,29 +41,45 @@ class SettingsScreen extends StatelessWidget {
|
|||
TableView(
|
||||
headerText: 'APP THEME',
|
||||
items: [
|
||||
TableViewItem(
|
||||
Tuple2('Material', AppThemeType.material),
|
||||
Tuple2('Cupertino', AppThemeType.cupertino),
|
||||
].map((t) {
|
||||
return TableViewItem(
|
||||
text: Text('Material'),
|
||||
rightWidget: _buildRightWidget(
|
||||
themeProvider.theme == AppThemeType.material),
|
||||
context,
|
||||
theme.theme == AppThemeType.material,
|
||||
),
|
||||
onTap: () {
|
||||
if (themeProvider.theme != AppThemeType.material) {
|
||||
themeProvider.setTheme(AppThemeType.material);
|
||||
if (theme.theme != AppThemeType.material) {
|
||||
theme.setTheme(AppThemeType.material);
|
||||
}
|
||||
},
|
||||
hideRightChevron: true,
|
||||
),
|
||||
TableViewItem(
|
||||
text: Text('Cupertino'),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
CommonStyle.verticalGap,
|
||||
TableView(
|
||||
headerText: 'BRIGHTNESS',
|
||||
items: [
|
||||
// Tuple2('Follow System', AppBrightnessType.followSystem),
|
||||
Tuple2('Light', AppBrightnessType.light),
|
||||
Tuple2('Dark', AppBrightnessType.dark),
|
||||
].map((t) {
|
||||
return TableViewItem(
|
||||
text: Text(t.item1),
|
||||
rightWidget: _buildRightWidget(
|
||||
themeProvider.theme == AppThemeType.cupertino),
|
||||
context,
|
||||
theme.brighnessValue == t.item2,
|
||||
),
|
||||
onTap: () {
|
||||
if (themeProvider.theme != AppThemeType.cupertino) {
|
||||
themeProvider.setTheme(AppThemeType.cupertino);
|
||||
}
|
||||
if (theme.brighnessValue != t.item2)
|
||||
theme.setBrightness(t.item2);
|
||||
},
|
||||
hideRightChevron: true,
|
||||
),
|
||||
],
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
CommonStyle.verticalGap,
|
||||
TableView(
|
||||
|
|
|
@ -230,7 +230,7 @@ class UserScreen extends StatelessWidget {
|
|||
spacing: 3,
|
||||
children: week.contributionDays.map((day) {
|
||||
var color = convertColor(day.color);
|
||||
if (theme.brightness == Brightness.dark) {
|
||||
if (theme.brightnessEnum == Brightness.dark) {
|
||||
color = Color.fromRGBO(0xff - color.red,
|
||||
0xff - color.green, 0xff - color.blue, 1);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:git_touch/models/theme.dart';
|
||||
import 'package:git_touch/widgets/link.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class AvatarSize {
|
||||
|
@ -37,7 +38,7 @@ class Avatar extends StatelessWidget {
|
|||
),
|
||||
);
|
||||
if (linkUrl == null) return widget;
|
||||
return GestureDetector(
|
||||
return Link(
|
||||
child: widget,
|
||||
onTap: () {
|
||||
Provider.of<ThemeModel>(context).push(context, linkUrl);
|
||||
|
|
Loading…
Reference in New Issue