【问题标题】:how to copy one list to another list in flutter如何在颤动中将一个列表复制到另一个列表
【发布时间】: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];
  }

【问题讨论】:

    标签: list flutter


    【解决方案1】:

    试试这个:

    list2.addAll(list1);
    

    Here 是一篇关于实现相同目标的其他方法的好文章。

    【讨论】:

    • 好的,很高兴知道。我认为您的问题更多是关于颤振应用程序中的状态管理,而不是关于列表连接。我建议做一些 introductory reading 并尝试相应的示例应用程序以熟悉 Flutter 应用程序的一般状态。
    【解决方案2】:

    你可以使用

    List list2 = [...list1];
    

    【讨论】:

    • 你能编辑我的代码吗?我可以在哪里使用它?
    • 这只是分配的另一种表示法,但我认为@Hira 想要串联。
    • 是的,我想将 list1 连接到 list2。换句话说,我想要 list2 中 list1 的先前项目以及 list1 的当前项目。
    猜你喜欢
    • 2020-02-11
    • 2016-02-28
    • 1970-01-01
    • 2017-11-02
    • 2019-01-26
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    相关资源
    最近更新 更多