首页 > 解决方案 > 为新行颤动创建按钮

问题描述

如果添加了一个,我如何创建按钮来添加一行和一个删除按钮。就像在联系人 屏幕下方的联系人菜单中一样

标签: buttondynamicdartflutter

解决方案


这只是一个关于如何做到这一点的示例,并不是完整的代码。您可以在StatefulWidget中使用ListView.builder并在每次单击按钮时将FormField添加到列表中。删除它也是如此:

var items = [
  FormField(...),
]

class Some extends StatefulWidget{
  SomeState createState()=>  SomeState();
}

class SomeState extends State<Some> {
  @override
  Widget build(BuildContext context){
    return Column(
      children: <Widget> [
        Expanded(child:
          ListView.builder(itemBuilder: (context, index){
            return items[index];  
          }),
        ),
        RaisedButton(
          text: new Text("someButton"), 
          onPressed: () {
            setState(() {
              items.remove(FormField(...));
              items.add(FormField(...));
            })
          }
        ),
      ]
    );
  }
}

推荐阅读