1 | initial version |
Here's an example code for including a product in a list using Flutter:
class Product {
final String name;
final double price;
Product(this.name, this.price);
}
class ProductList extends StatelessWidget {
final List<Product> products = [
Product('Apple', 1.0),
Product('Banana', 0.5),
Product('Orange', 0.8)
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: products.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(products[index].name),
trailing: Text('\$${products[index].price.toString()}'),
);
});
}
}
In the code above, we define a Product
class with name
and price
properties. We then create a ProductList
class with a products
list that contains three Product
objects.
In the build
method, we use a ListView.builder
widget to build a list of ListTile
widgets, with each tile displaying the name and price of a Product
. We use the itemCount
property of the ListView.builder
to set the number of items in the list, and the itemBuilder
property to define how each item should be built.
Ultimately, this results in a list of products being displayed in a Flutter app.