diff --git a/lib/guess/guess_page.dart b/lib/guess/guess_page.dart index 44d7508..fc5722f 100644 --- a/lib/guess/guess_page.dart +++ b/lib/guess/guess_page.dart @@ -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 with SingleTickerProviderStateMix vsync: this, duration: const Duration(milliseconds: 200), ); + _initConfig(); + } + + void _initConfig() async{ + Map 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 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 with SingleTickerProviderStateMix setState(() { _isBig = null; _guessing = false; + SpStorage.instance.saveGuessConfig(guessing: _guessing,value: _value); }); return; } @@ -116,5 +129,8 @@ class _GuessPageState extends State with SingleTickerProviderStateMix } @override + // TODO: implement wantKeepAlive bool get wantKeepAlive => true; + + } diff --git a/lib/storage/sp_storage.dart b/lib/storage/sp_storage.dart new file mode 100644 index 0000000..6610e10 --- /dev/null +++ b/lib/storage/sp_storage.dart @@ -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 initSpWhenNull() async { + if (_sp != null) return; + _sp = _sp ?? await SharedPreferences.getInstance(); + } + + Future saveGuessConfig({ + bool? guessing, + int? value, + }) async { + await initSpWhenNull(); + String content = json.encode({'guessing': guessing, 'value': value}); + return _sp!.setString(kGuessSpKey, content); + } + + Future> readGuessConfig() async { + await initSpWhenNull(); + String content = _sp!.getString(kGuessSpKey)??"{}"; + return json.decode(content); + } + +} diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index a9f2f23..3165782 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -7,8 +7,10 @@ import Foundation import audioplayers_darwin import path_provider_foundation +import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index 0989dde..e04c2bd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -328,6 +328,62 @@ packages: url: "https://pub.flutter-io.cn" source: hosted 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: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 7b4d9e5..0428f38 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,6 +33,7 @@ dependencies: flame_audio: ^2.0.2 uuid: ^3.0.7 intl: ^0.18.1 + shared_preferences: ^2.1.1 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2