单击添加新的ListTile项目 [英] Adding new ListTile item on click
本文介绍了单击添加新的ListTile项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了 method
和 widget
,现在我的代码可以正常工作了,因为数据是 static
,我想在其中添加新项当我按下添加"按钮时,会出现 ListTile
.
I have created method
and widget
, right now my code is working fine because data is static
, i want to add new item in ListTile
when i press Add Button.
方法:
Card listSectionMethod(String title, String subtitle, IconData icon) {
return new Card(
child: ListTile(
title: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(subtitle),
trailing: Icon(
icon,
color: Colors.blue,
),
),
);
}
小部件:
Widget listSection = Container(
margin: EdgeInsets.only(top: 210),
child: ListView(
children: [
Column(
children: [
listSectionMethod("TITLE 1", "hello i am subtitle one",Icons.forward),
listSectionMethod("TITLE 2", "hello i am subtitle two",Icons.forward),
],
),
],),
);
按钮:
FloatingActionButton(
onPressed:()
{
print("clicked"); //i want to add new item from here, when i press on click
},
child:Text("+"),
backgroundColor: Colors.blue,
),
);
推荐答案
要添加新项目,您必须执行以下操作:
look to add a new item you must do the following:
假设您的屏幕是一个StatefulWidget
class SomePage extends StatefulWidget {
@override
_SomePageState createState() => _SomePageState();
}
class _SomePageState extends State<SomePage> {
var _listSection = List<Widget>();
@override
void initState() {
super.initState();
// Here initialize the list in case it is required
_listSection.add(
listSectionMethod("TITLE 1", "hello i am subtitle one", Icons.forward),
);
_listSection.add(
listSectionMethod("TITLE 2", "hello i am subtitle two", Icons.forward),
);
}
}
小部件:
Widget listSection() {
return Container(
margin: EdgeInsets.only(top: 210),
child: ListView(
children: [
Column(
children: this._listSection, // ----> Add this
),
],
),
);
}
按钮:
FloatingActionButton(
onPressed: () {
setState(() {
// Here we add a new item to the list
_listSection.add(
listSectionMethod("New TITLE", "hello from click", Icons.forward),
);
});
},
child: Text("+"),
backgroundColor: Colors.blue,
);
这篇关于单击添加新的ListTile项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文