diff options
| author | Akshay Nair <phenax5@gmail.com> | 2020-06-03 14:39:52 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2020-06-03 14:39:52 +0530 |
| commit | 871706b606c77eeac13bfcea67c3ce1d71203d2c (patch) | |
| tree | 82d3ca8b51cc001606f5412a20730a813b7d9e2d /lib | |
| parent | c745b90dfdc97a2e04582051b01fde83883d8e3e (diff) | |
| download | daft-launcher-871706b606c77eeac13bfcea67c3ce1d71203d2c.tar.gz daft-launcher-871706b606c77eeac13bfcea67c3ce1d71203d2c.zip | |
Adds shared preferences for config/theme storage + refactors a streamstate
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/components/SearchableAppList.dart | 6 | ||||
| -rw-r--r-- | lib/data/config.dart | 51 | ||||
| -rw-r--r-- | lib/helpers/StreamState.dart | 31 | ||||
| -rw-r--r-- | lib/main.dart | 52 | ||||
| -rw-r--r-- | lib/pages/Home.dart | 9 |
5 files changed, 144 insertions, 5 deletions
diff --git a/lib/components/SearchableAppList.dart b/lib/components/SearchableAppList.dart index 8e0d818..f41ba18 100644 --- a/lib/components/SearchableAppList.dart +++ b/lib/components/SearchableAppList.dart @@ -21,6 +21,8 @@ class _SearchableAppListState extends State<SearchableAppList> { @override Widget build(BuildContext ctx) { + ThemeData theme = Theme.of(context); + return Column( children: [ Container( @@ -28,7 +30,9 @@ class _SearchableAppListState extends State<SearchableAppList> { child: TextField( onChanged: onInput, decoration: InputDecoration( - border: InputBorder.none, + prefixStyle: TextStyle(color: Color(0xFF888888)), + prefix: Text('/'), + hintStyle: TextStyle(color: Color(0xFFD8DEE9)), hintText: 'Search', ), ), diff --git a/lib/data/config.dart b/lib/data/config.dart new file mode 100644 index 0000000..0dd33cd --- /dev/null +++ b/lib/data/config.dart @@ -0,0 +1,51 @@ +import 'package:shared_preferences/shared_preferences.dart'; +import 'dart:async'; + +String defaultTheme = 'dark'; + +class Config { + String theme = defaultTheme; + Config({ this.theme }) {} + + bool isDarkMode() { + return theme == 'dark'; + } +} + +StreamController<Config> config_$ = StreamController<Config>.broadcast(); + +Future<Config> getConfig() async { + final prefs = await SharedPreferences.getInstance(); + + return Config( + theme: prefs.getString('theme'), + ); +} + +void initConfig() async { + Config c = await getConfig(); + config_$.add(c); +} + +Stream<Config> getConfig$() { + return config_$.stream; +} + +void setConfig(Config config) async { + final prefs = await SharedPreferences.getInstance(); + + prefs.setString('theme', config.theme); + + config_$.add(await getConfig()); +} + +void toggleTheme() async { + final config = await getConfig(); + + if (config.theme == 'dark') { + setConfig(Config(theme: 'light')); + } else { + setConfig(Config(theme: 'dark')); + } +} + diff --git a/lib/helpers/StreamState.dart b/lib/helpers/StreamState.dart new file mode 100644 index 0000000..3e329ed --- /dev/null +++ b/lib/helpers/StreamState.dart @@ -0,0 +1,31 @@ +import 'dart:async'; +import 'package:flutter/material.dart'; + +class StreamStateValue<Value> { + Stream<Value> stream$; + Value value; + + StreamStateValue({ this.stream$, this.value }) {} + + StreamSubscription initialize(void Function(void Function()) setState) { + return stream$.listen((Value v) { + setState(() { value = v; }); + }); + } +} + +abstract class StreamState<T extends StatefulWidget> extends State<T> { + List<StreamSubscription> subs = <StreamSubscription>[]; + + void initStateValue<V>(StreamStateValue<V> streamVal) { + subs.add(streamVal.initialize(setState)); + } + + @override + void dispose() { + super.dispose(); + for(StreamSubscription sub in subs) { + sub.cancel(); + } + } +} diff --git a/lib/main.dart b/lib/main.dart index 2ad0fb9..40aa069 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,23 +4,67 @@ import 'package:flutter/material.dart'; import 'pages/Home.dart'; import 'pages/Apps.dart'; +import 'data/config.dart'; +import 'helpers/StreamState.dart'; + void main() { runApp(MyApp()); } -class MyApp extends StatelessWidget { - final Stream<DateTime> time$ = Stream.periodic(Duration(seconds: 1), (_x) => DateTime.now()).asBroadcastStream(); +class MyAppState extends StreamState<MyApp> { + // Streams + StreamStateValue<Config> config = StreamStateValue<Config>(stream$: getConfig$(), value: Config()); + final Stream<DateTime> time$ = + Stream.periodic(Duration(seconds: 1), (_x) => DateTime.now()).asBroadcastStream(); + + void initState() { + super.initState(); + initStateValue(config); + } @override void dispose() { + super.dispose(); time$.drain(); } + ThemeData getLightTheme() { + return ThemeData(brightness: Brightness.light); + } + + ThemeData getDarkTheme() { + Color fg = Color(0xFFD8DEE9); + return ThemeData( + backgroundColor: Color(0xFF0F1215), + scaffoldBackgroundColor: Color(0xFF0F1215), + accentColor: fg, + primaryColor: Color(0xFF5E81AC), + textTheme: TextTheme( + headline1: TextStyle(color: fg), + headline2: TextStyle(color: fg), + headline3: TextStyle(color: fg), + headline4: TextStyle(color: fg), + headline5: TextStyle(color: fg), + headline6: TextStyle(color: fg), + subtitle1: TextStyle(color: fg), + subtitle2: TextStyle(color: fg), + bodyText1: TextStyle(color: fg), + bodyText2: TextStyle(color: fg), + button: TextStyle(color: fg), + caption: TextStyle(color: fg), + overline: TextStyle(color: fg), + ), + ); + } + // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'owyn launcher', + theme: getLightTheme(), + darkTheme: getDarkTheme(), + themeMode: config.value.isDarkMode() ? ThemeMode.dark : ThemeMode.light, home: Scaffold( body: PageView( controller: PageController(keepPage: true), @@ -33,3 +77,7 @@ class MyApp extends StatelessWidget { ); } } + +class MyApp extends StatefulWidget { + MyAppState createState() => MyAppState(); +} diff --git a/lib/pages/Home.dart b/lib/pages/Home.dart index 1f4b473..8efa86e 100644 --- a/lib/pages/Home.dart +++ b/lib/pages/Home.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '../components/FixedContainer.dart'; +import '../data/config.dart'; class StatusInfoCard extends StatelessWidget { Stream<DateTime> time$; @@ -10,7 +11,7 @@ class StatusInfoCard extends StatelessWidget { StatusInfoCard(this.time$, { this.defaultTime }): super(); - final timeFormat = DateFormat('h:m a'); // H fr 24 hrs + final timeFormat = DateFormat('h:mm a'); // H fr 24 hrs final dateFormat = DateFormat('EEEE, d MMM'); @override @@ -42,12 +43,16 @@ class StatusInfoCard extends StatelessWidget { fontWeight: FontWeight.w300, ), ), + FlatButton( + child: Text('Theme'), + onPressed: () { toggleTheme(); } + ), ] ); } return Container( - height: 80, + height: 300, padding: EdgeInsets.symmetric(vertical: 8.0), child: Align(alignment: Alignment.topLeft, child: child), ); |
