我正在尝试学习颤振,但是我偶然发现了一个我无法解决的问题。我有一个MyApp / MyAppState类,该类具有在listVeiw.builder中使用的小部件列表(ovelser)。
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), ); } } ```
我正在尝试学习颤振,但是我偶然发现了一个我无法解决的问题。我有一个MyApp / MyAppState类,该类具有在listVeiw.builder中使用的小部件(ovelser)列表。导入'。/ ...
progessjonList对于Ovelser类是本地的。您需要将overserList传递给Ovelser类。