如何在listView.builder中使用小部件列表?

发布于 2025-02-12 12:29:33 字数 2198 浏览 3 评论 0原文


class CardPage extends StatelessWidget {
  String title;
  String description;
  CardPage({Key key,this.title,this.description}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    return Container(
      height: (108),
      child: Card(
        semanticContainer: true,
        clipBehavior: Clip.antiAliasWithSaveLayer,
        child: ElevatedButton(
          onPressed: () {},
          style: ElevatedButton.styleFrom(
            primary: Colors.white54,
          ),
          child: ListTile(
              title: Text(title,
                  style: themeData.textTheme.headline2),
              trailing: Padding(
                padding: const EdgeInsets.only(bottom: 8.0),
                child: Wrap(
                  spacing: 6,
                  children: [
                    IconButton(
                      onPressed: () {},
                      icon: Icon(Icons.fastfood, size: 25),
                    ),
                  ],
                ),
              ),
              subtitle: Text(description,
                  style: themeData.textTheme.subtitle1)),
        ),
      ),
    );
  }
}

这是我的卡页面。 这是我最喜欢的页面

import 'package:flutter/material.dart';

import 'card_page.dart';

class FavoritesPage extends StatefulWidget {
  List<CardPage> cardList;

  FavoritesPage({Key key,
  this.cardList,
  }) : super(key: key);

  @override
  State<FavoritesPage> createState() => _FavoritesPageState();
}

class _FavoritesPageState extends State<FavoritesPage> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body:SafeArea(
        child: ListView.builder(
          itemCount:widget.cardList.length,
            itemBuilder: (BuildContext context, int index) {
              return widget.cardList;
            }
            ),
      ),
    );
  }
}

如您所见,

The return type 'List<CardPage>' isn't a 'Widget', as required by the closure's context.

,我想在listView.builder中使用widget.cardlist。但是,错误说:我只想创建一个喜欢的pack,就像有很多卡片一样,我想将它粘贴在另一个页面上,名称是最喜欢的页面,但我做不到。


class CardPage extends StatelessWidget {
  String title;
  String description;
  CardPage({Key key,this.title,this.description}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final ThemeData themeData = Theme.of(context);
    return Container(
      height: (108),
      child: Card(
        semanticContainer: true,
        clipBehavior: Clip.antiAliasWithSaveLayer,
        child: ElevatedButton(
          onPressed: () {},
          style: ElevatedButton.styleFrom(
            primary: Colors.white54,
          ),
          child: ListTile(
              title: Text(title,
                  style: themeData.textTheme.headline2),
              trailing: Padding(
                padding: const EdgeInsets.only(bottom: 8.0),
                child: Wrap(
                  spacing: 6,
                  children: [
                    IconButton(
                      onPressed: () {},
                      icon: Icon(Icons.fastfood, size: 25),
                    ),
                  ],
                ),
              ),
              subtitle: Text(description,
                  style: themeData.textTheme.subtitle1)),
        ),
      ),
    );
  }
}

This is my CardPage class. And this is my FavoritePage class

import 'package:flutter/material.dart';

import 'card_page.dart';

class FavoritesPage extends StatefulWidget {
  List<CardPage> cardList;

  FavoritesPage({Key key,
  this.cardList,
  }) : super(key: key);

  @override
  State<FavoritesPage> createState() => _FavoritesPageState();
}

class _FavoritesPageState extends State<FavoritesPage> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body:SafeArea(
        child: ListView.builder(
          itemCount:widget.cardList.length,
            itemBuilder: (BuildContext context, int index) {
              return widget.cardList;
            }
            ),
      ),
    );
  }
}

As you can see, i want to use widget.cardList in listView.builder but the error says:

The return type 'List<CardPage>' isn't a 'Widget', as required by the closure's context.

I just want to create a favoritePage like there are a lot of cards and i wanted to paste it another page which name is favoritePage but i couldn't do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云之铃。 2025-02-19 12:29:34

如您所见,您正在忽略索引。
这样使用:

 return widget.cardList[index];

As you can see, you are ignoring the index..
use it like this:

 return widget.cardList[index];
貪欢 2025-02-19 12:29:34

由于您已经有一个小部件列表,因此您不必添加listView.builder

return Scaffold(
      body:SafeArea(
       child: SingleChildScrollView(
        child: Column(
        mainAxisSize : MainAxisSize.min,
         children : widget.cardList,
        )
      ),
    )

Since you already have a list of widgets you dont have to add a ListView.builder

return Scaffold(
      body:SafeArea(
       child: SingleChildScrollView(
        child: Column(
        mainAxisSize : MainAxisSize.min,
         children : widget.cardList,
        )
      ),
    )
没︽人懂的悲伤 2025-02-19 12:29:34

检查这个。我希望这能解决您的问题。

 body: ListView.builder(
    itemCount: cities.length,
    itemBuilder: (context, index) {
    //..................................................ListTile
      return ListTile(
        leading: CircleAvatar(
          child: Text(cities[index].cityName[index][0]),
        ),
        title: Text(cities[index].cityName),
        subtitle: Text(cities[index].countryName),
        trailing: Text("\$" + prices[index]),
      );
    },
  ),

Check this one. I hope this will solve your problem.

 body: ListView.builder(
    itemCount: cities.length,
    itemBuilder: (context, index) {
    //..................................................ListTile
      return ListTile(
        leading: CircleAvatar(
          child: Text(cities[index].cityName[index][0]),
        ),
        title: Text(cities[index].cityName),
        subtitle: Text(cities[index].countryName),
        trailing: Text("\
quot; + prices[index]),
      );
    },
  ),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文