私はまだFlutterにまったく慣れていません。ListView.builderを介して作成されたアイテムを含むページがあり、ユーザーが表示できるように、そのページ内のアイテムの数を表示する必要があります。これが私が達成したいことです:
Listviewは、私が作成したクラスのデータを使用します。
class Product {
int id;
String title;
String image;
String description;
Product({this.id, this.title, this.image, this.description});
}
List <Product> products= [
Product(
id: 1,
title: 'Item 1',
image: 'assets/images/item1.png',
description: 'some text'
),
Product(
id: 2,
title: 'Item 2',
image: 'assets/images/item2.png',
description: 'some text'
),
Product(
id: 3,
title: 'Item 3',
image: 'assets/images/item3.png',
description: 'some text'
),
Product(
id: 4,
title: 'Item 4',
image: 'assets/images/item4.png',
description: 'some text'
),
];
リストビューが作成したアイテムの数に言及し、上の画像に従って列ウィジェット内にそれらを表示したいと思います。
class WishList extends StatefulWidget {
@override
_WishListState createState() => _WishListState();
}
class _WishListState extends State<WishList> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
body: Column(
children: <Widget> [
Container(
child: Column(
children: [
Text('My Wishlist',),
SizedBox(height: 20),
Text('You have (num_of_items) Items'),
それ、どうやったら出来るの?どんな助けでも大歓迎です。
のリストをProducts
に渡しListView.builder
ますproducts
。したがって、アイテムの総数は-
products.length
あなたのコードは-
class WishList extends StatefulWidget {
@override
_WishListState createState() => _WishListState();
}
class _WishListState extends State<WishList> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
body: Column(
children: <Widget> [
Container(
child: Column(
children: [
Text('My Wishlist',),
SizedBox(height: 20),
Text('You have ${products.length} Items'), // <--------------
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加