猜数字配置存储
This commit is contained in:
@@ -2,6 +2,7 @@ import 'dart:math';
|
|||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:flutter_first_station/storage/sp_storage.dart';
|
||||||
import 'guess_app_bar.dart';
|
import 'guess_app_bar.dart';
|
||||||
import 'result_notice.dart';
|
import 'result_notice.dart';
|
||||||
|
|
||||||
@@ -23,11 +24,21 @@ class _GuessPageState extends State<GuessPage> with SingleTickerProviderStateMix
|
|||||||
vsync: this,
|
vsync: this,
|
||||||
duration: const Duration(milliseconds: 200),
|
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;
|
int _value = 0;
|
||||||
|
|
||||||
Random _random = Random();
|
final Random _random = Random();
|
||||||
bool _guessing = false;
|
bool _guessing = false;
|
||||||
bool? _isBig;
|
bool? _isBig;
|
||||||
|
|
||||||
@@ -43,6 +54,7 @@ class _GuessPageState extends State<GuessPage> with SingleTickerProviderStateMix
|
|||||||
setState(() {
|
setState(() {
|
||||||
_guessing = true;
|
_guessing = true;
|
||||||
_value = _random.nextInt(100);
|
_value = _random.nextInt(100);
|
||||||
|
SpStorage.instance.saveGuessConfig(guessing: _guessing,value: _value);
|
||||||
print(_value);
|
print(_value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -60,6 +72,7 @@ class _GuessPageState extends State<GuessPage> with SingleTickerProviderStateMix
|
|||||||
setState(() {
|
setState(() {
|
||||||
_isBig = null;
|
_isBig = null;
|
||||||
_guessing = false;
|
_guessing = false;
|
||||||
|
SpStorage.instance.saveGuessConfig(guessing: _guessing,value: _value);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -116,5 +129,8 @@ class _GuessPageState extends State<GuessPage> with SingleTickerProviderStateMix
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
// TODO: implement wantKeepAlive
|
||||||
bool get wantKeepAlive => true;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,8 +7,10 @@ import Foundation
|
|||||||
|
|
||||||
import audioplayers_darwin
|
import audioplayers_darwin
|
||||||
import path_provider_foundation
|
import path_provider_foundation
|
||||||
|
import shared_preferences_foundation
|
||||||
|
|
||||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||||
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
|
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
|
||||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||||
|
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||||
}
|
}
|
||||||
|
|||||||
56
pubspec.lock
56
pubspec.lock
@@ -328,6 +328,62 @@ packages:
|
|||||||
url: "https://pub.flutter-io.cn"
|
url: "https://pub.flutter-io.cn"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.2.4"
|
version: "4.2.4"
|
||||||
|
shared_preferences:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: shared_preferences
|
||||||
|
sha256: "16d3fb6b3692ad244a695c0183fca18cf81fd4b821664394a781de42386bf022"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.1"
|
||||||
|
shared_preferences_android:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shared_preferences_android
|
||||||
|
sha256: "6478c6bbbecfe9aced34c483171e90d7c078f5883558b30ec3163cf18402c749"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.4"
|
||||||
|
shared_preferences_foundation:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shared_preferences_foundation
|
||||||
|
sha256: e014107bb79d6d3297196f4f2d0db54b5d1f85b8ea8ff63b8e8b391a02700feb
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "2.2.2"
|
||||||
|
shared_preferences_linux:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shared_preferences_linux
|
||||||
|
sha256: "9d387433ca65717bbf1be88f4d5bb18f10508917a8fa2fb02e0fd0d7479a9afa"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "2.2.0"
|
||||||
|
shared_preferences_platform_interface:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shared_preferences_platform_interface
|
||||||
|
sha256: fb5cf25c0235df2d0640ac1b1174f6466bd311f621574997ac59018a6664548d
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "2.2.0"
|
||||||
|
shared_preferences_web:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shared_preferences_web
|
||||||
|
sha256: "74083203a8eae241e0de4a0d597dbedab3b8fef5563f33cf3c12d7e93c655ca5"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.0"
|
||||||
|
shared_preferences_windows:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shared_preferences_windows
|
||||||
|
sha256: "5e588e2efef56916a3b229c3bfe81e6a525665a454519ca51dbcc4236a274173"
|
||||||
|
url: "https://pub.flutter-io.cn"
|
||||||
|
source: hosted
|
||||||
|
version: "2.2.0"
|
||||||
sky_engine:
|
sky_engine:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description: flutter
|
description: flutter
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ dependencies:
|
|||||||
flame_audio: ^2.0.2
|
flame_audio: ^2.0.2
|
||||||
uuid: ^3.0.7
|
uuid: ^3.0.7
|
||||||
intl: ^0.18.1
|
intl: ^0.18.1
|
||||||
|
shared_preferences: ^2.1.1
|
||||||
# The following adds the Cupertino Icons font to your application.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
cupertino_icons: ^1.0.2
|
cupertino_icons: ^1.0.2
|
||||||
|
|||||||
Reference in New Issue
Block a user