【发布时间】:2021-08-30 02:41:13
【问题描述】:
我想将用户输入到 list1 复制到另一个列表说 list2 并保存它,然后如果用户再次输入应该插入到 list1 中的内容,然后再次将其内容复制到 list2。
输出: 假设用户输入 A、B、C,那么它应该存储在 list1 中。 list1=['A','B','C'] 和 list2 将是 list2=['A','B','C']
然后如果用户再次输入一些东西让我们说 D,E,F 那么 list1 将是 list1=['D','E','F'] 和 list2 将是 list2=['A','B','C','D','E','F']
我想在 gridview 上显示 list2 的内容
这是我所做的:
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
class Result extends StatefulWidget {
final Function resetHandler;
final int resultScore;
List textList = [];
Result(this.resultScore, this.textList, this.resetHandler);
@override
_ResultState createState() => _ResultState();
}
class _ResultState extends State<Result> {
List textListCopy;
var resultList;
int index = 0;
String get resultPhrase {
if (widget.resultScore <= 8) {
return 'You are awesome and innocent!';
} else if (widget.resultScore <= 12) {
return 'Pretty likeable!';
} else if (widget.resultScore <= 16) {
return 'You are .... Strange?!';
} else {
return 'You are so Bad!';
}
}
Color colorSelect() {
Color color;
if (widget.textList[0] == 'Blue' &&
widget.textList[1] == 'Rabbit' &&
widget.textList[2] == 'A') {
color = Colors.blue;
} else if (widget.textList[0] == 'Blue' &&
widget.textList[1] == 'Snake' &&
widget.textList[2] == 'B') {
color = Colors.green;
} else if (widget.textList[1] == 'Lion') {
color = Colors.orange;
} else if (widget.textList[2] == 'C') {
color = Colors.yellow;
} else {
color = Colors.white;
}
return color;
}
String indexSelection() {
list2.addAll(widget.list1); //i am copying my list1 to list2 here
print(list2);
return list2[0];
}
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
/*24 is for notification bar on Android*/
final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
final double itemWidth = size.width / 2;
return Scaffold(
body: //Center(
//child:
//Column(
// mainAxisAlignment: MainAxisAlignment.center,
//children: <Widget>[
Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(color: colorSelect()),
child: //Center(
//child:
Column(children: [
Text(
resultPhrase,
//'Hello!!',
style: TextStyle(fontSize: 36, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
ElevatedButton(
onPressed: widget.resetHandler,
child: Text(
'Restart Quiz',
style: TextStyle(fontSize: 28),
textAlign: TextAlign.center,
)),
Expanded(
child: GridView.count(
childAspectRatio: (itemWidth / itemHeight),
controller: new ScrollController(keepScrollOffset: false),
shrinkWrap: true,
padding: EdgeInsets.fromLTRB(10, 20, 10, 0),
scrollDirection: Axis.vertical,
crossAxisCount: 3,
mainAxisSpacing: 10,
children: [
Card(
child: Text(
indexSelection(),
//index += 1;
//textList[index],
//index += 1;
textAlign: TextAlign.center,
),
),
],
))
])) //)
//]) //)
);
}
}
另一个文件
@override
Widget build(BuildContext context) {
return MaterialApp(
// Hide the debug banner
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text('My First App'),
),
body: _questionIndex < _questions.length
? Quiz(
answerQuestion: _answerQuestion,
questionIndex: _questionIndex,
questions: _questions,
)
: Result(_total_score, textList, _resetQuiz))); //here i'm calling _resetQuiz function
void _resetQuiz() {
setState(() {
_questionIndex = 0;
_total_score = 0;
list1= []; //here i'm emptying my list1 everytime when RESET QUIZ button is pressed so that the final screen color changes in the basis of my answer selection
});
更新:
我也尝试过 from.list,但是每次用户输入内容时它只显示该输入,而不是存储以前的输入
String indexSelection() {
print(list1);
list2= List.from(list1);
print(list2);
return list2[0];
}
【问题讨论】: