dimanche 31 octobre 2021

Need Query or condition to get selected data in flutter

I am stuck in creating query for the selected items and show selected data on other page. So help me to create query or condition. when I press floating action button then I want to get selected items data on other page.

Here is Main File code:

class MenuPage extends StatefulWidget {
  const MenuPage({Key? key}) : super(key: key);

  @override
  _MenuPageState createState() => _MenuPageState();
}

class _MenuPageState extends State<MenuPage> {
  DatabaseReference db = FirebaseDatabase.instance.reference();
  User? curUser = FirebaseAuth.instance.currentUser;

  int? len;
  List<GetData>? data;

  late List<bool>? avail = List.generate(
    len!,
    (index) => false,
  );

  late List<TextEditingController>? qty = List.generate(
    len!,
    (index) => TextEditingController(),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Today's Menu"),
        centerTitle: true,
        backgroundColor: Colors.deepOrange,
      ),
      body: StreamBuilder<List<GetData>>(
        stream: DataStreamPublisher("Juices").getMenuStream(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            data = snapshot.data;
            len = data!.length;
            return SafeArea(
              child: SingleChildScrollView(
                child: Container(
                  margin: EdgeInsets.symmetric(vertical: 10.0),
                  width: double.infinity,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(
                        width: double.infinity,
                        child: DataTable2(
                          showBottomBorder: true,
                          headingRowHeight: 40,
                          showCheckboxColumn: true,
                          sortAscending: true,
                          columnSpacing: 5,
                          dataRowHeight: 60,
                          dividerThickness: 2,
                          bottomMargin: 20.0,
                          checkboxHorizontalMargin: 5,
                          columns: [
                            DataColumn2(
                              size: ColumnSize.L,
                              label: Center(
                                child: Text(
                                  "Item Name",
                                  style: TextStyle(
                                    fontSize: 16,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                              ),
                            ),
                            DataColumn2(
                              size: ColumnSize.S,
                              label: Center(
                                child: Text(
                                  "Price",
                                  style: TextStyle(
                                    fontSize: 16,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                              ),
                            ),
                            DataColumn2(
                              size: ColumnSize.L,
                              label: Center(
                                child: Text(
                                  "Quantity",
                                  style: TextStyle(
                                    fontSize: 16,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                              ),
                            ),
                          ],
                          rows: List<DataRow>.generate(
                            data!.length,
                            (index) => DataRow2(
                              selected: avail![index],
                              onSelectChanged: (bool? value) {
                                setState(() {
                                  avail![index] = value!;
                                });
                              },
                              color: MaterialStateProperty.resolveWith<Color?>(
                                (Set<MaterialState> states) {
                                  if (states.contains(MaterialState.selected)) {
                                    return Theme.of(context)
                                        .colorScheme
                                        .primary
                                        .withOpacity(0.08);
                                  }
                                  return null;
                                },
                              ),
                              cells: [
                                DataCell(
                                  Text(
                                    data![index].Juice_Name,
                                  ),
                                ),
                                DataCell(
                                  Center(
                                    child: Text(
                                      data![index].Price,
                                    ),
                                  ),
                                ),
                                DataCell(
                                  Center(
                                    child: IncDecButton(
                                      Qty: qty![index],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          } else if (snapshot.hasError) {
            return Center(
              child: Text(
                'Add Item in Menu.',
                style: TextStyle(
                  fontSize: 18,
                ),
              ),
            );
          }
          return Center(
            child: const CircularProgressIndicator(),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        tooltip: "Proceed to Bill",
        onPressed: () {
          // When I pressed floating button I want only selected items data
        },
        child: Text("ADD"),
      ),
    );
  }
}

This is stream of GetDataPublisher This code get data from firebase.

      class DataStreamPublisher {
          final String dbChild;
          DatabaseReference database = FirebaseDatabase.instance.reference();
          User? curUser = FirebaseAuth.instance.currentUser;
        
          DataStreamPublisher(this.dbChild);
        
          Stream<List<GetData>> getMenuStream() {
            final dataStream =  database.child(curUser!.uid).child(dbChild)
               .orderByChild("Available").equalTo(true).onValue;
            final streamToPublish = dataStream.map((event) {
              final dataMap = Map<String, dynamic>.from(event.snapshot.value);
              final dataList = dataMap.entries.map((e) {
                return GetData.fromRTDB(Map<String, dynamic>.from(e.value));
              }).toList();
              return dataList;
            });
            return streamToPublish;
          }
        }

This is get data class

    class GetData {
          String Juice_Name;
          String Price;
        
          GetData({
            required this.Juice_Name,
            required this.Price,
          });
        
          factory GetData.fromRTDB(Map<String, dynamic> data) {
            return GetData(
              Juice_Name: data['Juice_Name'] ?? 'Enter Juice Name',
              Price: data['Price'] ?? 'Enter Price',
            );
          }
        }

Aucun commentaire:

Enregistrer un commentaire