Question:
How to stop counter initialize in listview builder list?

To stop counter initializing in the listview builder list, you have to make a single counter variable for all list view items. i.e.

int counter = 0;


Nest is to create a separate variable for each listview item.

One approach could be using a List of int instead of a single int variable.

List<int> counters = [];


The builder will add a new variable for each iteration.

counters.add(0);


You can will access it using the index of ListView.builder.

counters[index]++;


For your ease I have used your code example to show it. You can test it using below code.


import 'package:flutter/material.dart';


class TestPage extends StatefulWidget {

  const TestPage({super.key});


  @override

  State<TestPage> createState() => _TestPageState();

}


class _TestPageState extends State<TestPage> {

  // Create a list of texts

  final List<String> texts = [

    "Hello world",

    "Hello world!2"


  ];


  final List<int> subTexts = [

    1,

    2,

  ];


  // int counter = 0;

  List<int> counters = [];


  @override

  Widget build(BuildContext context) {

    return Scaffold(

      backgroundColor: Colors.grey[200],

      appBar: AppBar(

        backgroundColor: Colors.transparent,

        elevation: 0,

        title: const Text(

          "أذكار الصباح",

          textDirection: TextDirection.rtl,

        ),

        centerTitle: true,

      ),

      body: Container(

        padding: const EdgeInsets.all(

          25,

        ),

        child: ListView.builder(

          // Set the length of the list

          itemCount: texts.length,

          // Return a Card widget for each item

          itemBuilder: (context, index) {

            counters.add(0);

            return Padding(

              padding: const EdgeInsets.symmetric(

                vertical: 8,

              ),

              child: Card(

                shape: RoundedRectangleBorder(

                  borderRadius: BorderRadius.circular(10),

                  //set border radius more than 50% of height and width to make circle

                ),

                // Use a Column to arrange the text and the buttons vertically

                child: Container(

                  padding: const EdgeInsets.all(25.0),

                  color: Colors.amber,

                  child: Column(

                    children: [

                      // Display the text inside the card

                      AmiriText(

                        text: texts[index],

                        fontS: 25,

                        textDirection: TextDirection.rtl,

                      ),

                      SizedBox(

                        height: 25,

                      ),

                      CircleAvatar(

                        child: Text(

                          subTexts[index].toString(),

                        ),

                      ),

                      SizedBox(

                        height: 25,

                      ),

                      // Use a Row to display the two buttons horizontally

                      Row(

                        mainAxisAlignment: MainAxisAlignment.spaceAround,

                        children: <Widget>[

                          ElevatedButton(

                            style: ElevatedButton.styleFrom(

                              backgroundColor: Color(0xFF086788),

                              foregroundColor: Color(0xFF9CAFB7),

                              shadowColor: Colors.grey[400],

                              elevation: 3,

                              shape: RoundedRectangleBorder(

                                borderRadius: BorderRadius.circular(

                                  10.0,

                                ),

                              ),

                              minimumSize: Size(100, 40),

                            ),

                            child: AmiriText(

                              text: "return back",

                              fontS: 18,

                              color: Colors.white,

                            ),

                            onPressed: () {

                              setState(() {

                                if (counters[index] > 0) {

                                  counters[index]--;

                                }

                              });

                            },

                          ),

                          Text(

                            counters[index].toString(),

                            style: const TextStyle(

                              fontSize: 18,

                              fontWeight: FontWeight.w700,

                            ),

                          ),

                          ElevatedButton(

                            style: ElevatedButton.styleFrom(

                              backgroundColor: Color(0xFF062726),

                              foregroundColor: Color(0xFF66A182),

                              shadowColor: Colors.grey[400],

                              elevation: 3,

                              shape: RoundedRectangleBorder(

                                borderRadius: BorderRadius.circular(

                                  10.0,

                                ),

                              ),

                              minimumSize: Size(100, 40),

                            ),

                            child: AmiriText(

                              text: "Go",

                              fontS: 18,

                              color: Colors.white,

                            ),

                            onPressed: () {

                              setState(() {

                                counters[index]++;

                              });

                            },

                          ),

                        ],

                      ),

                    ],

                  ),

                ),

              ),

            );

          },

        ),

      ),

    );

  }


  AmiriText({required String text, required int fontS, Color? color, TextDirection? textDirection}) {

    return Text(text);

  }

}


Suggested blog:

>How to use a function to print S3 data to HTML using Django?

>How to make a code executed after an entry is inserted, removed or updated?

>How to insert new data into the table in Django?

>Disabling default ordering in the Django admin panel

>How to migrate `DateTimeField` to `DateField` in Django?

>Python code can be hosted in Heroku or not?

>Why VueJS / Vuetify error rendering a vTable component?

>How to watch for event changes on a tailwind listbox component with Vuejs?

>How to add the total amount for each index list Vuejs?

>How to set up a dynamic grid based on flex or grid in Vuejs?

>How to get all the rows selected in Vuejs?


Nisha Patel

Nisha Patel

Submit
0 Answers