制作新小部件后清除类的列表

Posted

技术标签:

【中文标题】制作新小部件后清除类的列表【英文标题】:List for a class clears out after making new widget 【发布时间】:2020-09-27 19:40:50 【问题描述】:

我正在尝试学习颤振,但我偶然发现了一个我无法解决的问题。我有一个 MyApp/MyAppState 类,它有一个小部件列表(ovelser),在 listVeiw.builder 中使用。


import './barbutton.dart';

import './ovelser.dart';

void main() 
  runApp(MaterialApp(home: MyApp()));


class MyApp extends StatefulWidget 
  // This widget is the root of your application.

  @override
  State<StatefulWidget> createState() 
    // TODO: implement createState
    return MyAppState();
  


class MyAppState extends State<MyApp> 
  List<Widget> ovelser = [];

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      appBar: AppBar(
        title: Text("progresjon"),
        backgroundColor: Colors.blue,
        actions: <Widget>[AddButton(nameOvelse)],
      ),
      body: ListView.builder(
        itemCount: ovelser.length,
        itemBuilder: (context, index) 
          final Widget ovelse = ovelser[index]; // lagrer bare ovelse objektet
          return Dismissible(
            // dismissible gjør det mulig å slette ting i listView
            key: UniqueKey(),
            onDismissed: (direction) 
              //hva som skjer når man skal slette
              setState(() 
                ovelser.removeAt(index);
              );
            ,
            background: Container(
              color: Colors.red,
            ),
            //child er hva som skal være objektet som kan slettes
            child: ovelse,
          );
        ,
      ),
    );
  

  void addOvelse(String name) 
    setState(() 
      ovelser.add(Ovelser(name));
    );
    print(ovelser.length);
  

  nameOvelse(BuildContext context) 
    TextEditingController custumcontroller = TextEditingController();
    return showDialog(
      context: context,
      builder: (BuildContext context) 
        return AlertDialog(
          title: Text("new activity"),
          content: TextField(
            controller: custumcontroller,
          ),
          actions: <Widget>[
            FlatButton(
              child: Text("create"),
              onPressed: () 
                String activityName = "  " + custumcontroller.text;
                addOvelse(activityName);
                Navigator.of(context).pop();
              ,
            )
          ],
        );
      ,
    );
  

列表 ovelser 接受 Ovelser 对象。这些对象有一个类,它有一个接收整数的列表(progresjonsList),我可以通过 AlertDialog 添加到该列表中。 int 中带有 progresjonList 的类的代码:


import './ovleseraddbutton.dart';

class Ovelser extends StatefulWidget 
  final String name;

  Ovelser(this.name);

  @override
  OvelserState createState() => OvelserState();


class OvelserState extends State<Ovelser> 
  List<int> progresjonList = [];

  @override
  Widget build(BuildContext context) 
    return Container(
      height: 80,
      width: double.infinity,
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
          border: Border(
        top: BorderSide(width: 0.5, color: Colors.grey),
        bottom: BorderSide(width: 0.5, color: Colors.grey),
      )),
      child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Flexible(
                child: Container(
              child: Text(widget.name,
                  overflow: TextOverflow.fade,
                  softWrap: false,
                  maxLines: 1,
                  style: TextStyle(
                      fontStyle: FontStyle.italic,
                      fontSize: 20,
                      fontWeight: FontWeight.bold)),
            )),
            OvelserAddbutton(addvalue)
          ]),
    );
  

  void insertValue(int value) 
    setState(() 
      this.progresjonList.add(value);
    );
  

  addvalue(BuildContext context) 
    TextEditingController custumcontroller = TextEditingController();
    return showDialog(
      context: context,
      builder: (BuildContext context) 
        return AlertDialog(
          title: Text("add new value"),
          content: TextField(
            controller: custumcontroller,
            keyboardType: TextInputType.number,
          ),
          actions: <Widget>[
            FlatButton(
              child: Text("add"),
              onPressed: () 
                String stringnumber = custumcontroller.text;
                int number = int.parse(stringnumber);
                insertValue(number);
                print(number);
                print(progresjonList.length);
                print(this.progresjonList);
                Navigator.of(context).pop();
              ,
            )
          ],
        );
      ,
    );
  

问题是每次我在 ovelser(ListView 中使用的列表)中创建一个新小部件时,带有整数的列表 (progresjonList) 都会被清除,因此它们是空的,并且不会保留先前由 AlertDialog 添加的值。我不明白我怎么能防止这种情况发生,所以我保持整数相加。谁能帮我?提前谢谢你:)

还有两个其他小文件,其中只有图标小部件,我认为这不是问题,但如果您需要它们,它们就是:)


class AddButton extends StatelessWidget 
  final Function setInFunction;

  AddButton(this.setInFunction);

  @override
  Widget build(BuildContext context) 
    return IconButton(
      icon: Icon(Icons.add),
      onPressed: () => setInFunction(context),
    );
  

import 'package:flutter/material.dart';

class OvelserAddbutton extends StatelessWidget 
  final Function setInFunction;

  OvelserAddbutton(this.setInFunction);

  @override
  Widget build(BuildContext context) 
    return IconButton(
      icon: Icon(Icons.add),
      onPressed: () => setInFunction(context),
    );
  

```

【问题讨论】:

【参考方案1】:

progessjonList 对于 Ovelser 类是本地的。您需要将 overserList 传递给 Ovelser 类。

class Ovelser extends StatefulWidget 
  final String name;
  final List<int> list;

  Ovelser(this.name, this.list);

  @override
  OvelserState createState() => OvelserState();

然后,当您想添加到 OvelserState 中的列表时,只需使用

widget.list.add(/*add int here*/);

我看到的是你的 insertValue 函数

void insertValue(int value) 
  setState(() 
    widget.list.add(value);
  );

您传入的列表将是对原始类中的 ovelser 列表的引用。

【讨论】:

很高兴为您提供帮助。享受吧。

以上是关于制作新小部件后清除类的列表的主要内容,如果未能解决你的问题,请参考以下文章

QDialog 在添加新小部件时将小部件涂成黑色

更改新小部件的大小

Gridstack - 添加带有内容的新小部件

在新小部件中使用提供程序实例时哪个更好?

tkinter 中的框架对象没有对小部件进行分组

客户验收测试应该有多详细?