以一种非常简单的方式,您可以使用SharedPreferences 插件来永久存储您的问题的答案,例如
您可以存储一个“question”键,其值为“世界上有多少个国家?”(可选)。
您还存储了一个值为“324”(世界上国家的确切数量)的“answer”键
然后您创建一个“answer_found”键,该键将是一个布尔值,如果用户正确回答问题,则会更新。
然后在应用启动时,首先查询“answer_found”键,看其值是否为True。
如果该值为True,则不显示调查问卷页面,如果为false或null,则显示调查问卷页面。
当用户输入答案时,只需将他的答案与首选项中“answer”键中包含的答案进行比较。如果正确,只需更新键“answer_found”即可。在相反的情况下什么都不做(或你想要的)
更新:
如您所问,这是代码的摘录。
我让它尽可能简单(尽管它有点野蛮),以便您可以尽可能地理解该机制,并且您可以根据需要对其进行调整。
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SharedPreferences.getInstance().then((preferences) async {
//Optional key (and can put want you want)
//Store the question in preferences
await preferences.setString('question', 'how many countries are there in the world?');
//Store the answer
await preferences.setInt('answer', 324);
});
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool? questionAnswered;
@override
void initState() {
super.initState();
_getQuestionState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: home,
);
}
Widget get home {
if (questionAnswered == null) {
return const Scaffold(body: Center(child: CircularProgressIndicator()));
} else if (questionAnswered!) {
return const HomePage();
} else {
return const QuestionPage();
}
}
Future<void> _getQuestionState() async {
final preferences = await SharedPreferences.getInstance();
//obtaining the present value of 'answer_found' to know if the question has been answered (with a correct answer)
final isQuestionAnswered = preferences.getBool('answer_found') ??
false; //if the value is null, we set it to false (to avoid a NullException)
setState(() => questionAnswered = isQuestionAnswered);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: const Center(
child: Text('WELCOME'),
),
);
}
class QuestionPage extends StatefulWidget {
const QuestionPage({Key? key}) : super(key: key);
@override
State<QuestionPage> createState() => _QuestionPageState();
}
class _QuestionPageState extends State<QuestionPage> {
late final TextEditingController _answerTextController;
@override
void initState() {
super.initState();
_answerTextController = TextEditingController();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const Text('how many countries are there in the world?'),
TextField(
controller: _answerTextController,
decoration: const InputDecoration(hintText: 'Enter answer'),
),
ElevatedButton(
onPressed: () async {
//Convert the user's response to be in an integer type (because we want to make a comparison with an integer)
//The user's response will be null, if the user has not entered an integer
final userAnswer = int.tryParse(_answerTextController.text);
if (userAnswer != null) {
final preferences = await SharedPreferences.getInstance();
final storedAnswer = preferences.getInt('answer')!;
if (userAnswer == storedAnswer) {
preferences.setBool('answer_found', true);
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const HomePage()));
} else {
//Notify user that his answer was wrong or do some stuff (what you want)
}
} else {
//Do some stuff
}
},
child: const Text('Validate')),
],
),
),
);
}
}
我从未使用过flutter_secure_storage插件,但是按照我为您制作的代码sn-p,我认为您可以将其重新调整为使用flutter_secure存储,推理和逻辑是相同的。
P.S:您不必同时使用 shared_preferences 和 flutter_secure_storage。你可以简单地使用flutter_secure_storage,它与shared_preferences不同,它为你提供了一个安全的存储空间(正如它的名字所示),你只需要实现相同的逻辑。