mirror of
https://github.com/foss42/apidash.git
synced 2025-05-24 01:36:46 +08:00
add persisted theme toggle to RequestEditorPaneHome
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
|
import 'package:apidash/providers/providers.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:window_size/window_size.dart' as window_size;
|
import 'package:window_size/window_size.dart' as window_size;
|
||||||
@ -39,11 +40,12 @@ void main() async {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class App extends StatelessWidget {
|
class App extends ConsumerWidget {
|
||||||
const App({Key? key}) : super(key: key);
|
const App({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
|
final theme = ref.watch(themeStateProvider);
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
@ -52,6 +54,17 @@ class App extends StatelessWidget {
|
|||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
brightness: Brightness.light,
|
brightness: Brightness.light,
|
||||||
),
|
),
|
||||||
|
darkTheme: ThemeData(
|
||||||
|
fontFamily: GoogleFonts.openSans().fontFamily,
|
||||||
|
colorSchemeSeed: Colors.blue,
|
||||||
|
useMaterial3: true,
|
||||||
|
brightness: Brightness.dark,
|
||||||
|
),
|
||||||
|
themeMode: theme != null
|
||||||
|
? theme
|
||||||
|
? ThemeMode.light
|
||||||
|
: ThemeMode.dark
|
||||||
|
: ThemeMode.system,
|
||||||
home: const HomePage(),
|
home: const HomePage(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,26 @@ final codePaneVisibleStateProvider = StateProvider<bool>((ref) => false);
|
|||||||
final saveDataStateProvider = StateProvider<bool>((ref) => false);
|
final saveDataStateProvider = StateProvider<bool>((ref) => false);
|
||||||
final clearDataStateProvider = StateProvider<bool>((ref) => false);
|
final clearDataStateProvider = StateProvider<bool>((ref) => false);
|
||||||
|
|
||||||
|
final StateNotifierProvider<ThemeStateNotifier, bool?> themeStateProvider =
|
||||||
|
StateNotifierProvider((ref) => ThemeStateNotifier());
|
||||||
|
|
||||||
|
class ThemeStateNotifier extends StateNotifier<bool?> {
|
||||||
|
ThemeStateNotifier() : super(false) {
|
||||||
|
loadData();
|
||||||
|
}
|
||||||
|
|
||||||
|
final hiveHandler = HiveHandler();
|
||||||
|
|
||||||
|
Future<void> toggle() async {
|
||||||
|
state = !state!;
|
||||||
|
await hiveHandler.setTheme(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadData() {
|
||||||
|
state = hiveHandler.getTheme() ?? false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final StateNotifierProvider<CollectionStateNotifier, List<RequestModel>?>
|
final StateNotifierProvider<CollectionStateNotifier, List<RequestModel>?>
|
||||||
collectionStateNotifierProvider =
|
collectionStateNotifierProvider =
|
||||||
StateNotifierProvider((ref) => CollectionStateNotifier());
|
StateNotifierProvider((ref) => CollectionStateNotifier());
|
||||||
|
@ -48,6 +48,7 @@ class RequestEditorPaneHome extends ConsumerWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
|
final theme = ref.watch(themeStateProvider);
|
||||||
return Padding(
|
return Padding(
|
||||||
padding: const EdgeInsets.symmetric(
|
padding: const EdgeInsets.symmetric(
|
||||||
vertical: 40,
|
vertical: 40,
|
||||||
@ -155,6 +156,48 @@ class RequestEditorPaneHome extends ConsumerWidget {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
kVSpacer10,
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Text.rich(
|
||||||
|
TextSpan(
|
||||||
|
children: [
|
||||||
|
const TextSpan(
|
||||||
|
text: "Choose your theme now: ",
|
||||||
|
),
|
||||||
|
WidgetSpan(
|
||||||
|
alignment: PlaceholderAlignment.middle,
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: () async => await ref
|
||||||
|
.read(themeStateProvider.notifier)
|
||||||
|
.toggle(),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
theme != null
|
||||||
|
? theme
|
||||||
|
? const Icon(Icons.dark_mode)
|
||||||
|
: const Icon(Icons.light_mode)
|
||||||
|
: const Icon(Icons.light_mode),
|
||||||
|
kHSpacer10,
|
||||||
|
Text.rich(
|
||||||
|
TextSpan(
|
||||||
|
text: theme != null
|
||||||
|
? theme
|
||||||
|
? "Dark"
|
||||||
|
: "Light"
|
||||||
|
: "Light",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
style: Theme.of(context).textTheme.titleMedium,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
kVSpacer20,
|
kVSpacer20,
|
||||||
kVSpacer10,
|
kVSpacer10,
|
||||||
Row(
|
Row(
|
||||||
|
@ -6,6 +6,9 @@ const String kDataBox = "data";
|
|||||||
// sequence of ids
|
// sequence of ids
|
||||||
const String kDataBoxIds = "ids";
|
const String kDataBoxIds = "ids";
|
||||||
|
|
||||||
|
// dark theme boolean
|
||||||
|
const String kDataBoxTheme = "theme";
|
||||||
|
|
||||||
Future<void> openBoxes() async {
|
Future<void> openBoxes() async {
|
||||||
await Hive.initFlutter();
|
await Hive.initFlutter();
|
||||||
await Hive.openBox(kDataBox);
|
await Hive.openBox(kDataBox);
|
||||||
@ -18,6 +21,9 @@ class HiveHandler {
|
|||||||
dataBox = Hive.box(kDataBox);
|
dataBox = Hive.box(kDataBox);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dynamic getTheme() => dataBox.get(kDataBoxTheme);
|
||||||
|
Future<void> setTheme(bool? theme) => dataBox.put(kDataBoxTheme, theme);
|
||||||
|
|
||||||
dynamic getIds() => dataBox.get(kDataBoxIds);
|
dynamic getIds() => dataBox.get(kDataBoxIds);
|
||||||
Future<void> setIds(List<String>? ids) => dataBox.put(kDataBoxIds, ids);
|
Future<void> setIds(List<String>? ids) => dataBox.put(kDataBoxIds, ids);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user