如上图所示,用户通过点击True或者False按钮,对于上图中的题目进行批阅,
并且在最下边的横条中出现√或者×
1、首先在主文件中画出该界面的布局,和样式
由下图所示,该布局在整体的StatelessWidget 中嵌套了一个StatefulWidget
并且在安全区域中嵌套了两个Expanded widget
import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
import 'QuizBrain.dart';
QuizBrain quizBrain = QuizBrain();
void main() => runApp(Quizzler());
class Quizzler extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey.shade900,
body: SafeArea(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: QuizPage(),
),
),
),
);
}
}
class QuizPage extends StatefulWidget {
@override
_QuizPageState createState() => _QuizPageState();
}
class _QuizPageState extends State<QuizPage> {
List<Icon> scoreKeeper = [];
void checkAnswer(bool userPickedAnswer) {
bool topOrNot = quizBrain.topOrNot();
print(topOrNot);
if (topOrNot) {
Alert(
context: context,
type: AlertType.error,
title: "RFLUTTER ALERT",
desc: "You have finished the questions",
buttons: [
DialogButton(
child: Text(
"COOL",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();
}
bool correctAnswer = quizBrain.getAnswer();
setState(() {
quizBrain.getQuestion();
if (correctAnswer == userPickedAnswer) {
scoreKeeper.add(Icon(
Icons.check,
color: Colors.green,
));
} else {
scoreKeeper.add(Icon(
Icons.close,
color: Colors.red,
));
}
quizBrain.nextQuestion();
});
}
List<String> questions = [
'You can lead a cow down stairs but not up stairs.',
'Approximately one quarter of human bones are in the feet.',
'A slug\'s blood is green.'
];
List<bool> answers = [false, true, true];
int questionNumber = 0;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
quizBrain.getQuestion(),
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: FlatButton(
textColor: Colors.white,
color: Colors.green,
child: Text(
'True',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
onPressed: () {
checkAnswer(true);
},
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(15.0),
child: FlatButton(
color: Colors.red,
child: Text(
'False',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
onPressed: () {
checkAnswer(false);
},
),
),
),
Row(
children: scoreKeeper,
),
],
);
}
}
2、对于题目和分页的管理,这里使用Dart的面向对象编程
在一个Dart的类中储存,并计算分页
import 'question.dart';
class QuizBrain{
int _questionNumber = 0;
List<Question> _questionBank = [
Question(q:'Some cats are actually allergic to humans',a: true),
Question(q:'You can lead a cow down stairs but not up stairs.',a: false),
Question(q:'Approximately one quarter of human bones are in the feet.',a: true),
Question(q:'A slug\'s blood is green.',a: true),
Question(q:'Buzz Aldrin\'s mother\'s maiden name was \"Moon\".',a: true),
Question(q:'It is illegal to pee in the Ocean in Portugal.',a: true),
Question(
q:'No piece of square dry paper can be folded in half more than 7 times.',
a:false),
Question(
q:'In London, UK, if you happen to die in the House of Parliament, you are technically entitled to a state funeral, because the building is considered too sacred a place.',
a:true),
Question(
q:'The loudest sound produced by any animal is 188 decibels. That animal is the African Elephant.',
a:false),
Question(
q:'The total surface area of two human lungs is approximately 70 square metres.',
a:true),
Question(q:'Google was originally called \"Backrub\".',a: true),
Question(
q:'Chocolate affects a dog\'s heart and nervous system; a few ounces are enough to kill a small dog.',
a:true),
Question(
q:'In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.',
a:true),
];
String getQuestion(){
return _questionBank[_questionNumber].questionText;
}
bool getAnswer(){
return _questionBank[_questionNumber].questionAnswer;
}
void nextQuestion(){
if(_questionNumber<_questionBank.length-1){
_questionNumber++;
}
}
bool topOrNot(){
return _questionNumber==_questionBank.length-1;
}
void previousQuestion(){
if(_questionNumber>0){
_questionNumber--;
}
}
}
3、本文自建的po对象有
class Question{
String questionText ;
bool questionAnswer ;
Question({String q,bool a}){
questionText = q;
questionAnswer = a;
}
}
4、使用到的弹出提示框的Dart插件有
rflutter_alert: ^1.0.2
import 'package:rflutter_alert/rflutter_alert.dart';
Alert(
context: context,
type: AlertType.error,
title: "RFLUTTER ALERT",
desc: "You have finished the questions",
buttons: [
DialogButton(
child: Text(
"COOL",
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () => Navigator.pop(context),
width: 120,
)
],
).show();