title: How to add item in a listview from one class to another in Flutter?
body: <p>I am working on a project in which i have a class which has a row that has two children. Child one contains a TabView with a child class TabViewChild in which i am generating a gridview. On the other hand child two contains a listView. So the problem is, when i click on the gridview item i am passing that item's value to a static list and passing that list to a listview of other class. But i have no idea how to change the state of that class on item clicked or how can i achieve this task in a better way as i am new to Flutter. I want that when a person click on any gridview's item that item appears in the listview  simultaneously.</p>
  <pre><code>class ItemMenus {
  
     int id;
     String code;
     String name;
     String salePrice;
     String photo;
     String categoryName;
     String percentage;
     int quantity;
  
    ItemMenus({this.id, this.code, this.name, this.salePrice, this.photo,
        this.categoryName, this.percentage, this.quantity});
  
    ItemMenus.fromJson(Map&lt;String, dynamic&gt; json)
    :
      id = int.parse(json['id']),
      code = json['code'],
      name = json['name'],
      salePrice = json['sale_price'],
      photo = json['photo'],
      categoryName = json['category_name'],
      percentage = json['percentage'];
  
    @override
    String toString() {
      return 'ItemMenus{id: $id, code: $code, name: $name, salePrice: $salePrice, photo: $photo, categoryName: $categoryName, percentage: $percentage}';
    }
  
  
  }
  </code></pre>
  
  <pre><code>import 'package:food_app/model/mdl_item_menus.dart';
  
  class ItemMenuList{
  
    List&lt;ItemMenus&gt; _itmMenuLst = [];
  
    ItemMenuList._();
    static final ItemMenuList instanceItmMenuLst = ItemMenuList._();
    static ItemMenuList get instance =&gt; instanceItmMenuLst;
  
    void addItem(ItemMenus im){
      _itmMenuLst.add(im);
    }
  
    List&lt;ItemMenus&gt; get list =&gt; _itmMenuLst;
  
    @override
    String toString() {
      return 'ItemMenuList{_itmMenuLst: $_itmMenuLst}';
    }
  }
  </code></pre>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:food_app/database/tables/tbl_categories.dart';
  import 'package:food_app/model/list/item_menu_list.dart';
  import 'package:food_app/model/mdl_categories.dart';
  import 'package:food_app/model/mdl_item_menus.dart';
  import 'package:food_app/pos/tab_bar_view.dart';
  
  class EPos extends StatefulWidget{
  
    @override
    _EPosState createState() =&gt; _EPosState();
  }
  
  class _EPosState extends State&lt;EPos&gt; {
  
    final categoryDBHelper = TblCategories.categoriesInstance;
    final ItemMenuList instance2 = ItemMenuList.instance;
  
    String _orderType = 'Dine-In', _info = 'Table No. 1', _userName = 'ZiaUddin';
  
    List&lt;Categories&gt; catLst = [];
    List&lt;ItemMenus&gt; itmLst = [];
  
    Future getCategories() async {
      catLst.clear();
      var categories = await categoryDBHelper.getCategories();
      categories.forEach((element) {
        catLst.add(Categories(
            id: element['id'],
            categoryName: element['category_name'],
            description: element['description'],
            userId: element['user_id'],
            companyId: element['company_id'],
            delStatus: element['del_status']));
      });
      catLst.forEach((element) {
        print(element);
      });
      return catLst;
    }
  
    @override
    void initState() {
      // TODO: implement initState
      super.initState();
      // itmLst = instance2.list;
  
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        backgroundColor: Colors.white,
        body: Column(
          children: [
            //#region AppBar
            Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height * 0.15,
              color: Colors.redAccent,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(
                    margin: EdgeInsets.fromLTRB(8, 10, 0, 0),
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      borderRadius: BorderRadius.circular(20),
                      color: Colors.red,
                    ),
                    child: Row(
                      children: [
                        Icon(
                          Icons.arrow_back,
                          color: Colors.white,
                          size: 25,
                        ),
                        Padding(
                          padding: const EdgeInsets.fromLTRB(4, 8, 10, 8),
                          child: Text(
                            _orderType,
                            style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                              color: Colors.white,
                              fontFamily: 'Ubuntu',
                              letterSpacing: 2.0,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.fromLTRB(8, 10, 0, 0),
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      borderRadius: BorderRadius.circular(10),
                      color: Colors.red,
                    ),
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(20, 8, 20, 8),
                      child: Text(
                        _info,
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.bold,
                          color: Colors.amberAccent,
                          fontFamily: 'Ubuntu',
                          letterSpacing: 2.0,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 15, right: 5),
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      borderRadius: BorderRadius.circular(30),
                      color: Colors.red,
                    ),
                    child: Row(
                      children: [
                        Padding(
                          padding: const EdgeInsets.fromLTRB(10, 8, 8, 8),
                          child: Text(
                            _userName,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.normal,
                              color: Colors.white,
                              fontFamily: 'Ubuntu',
                              letterSpacing: 1.0,
                            ),
                          ),
                        ),
                        CircleAvatar(
                          backgroundColor: Colors.red,
                          radius: MediaQuery.of(context).size.height * 0.041,
                          child: CircleAvatar(
                            radius: MediaQuery.of(context).size.height * 0.04,
                            backgroundImage: AssetImage('assets/user.png'),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            //endregion
            Expanded(
              child: Row(
                children: [
                  //# region Menu
                  Flexible(
                    flex: 1,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.yellowAccent,
                        shape: BoxShape.rectangle,
                      ),
                      child: Column(
                        children: [
                          Expanded(
                            child: Container(
                              color: Colors.white,
                              child: FutureBuilder(
                                  future: getCategories(),
                                  builder: (context, snapShot) {
                                    if (snapShot.connectionState ==
                                            ConnectionState.none &amp;&amp;
                                        snapShot.hasData == null) {
                                      return Center(
                                          child: CircularProgressIndicator());
                                    }
                                    return MaterialApp(
                                      debugShowCheckedModeBanner: false,
                                      home: DefaultTabController(
                                        length: catLst.length,
                                        child: Scaffold(
                                          backgroundColor: Colors.white,
                                          appBar: PreferredSize(
                                            preferredSize:
                                                Size.fromHeight(kToolbarHeight),
                                            child: Container(
                                              height: MediaQuery.of(context)
                                                      .size
                                                      .height *
                                                  0.1,
                                              child: TabBar(
                                                indicatorColor:
                                                    Colors.amberAccent,
                                                isScrollable: true,
                                                tabs: catLst
                                                    .map&lt;Widget&gt;((Categories c) {
                                                  return Tab(
                                                    icon: Icon(
                                                      Icons.style,
                                                      color: Colors.amberAccent,
                                                      size: 15,
                                                    ),
                                                    child: Text(
                                                      c.categoryName
                                                          .toUpperCase(),
                                                      style: TextStyle(
                                                        color: Colors.black,
                                                        fontWeight:
                                                            FontWeight.w400,
                                                      ),
                                                    ),
                                                  );
                                                }).toList(),
                                              ),
                                            ),
                                          ),
                                          body: TabBarView(
                                            children: catLst.map((Categories c) {
                                              return TabBarViewChild(categoryName:c.categoryName,
                                                callback: (){
                                                setState(() {
                                                  instance2.addItem(ItemMenus(name: c.categoryName));
                                                  itmLst = instance2.list;
                                                  print('I am Callback');
                                                });
                                              },);
                                            }).toList(),
                                          ),
                                        ),
                                      ),
                                    );
                                  }),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  //endregion
                  //# region OrderList
                  Flexible(
                    flex: 1,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        shape: BoxShape.rectangle,
                      ),
                      child: ListView.builder(
                            itemCount: itmLst.length,
                            itemBuilder: (context, index){
                              return ListTile(
                                title: Text(itmLst[index].name),
                              );
                            },
                          ),
                    ),
                  ),
                  //endregion
                ],
              ),
            ),
          ],
        ),
      );
    }
  }
  </code></pre>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:food_app/database/tables/tbl_item_menus.dart';
  import 'package:food_app/model/list/item_menu_list.dart';
  import 'package:food_app/model/mdl_item_menus.dart';
  import 'package:food_app/pos/new_sale.dart';
  
  
  class TabBarViewChild extends StatefulWidget {
  
      String categoryName;
      VoidCallback callback;
      TabBarViewChild({Key key,@required this.categoryName, this.callback}) : super(key:key);
  
    @override
    _TabBarViewChildState createState() =&gt; _TabBarViewChildState();
  }
  
  class _TabBarViewChildState extends State&lt;TabBarViewChild&gt; {
  
      final ItemMenuList instance1 = ItemMenuList.instance;
  
      List&lt;ItemMenus&gt; itmLst = [];
  
      final itemMenus = TblItemMenus.itemMenusInstance;
  
      Future getSpecificItemMenus() async{
        var items = await itemMenus.getSpecificItemMenus(widget.categoryName);
        itmLst.clear();
        items.forEach((e) {
          itmLst.add(ItemMenus(id: e['id'], categoryName: e['category_name'], code: e['code'],
          name: e['name'], percentage: e['percentage'], photo: e['photo'], quantity: e['quantity'],
          salePrice: e['sale_price']));
        });
        for (var value in itmLst) {
          print(value);
        }
        return itmLst;
      }
  
    @override
    Widget build(BuildContext context) {
      return Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        color: Colors.white,
        child: FutureBuilder(
          future: getSpecificItemMenus(),
          builder: (context, snapShot) {
            if (snapShot.connectionState == ConnectionState.none
                &amp;&amp; snapShot.hasData == null) {
              return Center(child: CircularProgressIndicator());
            }
            return GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
                scrollDirection: Axis.vertical,
                itemCount: itmLst.length,
                itemBuilder: (context, index){
                  return Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: InkWell(
                      child: Card(
                        elevation: 4,
                        color: Colors.amberAccent,
                        child: Center(
                          child: Text(
                            itmLst[index].name.toUpperCase(),
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              color: Colors.red,
                              fontSize: 13,
                              fontWeight: FontWeight.w700,
                              fontFamily: 'Ubuntu',
                              letterSpacing: 1.0,
                            ),
                          ),
                        ),
                      ),
                      onTap: (){
                        // Provider.of&lt;ProItemMenus&gt;(context, listen: false).addNewItemInList(ItemMenus(name: itmLst[index].name.toUpperCase()));
                        instance1.addItem(ItemMenus(categoryName: itmLst[index].name.toUpperCase()));
                        print(itmLst[index].name.toUpperCase());
                      },
                    ),
                  );
                }
            );
          }
        ),
      );
    }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/4jKb8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4jKb8.jpg" alt="enter image description here" /></a></p>