Created
April 6, 2023 08:19
-
-
Save gausoft/87f70efc04a6912e09e3234ef30d3fb6 to your computer and use it in GitHub Desktop.
Liste des produits
This file contains 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 characters
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), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment