当用户单击FAB时,我希望它创建包裹在Card
中的新GestureDetector
小部件。我已经创建了类型窗口小部件的列表,还创建了一个带有setState
的函数以添加到列表并在SliverGrid
中显示它。但是当我单击FAB时,它似乎没有添加。
我可以获取有关如何使用FAB添加Card
的小指南。 (或在SliverGrid
中添加任何类型的小部件)。
列表:List<Widget> cardList = <Widget>[];
将项目添加到列表的功能:
void addItems() async{
setState(() {
cardList.insert(0, GestureDetector(
onTap: () async {
await Navigator.push( context, MaterialPageRoute(
builder: (context) => TodoList(), // this just navigates to another screen ; not important in this question
)
);
},
child: Card(
child:Center(child: whitefontstylemont(text: "project 1", size:20,)) //this is just a custom TextStyle
),
));
});
}
将新卡片添加到列表中的浮动操作按钮:
floatingActionButton: FloatingActionButton( // this is inside a Scaffold
onPressed: ()async {
setState(() {
addItems();
});
},
heroTag: "btn2",
child: Icon(Icons.add, color: Color(whitecolor),), backgroundColor: Color(redcolor),),
我要在其中显示新卡的SliverGrid
:
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2
),
delegate: new SliverChildBuilderDelegate((context, index) {
return cardList[index];
},
childCount: cardList.length // this is where i want a new Card to be displayed
)
),