2

I have made the listview.builder in my page. In that i have designed my card . Now i want to generate card in the Listveiw.buider one by one by clicking a onpressed method in a floating action button. Which method/function should i write in that onpressed ? and how to generate card in that and what should i change in itemCount and itemBuilder. I need that roadmap. Thanks.

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,
                ),
              ),
            ],
          ),
        ),
      );
    }
  ),
  • Try using a variable let's say `totalCards` then put it at `itemCount: totalCards` and increment it in `FAB` this will increase the `index` of the `ListView.builder` and since you are returning some `Widgets` so the number of widgets will also increase. Give it a shot :) – Hamza Oct 25 '20 at 11:29
  • thanks for your response brother . But after implement this with variables it is now showing infinitive cards in my listview.builder. I think i should write some mehod in the onpressed . Can u provide a solution of it. I just only want ceate one card by clicking the button. – Mir Fahim Rahman Oct 25 '20 at 11:46
  • 1
    I have add the working code, which will increase the `ListTile` in a `ListView.builder` when `onPressed` Try it. I hope this will help :) – Hamza Oct 25 '20 at 12:05

2 Answers2

1

This is something you are looking for I think. Just replace the ListTile with your own 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"),
                );
              })),
    );
  }
}

Hamza
  • 1,523
  • 15
  • 33
0

I have solved it by using a var which initial value in 0 then make a function in set state and increment the variable. Then I called the function in my floating action button and in listview.builder item count I set the var name. I hope it will help others it's a easy solution to generate card by using onpressed.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459