Add persistent storage
This commit is contained in:
parent
fef9733ad7
commit
d6fb9caa63
|
@ -1,3 +1,4 @@
|
|||
import 'package:get_it/get_it.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tangocard_reader/router.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
@ -6,25 +7,24 @@ import 'service/theme_bloc.dart';
|
|||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
runApp(MyApp(prefs: prefs));
|
||||
GetIt.instance.registerSingleton<SharedPreferences>(
|
||||
await SharedPreferences.getInstance());
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
final SharedPreferences prefs;
|
||||
|
||||
const MyApp({
|
||||
Key? key,
|
||||
required this.prefs,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => BlocProvider(
|
||||
create: (context) => ThemeBloc(
|
||||
prefs: prefs,
|
||||
init: (prefs.getBool('darkTheme') ?? false)
|
||||
? Brightness.dark
|
||||
: Brightness.light),
|
||||
init:
|
||||
(GetIt.instance.get<SharedPreferences>().getBool('darkTheme') ??
|
||||
false)
|
||||
? Brightness.dark
|
||||
: Brightness.light),
|
||||
child: BlocBuilder<ThemeBloc, Brightness>(
|
||||
builder: (context, state) => MaterialApp(
|
||||
title: 'Tangocard Reader',
|
||||
|
|
|
@ -76,6 +76,19 @@ class _KanjiPageState extends State<KanjiPage> {
|
|||
),
|
||||
const SizedBox(width: 20),
|
||||
const Divider(thickness: 5),
|
||||
if (widget.showDrawingPanel) ...[
|
||||
const SizedBox(width: 20),
|
||||
Row(
|
||||
children: [
|
||||
const Expanded(child: SizedBox()),
|
||||
IconButton(
|
||||
iconSize: 40,
|
||||
onPressed: controller.clear,
|
||||
icon: const Icon(Icons.delete),
|
||||
)
|
||||
],
|
||||
)
|
||||
]
|
||||
],
|
||||
),
|
||||
Positioned(
|
||||
|
|
|
@ -2,6 +2,8 @@ import 'dart:math';
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:tangocard_reader/models/data_entry.dart';
|
||||
import 'package:tangocard_reader/screens/practise/kanji.dart';
|
||||
|
||||
|
@ -32,13 +34,22 @@ const encouragingWords = [
|
|||
|
||||
class _PractiseViewState extends State<PractiseView> {
|
||||
late int currentCard;
|
||||
final List<bool> _flashcardToggles = [false, false];
|
||||
final List<bool> _kanjiToggles = [false, false];
|
||||
|
||||
bool get isShuffleMode => _flashcardToggles[0];
|
||||
bool get isLanguageSwitchedMode => _flashcardToggles[1];
|
||||
bool get isKanjiDrawingMode => _kanjiToggles[0];
|
||||
bool get isKanjiAnimationMode => _kanjiToggles[1];
|
||||
SharedPreferences get prefs => GetIt.instance.get<SharedPreferences>();
|
||||
|
||||
set isShuffleMode(b) => prefs.setBool('shuffleMode', b);
|
||||
set isLanguageSwitchedMode(b) => prefs.setBool('languageSwitchedMode', b);
|
||||
set isKanjiDrawingMode(b) => prefs.setBool('kanjiDrawingMode', b);
|
||||
set isKanjiAnimationMode(b) => prefs.setBool('kanjiAnimationMode', b);
|
||||
|
||||
bool get isShuffleMode => prefs.getBool('shuffleMode') ?? false;
|
||||
bool get isLanguageSwitchedMode =>
|
||||
prefs.getBool('languageSwitchedMode') ?? false;
|
||||
bool get isKanjiDrawingMode => prefs.getBool('kanjiDrawingMode') ?? false;
|
||||
bool get isKanjiAnimationMode => prefs.getBool('kanjiAnimationMode') ?? false;
|
||||
|
||||
List<bool> get _flashcardToggles => [isShuffleMode, isLanguageSwitchedMode];
|
||||
List<bool> get _kanjiToggles => [isKanjiDrawingMode, isKanjiAnimationMode];
|
||||
|
||||
int get randomCard => Random().nextInt(widget.entries.length);
|
||||
String get randomEncouragingWord =>
|
||||
|
@ -98,8 +109,15 @@ class _PractiseViewState extends State<PractiseView> {
|
|||
Icon(Icons.translate),
|
||||
],
|
||||
isSelected: _flashcardToggles,
|
||||
onPressed: (int index) =>
|
||||
setState(() => _flashcardToggles[index] = !_flashcardToggles[index])),
|
||||
onPressed: (int index) => setState(
|
||||
() {
|
||||
if (index == 0) {
|
||||
isShuffleMode = !_flashcardToggles[index];
|
||||
} else if (index == 1) {
|
||||
isLanguageSwitchedMode = !_flashcardToggles[index];
|
||||
}
|
||||
},
|
||||
)),
|
||||
],
|
||||
),
|
||||
centerTitle: true,
|
||||
|
@ -116,14 +134,22 @@ class _PractiseViewState extends State<PractiseView> {
|
|||
icon: const Icon(Icons.repeat),
|
||||
),
|
||||
ToggleButtons(
|
||||
selectedColor: Colors.white,
|
||||
children: const [
|
||||
Icon(Icons.edit),
|
||||
Icon(Icons.animation),
|
||||
],
|
||||
isSelected: _kanjiToggles,
|
||||
onPressed: (int index) =>
|
||||
setState(() => _kanjiToggles[index] = !_kanjiToggles[index])),
|
||||
selectedColor: Colors.white,
|
||||
children: const [
|
||||
Icon(Icons.edit),
|
||||
Icon(Icons.animation),
|
||||
],
|
||||
isSelected: _kanjiToggles,
|
||||
onPressed: (int index) => setState(
|
||||
() {
|
||||
if (index == 0) {
|
||||
isKanjiDrawingMode = !_flashcardToggles[index];
|
||||
} else if (index == 1) {
|
||||
isKanjiAnimationMode = !_flashcardToggles[index];
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
centerTitle: true,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
|
@ -12,14 +13,13 @@ class SetTheme {
|
|||
}
|
||||
|
||||
class ThemeBloc extends Bloc<SetTheme, Brightness> {
|
||||
final SharedPreferences prefs;
|
||||
|
||||
ThemeBloc({
|
||||
required this.prefs,
|
||||
required Brightness init,
|
||||
}) : super(init) {
|
||||
on<SetTheme>((event, emit) {
|
||||
prefs.setBool('darkTheme', event.isDark);
|
||||
GetIt.instance
|
||||
.get<SharedPreferences>()
|
||||
.setBool('darkTheme', event.isDark);
|
||||
emit(event.isDark ? Brightness.dark : Brightness.light);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -114,6 +114,13 @@ packages:
|
|||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
get_it:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: get_it
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "7.2.0"
|
||||
image:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
@ -30,6 +30,7 @@ dependencies:
|
|||
flutter:
|
||||
sdk: flutter
|
||||
flutter_bloc: ^8.0.1
|
||||
get_it: ^7.2.0
|
||||
shared_preferences: ^2.0.12
|
||||
signature: ^5.0.0
|
||||
|
||||
|
|
Loading…
Reference in New Issue