0

I have a Flutter project that scans barcodes and move the name and barcode and expiration date by the button to the row on another page and this works as expected. But I'm trying to display this row in my home page that contains a ListView and I let inside it some empty list I defined above on the same page.

My question: How can I add the row that contains the name, barcode, and date to the list on my home page when I press the button?

This is the button code:

ElevatedButton(
            onPressed: () {
              final list = [item_Name.text, item_Barcode.text, date.text];
              Provider.of<DataListProvider>(context, listen: false).setData(list);
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => Details_Item(
                      name: item_Name.text,
                      barcode: item_Barcode.text,
                      dateexpair: date.text,
                  ),
                ),
              );
            },

and this is the row page :

class Details_Item extends StatelessWidget {
  late String name, barcode;
  String dateexpair;

  Details_Item(
      {required this.name, required this.barcode, required this.dateexpair});

  @override
  Widget build(BuildContext context) {
    return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            '${name}',
            style: TextStyle(fontSize: 13),
          ),
          SizedBox(
            width: 70,
          ),
          Text(
            '${barcode}',
            style: TextStyle(fontSize: 13),
          ),
          SizedBox(
            width: 70,
          ),
          Text(
            '${dateexpair}',
            style: TextStyle(fontSize: 13),
          ),
        ],
    );
  }
}

my home page code :

child: ListView(
              children: Container_items,
lepsch
  • 8,927
  • 5
  • 24
  • 44
Delyar Alo
  • 11
  • 2

1 Answers1

0

I think the best option is to implement a model/provider where you can share data between pages. Read the official docs here.

On the other hand, Navigator.pop(context, result) has a result parameter that returns to the caller of await Navigator.push(...). You can use that to pass data between pages.

lepsch
  • 8,927
  • 5
  • 24
  • 44