如何在listView.builder中使用小部件列表?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如您所见,您正在忽略索引。
这样使用:
As you can see, you are ignoring the index..
use it like this:
由于您已经有一个小部件列表,因此您不必添加listView.builder
Since you already have a list of widgets you dont have to add a ListView.builder
检查这个。我希望这能解决您的问题。
Check this one. I hope this will solve your problem.