木鱼配置存储
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user