代码之家  ›  专栏  ›  技术社区  ›  bora399

如何在ListView中使用小部件列表。建设者

  •  0
  • bora399  · 技术社区  · 1 年前
    
    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)),
            ),
          ),
        );
      }
    }
    

    这是我的CardPage课程。这是我最喜欢的页面类

    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;
                }
                ),
          ),
        );
      }
    }
    
    

    如你们所见,我想使用widget。listView中的cardList。但错误显示:

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

    我只是想创建一个favoritePage,就像有很多卡片一样,我想把它粘贴到另一个名为favoritePage的页面上,但我做不到。

    3 回复  |  直到 1 年前
        1
  •  1
  •   Kaushik Chandru    1 年前

    因为你已经有了一个小部件列表,所以你不必添加ListView。建设者

    return Scaffold(
          body:SafeArea(
           child: SingleChildScrollView(
            child: Column(
            mainAxisSize : MainAxisSize.min,
             children : widget.cardList,
            )
          ),
        )
    
        2
  •  0
  •   peretsoli    1 年前

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

     return widget.cardList[index];
    
        3
  •  0
  •   Uncle Roger    1 年前

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

     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]),
          );
        },
      ),