猜数字配置存储
This commit is contained in:
@@ -2,6 +2,7 @@ import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_first_station/storage/sp_storage.dart';
|
||||
import 'guess_app_bar.dart';
|
||||
import 'result_notice.dart';
|
||||
|
||||
@@ -23,11 +24,21 @@ class _GuessPageState extends State<GuessPage> with SingleTickerProviderStateMix
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
);
|
||||
_initConfig();
|
||||
}
|
||||
|
||||
void _initConfig() async{
|
||||
Map<String,dynamic> config = await SpStorage.instance.readGuessConfig();
|
||||
_guessing = config['guessing']??false;
|
||||
_value = config['value']??0;
|
||||
setState(() {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
int _value = 0;
|
||||
|
||||
Random _random = Random();
|
||||
final Random _random = Random();
|
||||
bool _guessing = false;
|
||||
bool? _isBig;
|
||||
|
||||
@@ -43,6 +54,7 @@ class _GuessPageState extends State<GuessPage> with SingleTickerProviderStateMix
|
||||
setState(() {
|
||||
_guessing = true;
|
||||
_value = _random.nextInt(100);
|
||||
SpStorage.instance.saveGuessConfig(guessing: _guessing,value: _value);
|
||||
print(_value);
|
||||
});
|
||||
}
|
||||
@@ -60,6 +72,7 @@ class _GuessPageState extends State<GuessPage> with SingleTickerProviderStateMix
|
||||
setState(() {
|
||||
_isBig = null;
|
||||
_guessing = false;
|
||||
SpStorage.instance.saveGuessConfig(guessing: _guessing,value: _value);
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -116,5 +129,8 @@ class _GuessPageState extends State<GuessPage> with SingleTickerProviderStateMix
|
||||
}
|
||||
|
||||
@override
|
||||
// TODO: implement wantKeepAlive
|
||||
bool get wantKeepAlive => true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
39
lib/storage/sp_storage.dart
Normal file
39
lib/storage/sp_storage.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
const String kGuessSpKey = 'guess-config';
|
||||
|
||||
class SpStorage {
|
||||
SpStorage._();
|
||||
|
||||
static SpStorage? _storage;
|
||||
|
||||
static SpStorage get instance {
|
||||
_storage = _storage ?? SpStorage._();
|
||||
return _storage!;
|
||||
}
|
||||
|
||||
SharedPreferences? _sp;
|
||||
|
||||
Future<void> initSpWhenNull() async {
|
||||
if (_sp != null) return;
|
||||
_sp = _sp ?? await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
Future<bool> saveGuessConfig({
|
||||
bool? guessing,
|
||||
int? value,
|
||||
}) async {
|
||||
await initSpWhenNull();
|
||||
String content = json.encode({'guessing': guessing, 'value': value});
|
||||
return _sp!.setString(kGuessSpKey, content);
|
||||
}
|
||||
|
||||
Future<Map<String,dynamic>> readGuessConfig() async {
|
||||
await initSpWhenNull();
|
||||
String content = _sp!.getString(kGuessSpKey)??"{}";
|
||||
return json.decode(content);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user