我想在我的 flutter 应用程序中通过 onPressed 方法生成卡片。谁能给我一个路线图……?

I want to generate card by onPressed method in my flutter app . Can anyone show me a roadmap for that...?

我在我的页面中制作了listview.builder。我设计了我的卡片。现在我想通过单击浮动操作按钮中的 onpressed 方法在 Listveiw.buider 中一张一张地生成卡片。我应该在那个 onpressed 中写哪个 method/function ?以及如何在其中生成卡片以及我应该在 itemCount 和 itemBuilder 中更改什么。我需要那个路线图。谢谢

ListView.builder(
      itemCount: 5,
    itemBuilder: (context, index) {
      return Dismissible(
        key:Key(null),
        direction: DismissDirection.startToEnd,
        onDismissed: (direction) {},
        confirmDismiss: (direction) {
          return showDialog(
              context: context, builder: (_) => DeleteCard());
        },
        background: Container(
          color: Colors.red,
          padding: EdgeInsets.only(left: 16),
          child: Icon(
            Icons.delete,
            color: Colors.white,
          ),
          alignment: Alignment.centerLeft,
        ),
        child: Card(
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(12.0)),
          color: Colors.white70,
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    width: 120,
                    padding: EdgeInsets.all(10.0),
                    child: Text(
                      "Project ",
                      style: TextStyle(
                          fontSize: 11, fontWeight: FontWeight.bold),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.all(10.0),
                    child: ProjectName(),
                  ),
                ],
              ),

              // item add start

              Container(
                padding: EdgeInsets.all(10.0),
                child: TextFormField(
                  decoration: InputDecoration(
                    hintText: "Item name" ,hintStyle: TextStyle(fontSize: 12),
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.all(10.0),
                child: TextFormField(
                  decoration: InputDecoration(
                    hintText: "Amount",hintStyle: TextStyle(fontSize: 12),

                  ),
                  keyboardType: TextInputType.number,
                ),
              ),
            ],
          ),
        ),
      );
    }
  ),

我想这就是您要找的东西。只需将 ListTile 替换为您自己的 Card

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  var listLength = 1;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Flutter App :)"),
      ),
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () {
            setState(() {
              listLength++;
            });
          }),
      body: Container(
          child: ListView.builder(
              itemCount: listLength,
              itemBuilder: (context, index) {
                return ListTile(
                  leading: Text("$index"),
                  title: Text("List Tile as Widget"),
                );
              })),
    );
  }
}

我已经通过使用 var 解决了这个问题,它的初始值为 0,然后在设置状态下创建一个函数并递增变量。然后我在我的浮动操作按钮中调用了该函数,并在 listview.builder 项目计数中设置了 var 名称。我希望它能帮助其他人这是一个使用 onpressed 生成卡片的简单解决方案。