Skip to content

Instantly share code, notes, and snippets.

@gausoft
Created April 6, 2023 08:19
Show Gist options
  • Select an option

  • Save gausoft/87f70efc04a6912e09e3234ef30d3fb6 to your computer and use it in GitHub Desktop.

Select an option

Save gausoft/87f70efc04a6912e09e3234ef30d3fb6 to your computer and use it in GitHub Desktop.

Revisions

  1. gausoft created this gist Apr 6, 2023.
    54 changes: 54 additions & 0 deletions product_list.dart
    Original 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),
    ),
    );
    }
    }