0

所以我有一堆用户之前回答过的问题,我正在显示带有答案的问题,并让用户可以选择只显示用户用正确答案回答过的问题,同样适用于答案错误的问题,我想做的是从动画列表中删除一堆项目代码正在工作,但我不断收到“多个小部件使用相同的 GlobalKey”。错误

这就是我创建和初始化密钥的方式:

late GlobalKey<AnimatedListState> _listKey;

@override
  void initState() {
    super.initState();
    mainItems = items;
    _listKey = GlobalKey<AnimatedListState>(debugLabel: "ListGlobalKey");
  }

这是代码中 AnimatedList 部分的代码:

Expanded(
              

child: Container(
   padding: EdgeInsets.all(12.0),
   child: AnimatedList(
   initialItemCount: mainItems.length,
   key: _listKey,
   itemBuilder: (context, index, animation) {
       return QueViewWidget(
          item: mainItems[index],
          animation: animation,
          onlyRight: onlyRight,
          onlyWrong: onlyWrong,
                  );
                },
              ),

这是删除项目的代码:

void removeItems(){
    if(onlyRight){
      for(int i = 0; i < mainItems.length; i++){
        QueList item = mainItems[i];
        if(item.isRight==false){
          mainItems.removeAt(i);
          _listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
            item: item,
            animation: animation,
            onlyRight: onlyRight,
            onlyWrong: onlyWrong,
          )
          );
        }
      }
    }
    if(onlyWrong){
      for(int i = 0; i < mainItems.length; i++){
        QueList item = mainItems[i];
        if(item.isRight!=false){
          mainItems.removeAt(i);
          _listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
            item: item,
            animation: animation,
            onlyRight: onlyRight,
            onlyWrong: onlyWrong,
          )
          );
        }
      }
    }
    if(onlyRight==false && onlyWrong == false){
      Navigator.of(context).pushReplacementNamed('/view');
    }
  }

这是整个页面脚本:

import 'package:flutter/material.dart';
import 'package:liquid_swipe/liquid_swipe.dart';
import 'package:qudorat_app/prosses/card_que_view.dart';
import 'package:qudorat_app/prosses/question_list.dart';
import 'package:qudorat_app/prosses/temp_list.dart';

class ViewAnswers extends StatefulWidget {

  const ViewAnswers();


  @override
  _ViewAnswersState createState() => _ViewAnswersState();
}

class _ViewAnswersState extends State<ViewAnswers> {
  bool onlyRight = false;
  bool onlyWrong = false;

  late GlobalKey<AnimatedListState> _listKey;
  List<QueList> mainItems = [];

  List<QueList> items = List.from(queListItems);


  @override
  void initState() {
    super.initState();
    mainItems = items;
    _listKey = GlobalKey<AnimatedListState>(debugLabel: "ListGlobalKey");
  }

  void removeItems(){
    if(onlyRight){
      for(int i = 0; i < mainItems.length; i++){
        QueList item = mainItems[i];
        if(item.isRight==false){
          mainItems.removeAt(i);
          _listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
            item: item,
            animation: animation,
            onlyRight: onlyRight,
            onlyWrong: onlyWrong,
          )
          );
        }
      }
    }
    if(onlyWrong){
      for(int i = 0; i < mainItems.length; i++){
        QueList item = mainItems[i];
        if(item.isRight!=false){
          mainItems.removeAt(i);
          _listKey.currentState!.removeItem(i, (context, animation) => QueViewWidget(
            item: item,
            animation: animation,
            onlyRight: onlyRight,
            onlyWrong: onlyWrong,
          )
          );
        }
      }
    }
    if(onlyRight==false && onlyWrong == false){
      Navigator.of(context).pushReplacementNamed('/view');
    }
  }
  @override
  Widget build(BuildContext context) {
    print(mainItems.length);
    return LiquidSwipe(pages: [
      Scaffold(
        backgroundColor: Colors.amber,
        body: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                  colors: [Colors.pink, Colors.amber])),
          child: Center(
            child: SafeArea(
              child: Column(children: [
                Row(
                  children: [
                    Expanded(
                        child: Container(
                      margin: EdgeInsets.all(8),
                      padding: EdgeInsets.fromLTRB(8, 12, 8, 12),
                      decoration: BoxDecoration(
                        color: Colors.lightBlueAccent,
                        borderRadius: BorderRadius.circular(25.0),
                        boxShadow: [
                          BoxShadow(
                              color: Colors.black54,
                              blurRadius: 15.0,
                              offset: Offset(0.0, 0.75))
                        ],
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Switch(
                            activeColor: Colors.grey[800],
                            value: onlyRight,
                            onChanged: onlyWrong ? null: (b) {
                              setState(() {
                                onlyRight = b;
                                removeItems();

                              });
                            },
                          ),
                          Text(
                            "الإجابات \n الصحيحة", \\ stands for Right Answers
                            textDirection: TextDirection.rtl,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: Colors.white,
                                fontFamily: "ArabicModern-Bold"),
                          ),
                          SizedBox(
                            width: 100,
                          ),
                          // add math arabic
                          Switch(
                            activeColor: Colors.grey[800],
                            value: onlyWrong,
                            onChanged: onlyRight ? null: (b) {
                              setState(() {
                                onlyWrong = b;
                                removeItems();

                              });
                            },
                          ),
                          Text(
                            "الإجابات \n الخاطئة", \\ stands for Wrong Answers
                            textDirection: TextDirection.rtl,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: Colors.white,
                                fontFamily: "ArabicModern-Bold"),
                          ),
                        ],
                      ),
                    )),
                  ],
                ),
                Expanded(
                    child: Container(
                  padding: EdgeInsets.all(12.0),
                  child: AnimatedList(
                    initialItemCount: mainItems.length,
                    key: _listKey,
                    itemBuilder: (context, index, animation) {
                      return QueViewWidget(
                        item: mainItems[index],
                        animation: animation,
                        onlyRight: onlyRight,
                        onlyWrong: onlyWrong,
                      );
                    },
                  ),
                ))
              ]),
            ),
          ),
        ),
      ),
    ]);
  }
}

在过去的 12 个小时里,我一直在尝试解决这个问题,如果你知道如何解决这个问题,请帮助我,谢谢你的时间。

4

0 回答 0