木鱼配置存储

This commit is contained in:
toly
2023-05-15 20:23:09 +08:00
parent 98524f8ca1
commit fe21bfcba3
2 changed files with 51 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_first_station/muyu/animate_text.dart';
import 'package:flutter_first_station/muyu/options/select_audio.dart';
import 'package:uuid/uuid.dart';
import '../storage/sp_storage.dart';
import 'models/audio_option.dart';
import 'models/image_option.dart';
import 'models/merit_record.dart';
@@ -23,7 +24,8 @@ class MuyuPage extends StatefulWidget {
State<MuyuPage> createState() => _MuyuPageState();
}
class _MuyuPageState extends State<MuyuPage> with AutomaticKeepAliveClientMixin {
class _MuyuPageState extends State<MuyuPage>
with AutomaticKeepAliveClientMixin {
int _counter = 0;
MeritRecord? _cruRecord;
@@ -52,11 +54,22 @@ class _MuyuPageState extends State<MuyuPage> with AutomaticKeepAliveClientMixin
void initState() {
super.initState();
_initAudioPool();
_initConfig();
}
void _initConfig() async{
Map<String,dynamic> config = await SpStorage.instance.readMuYUConfig();
_counter = config['counter']??0;
_activeImageIndex = config['activeImageIndex']??0;
_activeAudioIndex = config['activeAudioIndex']??0;
setState(() {
});
}
void _initAudioPool() async {
pool = await FlameAudio.createPool(
'muyu_1.mp3',
audioOptions[_activeAudioIndex].src,
maxPlayers: 1,
);
}
@@ -84,8 +97,7 @@ class _MuyuPageState extends State<MuyuPage> with AutomaticKeepAliveClientMixin
image: activeImage,
onTap: _onKnock,
),
if (_cruRecord != null)
AnimateText(record: _cruRecord!)
if (_cruRecord != null) AnimateText(record: _cruRecord!)
],
),
),
@@ -142,11 +154,20 @@ class _MuyuPageState extends State<MuyuPage> with AutomaticKeepAliveClientMixin
audioOptions[_activeAudioIndex].name,
);
_counter += _cruRecord!.value;
saveConfig();
// 添加功德记录
_records.add(_cruRecord!);
});
}
void saveConfig() {
SpStorage.instance.saveMuYUConfig(
counter: _counter,
activeImageIndex: _activeImageIndex,
activeAudioIndex: _activeAudioIndex,
);
}
String get activeImage => imageOptions[_activeImageIndex].src;
int get knockValue {
@@ -160,6 +181,7 @@ class _MuyuPageState extends State<MuyuPage> with AutomaticKeepAliveClientMixin
if (value == _activeImageIndex) return;
setState(() {
_activeImageIndex = value;
saveConfig();
});
}
@@ -169,6 +191,7 @@ class _MuyuPageState extends State<MuyuPage> with AutomaticKeepAliveClientMixin
Navigator.of(context).pop();
if (value == _activeAudioIndex) return;
_activeAudioIndex = value;
saveConfig();
pool = await FlameAudio.createPool(
activeAudio,
maxPlayers: 1,

View File

@@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
const String kGuessSpKey = 'guess-config';
const String kMuYUSpKey = 'muyu-config';
class SpStorage {
SpStorage._();
@@ -22,18 +23,37 @@ class SpStorage {
}
Future<bool> saveGuessConfig({
bool? guessing,
int? value,
required bool guessing,
required int value,
}) async {
await initSpWhenNull();
String content = json.encode({'guessing': guessing, 'value': value});
return _sp!.setString(kGuessSpKey, content);
}
Future<Map<String,dynamic>> readGuessConfig() async {
Future<Map<String, dynamic>> readGuessConfig() async {
await initSpWhenNull();
String content = _sp!.getString(kGuessSpKey)??"{}";
String content = _sp!.getString(kGuessSpKey) ?? "{}";
return json.decode(content);
}
Future<bool> saveMuYUConfig({
required int counter,
required int activeImageIndex,
required int activeAudioIndex,
}) async {
await initSpWhenNull();
String content = json.encode({
'counter': counter,
'activeImageIndex': activeImageIndex,
'activeAudioIndex': activeAudioIndex,
});
return _sp!.setString(kMuYUSpKey, content);
}
Future<Map<String, dynamic>> readMuYUConfig() async {
await initSpWhenNull();
String content = _sp!.getString(kMuYUSpKey) ?? "{}";
return json.decode(content);
}
}