Created
April 6, 2023 08:19
-
-
Save gausoft/87f70efc04a6912e09e3234ef30d3fb6 to your computer and use it in GitHub Desktop.
Revisions
-
gausoft created this gist
Apr 6, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ import 'package:flutter/material.dart'; import 'add_product.dart'; import 'product.dart'; class ProductList extends StatefulWidget { const ProductList({Key? key}) : super(key: key); @override _ProductListState createState() => _ProductListState(); } class _ProductListState extends State<ProductList> { final List<Product> _products = []; void _addProduct(Product product) { setState(() { _products.insert(0, product); }); } void _navigateToAddProduct(BuildContext context) async { final product = await Navigator.push<Product>( context, MaterialPageRoute(builder: (context) => AddProduct()), ); if (product != null) { _addProduct(product); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Product List'), ), body: ListView.builder( itemCount: _products.length, itemBuilder: (context, index) { final product = _products[index]; return ListTile( title: Text(product.name), subtitle: Text('\$${product.price.toStringAsFixed(2)}'), ); }, ), floatingActionButton: FloatingActionButton( onPressed: () => _navigateToAddProduct(context), child: const Icon(Icons.add), ), ); } }