我想这个问题很愚蠢,但我一天也解不出这个最简单的问题。
在类“MyHomePage”中,我有一个变量“bal”,用于计算正确答案。
import 'package:australia/qlist.dart';
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 1;
int bal = 0;
int bad = 0;
List<Color> colorsList = [
Colors.white,
Colors.white,
Colors.white,
Colors.white
];
List<String> answers = [];
@override
void initState() {
answers.addAll([
QuestionsList.shared.getCurrentQuestion().correctAnswer,
QuestionsList.shared.getCurrentQuestion().dis1,
QuestionsList.shared.getCurrentQuestion().dis2,
QuestionsList.shared.getCurrentQuestion().dis3,
]);
answers.shuffle();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text ("Exam test"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Text('$_counter'), Text('/20____'), Text('$bal'), Text('____'), Text('$bad'),
],
),
),
Center(
child: Text(QuestionsList.shared.getCurrentQuestion().question)),
],
),
Column(
children: [
Container(
width: double.infinity,
child: Card(
color: colorsList[0],
child: InkWell(
child: Container(
margin: const EdgeInsets.all(5.0),
child: Text(answers[0])),
onTap: () {
if (answers[0] ==
QuestionsList.shared.getCurrentQuestion().correctAnswer) {
colorsList[0] = Colors.green;
setState(() {
if (bad==0) {bal++;}
;
});
} else {
colorsList[0] = Colors.red;
setState(() {bad++;});
}
},
),
),),
Container(
width: double.infinity,
child: Card(
color: colorsList[1],
child: InkWell(
child: Container(
margin: const EdgeInsets.all(5.0),
child: Text(answers[1])),
onTap: () {
if (answers[1] ==
QuestionsList.shared.getCurrentQuestion().correctAnswer) {
colorsList[1] = Colors.green;
setState(() {
if (bad==0) {bal++;};
});
} else {
colorsList[1] = Colors.red;
setState(() {bad++;});
}
},
),
),),
Container(
width: double.infinity,
child: Card(
color: colorsList[2],
child: InkWell(
child: Container(
margin: const EdgeInsets.all(5.0),
child: Text(answers[2])),
onTap: () {
if (answers[2] ==
QuestionsList.shared.getCurrentQuestion().correctAnswer) {
colorsList[2] = Colors.green;
setState(() {
if (bad==0) {bal++;};
});
} else {
colorsList[2] = Colors.red;
setState(() {bad++;});
}
},
),
),),
Container(
width: double.infinity,
child: Card(
color: colorsList[3],
child: InkWell(
child: Container(
margin: const EdgeInsets.all(5.0),
child: Text(answers[3])),
onTap: () {
if (answers[3] ==
QuestionsList.shared.getCurrentQuestion().correctAnswer) {
colorsList[3] = Colors.green;
setState(() {
if (bad==0) {bal++;};
});
} else {
colorsList[3] = Colors.red;
setState(() {bad++;});
}
},
),
),),
ElevatedButton(
onPressed: () {
QuestionsList.shared.nextQuestion();
colorsList[0] = Colors.white;
colorsList[1] = Colors.white;
colorsList[2] = Colors.white;
colorsList[3] = Colors.white;
answers.clear();
answers.addAll([
QuestionsList.shared.getCurrentQuestion().correctAnswer,
QuestionsList.shared.getCurrentQuestion().dis1,
QuestionsList.shared.getCurrentQuestion().dis2,
QuestionsList.shared.getCurrentQuestion().dis3,
]);
answers.shuffle();
setState(() {
_counter++; bad=0;
if (_counter==20) {Navigator.push(
context,
MaterialPageRoute(
builder: (context)
=> PageResult()
),
);}
});
},
child: Text('NEXT')),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('BACK')),
],
)
]));
}
}
我需要把它的值传递给“PageResult”类。
class PageResult extends StatefulWidget {
createState() => new MyWidgetState();
}
class MyWidgetState extends State<PageResult> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text ("Test Result")
),
// body: Text('$bal'),
);
}
}
我知道有两种解决方案。
1.在类之间传递数据
2.使“bal”变量成为全局变量。
但我在手册上找不到解决办法。请帮帮忙。
1条答案
按热度按时间n6lpvg4x1#
可以用
PageResult
小部件的参数化构造函数传递变量,并与State
中的引用widget.bal
一起使用在推进路线的同时