1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Add dark theme

This commit is contained in:
Pierre HUBERT 2019-11-01 13:48:40 +01:00
parent 9e151639c2
commit 6a34df6814
3 changed files with 104 additions and 8 deletions

View File

@ -9,9 +9,12 @@ import 'package:shared_preferences/shared_preferences.dart';
/// ///
/// @author Pierre HUBERT /// @author Pierre HUBERT
enum PreferencesKeyList { LOGIN_TOKENS } enum PreferencesKeyList { LOGIN_TOKENS, ENABLE_DARK_THEME }
const _PreferenceKeysName = {PreferencesKeyList.LOGIN_TOKENS: "login_tokens"}; const _PreferenceKeysName = {
PreferencesKeyList.LOGIN_TOKENS: "login_tokens",
PreferencesKeyList.ENABLE_DARK_THEME: "dark_theme",
};
class PreferencesHelper { class PreferencesHelper {
static PreferencesHelper _instance; static PreferencesHelper _instance;
@ -51,11 +54,20 @@ class PreferencesHelper {
} }
} }
Future<void> setString(PreferencesKeyList key, String value) async { Future<bool> setString(PreferencesKeyList key, String value) async {
await _sharedPreferences.setString(_PreferenceKeysName[key], value); return await _sharedPreferences.setString(_PreferenceKeysName[key], value);
} }
String getString(PreferencesKeyList key) { String getString(PreferencesKeyList key) {
return _sharedPreferences.getString(_PreferenceKeysName[key]); return _sharedPreferences.getString(_PreferenceKeysName[key]);
} }
Future<bool> setBool(PreferencesKeyList key, bool value) async {
return await _sharedPreferences.setBool(_PreferenceKeysName[key], value);
}
bool getBool(PreferencesKeyList key, {bool alternative = false}) {
final v = _sharedPreferences.getBool(_PreferenceKeysName[key]);
return v == null ? alternative : v;
}
} }

View File

@ -1,5 +1,6 @@
import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/helpers/account_helper.dart';
import 'package:comunic/helpers/database/database_helper.dart'; import 'package:comunic/helpers/database/database_helper.dart';
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/ui/routes/home_route.dart'; import 'package:comunic/ui/routes/home_route.dart';
import 'package:comunic/ui/routes/login_route.dart'; import 'package:comunic/ui/routes/login_route.dart';
import 'package:comunic/utils/ui_utils.dart'; import 'package:comunic/utils/ui_utils.dart';
@ -9,19 +10,29 @@ import 'package:flutter/material.dart';
/// ///
/// @author Pierre HUBERT /// @author Pierre HUBERT
void subMain() { void subMain() async {
// Connect to database // Connect to database
DatabaseHelper.open(); await DatabaseHelper.open();
runApp(ComunicApplication()); runApp(ComunicApplication(
darkMode: (await PreferencesHelper.getInstance())
.getBool(PreferencesKeyList.ENABLE_DARK_THEME),
));
} }
class ComunicApplication extends StatelessWidget { class ComunicApplication extends StatelessWidget {
final bool darkMode;
const ComunicApplication({Key key, @required this.darkMode})
: assert(darkMode != null),
super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
home: ComunicApplicationHome(), home: ComunicApplicationHome(),
theme: darkMode ? ThemeData.dark() : null,
); );
} }
} }

View File

@ -1,3 +1,4 @@
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -23,9 +24,81 @@ class _AppSettingsBody extends StatefulWidget {
} }
class __AppSettingsBodyState extends State<_AppSettingsBody> { class __AppSettingsBodyState extends State<_AppSettingsBody> {
PreferencesHelper _preferencesHelper;
@override
void initState() {
super.initState();
_load();
}
void _load() async {
final prefs = await PreferencesHelper.getInstance();
setState(() {
_preferencesHelper = prefs;
});
}
void _needRefresh() {
setState(() {});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ListView(children: <Widget>[],); if (_preferencesHelper == null)
return Center(child: CircularProgressIndicator());
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListView(
children: <Widget>[
// Dark theme
_CheckBoxPreference(
preferenceKey: PreferencesKeyList.ENABLE_DARK_THEME,
preferencesHelper: _preferencesHelper,
onUpdate: _needRefresh,
title: tr("Enable dark theme"),
description:
tr("You will need to restart the application to apply changes"),
),
],
),
);
} }
} }
class _CheckBoxPreference extends StatelessWidget {
final PreferencesKeyList preferenceKey;
final PreferencesHelper preferencesHelper;
final VoidCallback onUpdate;
final String title;
final String description;
const _CheckBoxPreference({
Key key,
@required this.preferenceKey,
@required this.preferencesHelper,
@required this.onUpdate,
@required this.title,
@required this.description,
}) : assert(preferencesHelper != null),
assert(preferencesHelper != null),
assert(onUpdate != null),
assert(title != null),
assert(description != null),
super(key: key);
@override
Widget build(BuildContext context) {
return SwitchListTile(
value: preferencesHelper.getBool(preferenceKey),
onChanged: (b) async {
await preferencesHelper.setBool(preferenceKey, b);
onUpdate();
return true;
},
title: Text(title),
subtitle: Text(description),
);
}
}