我正在将以前函数中的内容移至无状态小部件中。我想知道是否可以返回一个带有 Widget 类型的列表,如果可以,我将如何执行此操作?
我尝试了一些随机的东西,但这些东西只会进一步破坏我的应用程序。
class returnList1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return <Widget>[
InfoCard(),
InfoCard(),
InfoCard(),
Stack(
children: <Widget>[
IconButton(
icon: Icon(Icons.more_horiz),
iconSize:
35.0, // TODO: scale icons based on mediaquery height and width
color: Colors.black,
onPressed: () {
print("More Pressed!");
},
),
Padding(
padding: EdgeInsets.fromLTRB(
MediaQuery.of(context).size.width * 0.034,
MediaQuery.of(context).size.height * 0.02839,
0.0,
0.0),
child: Material(
color: Colors.transparent,
elevation: 7.5,
borderRadius: BorderRadius.circular(200.0),
child: Container(
height: MediaQuery.of(context).size.height * 0.009675,
width: MediaQuery.of(context).size.width * 0.0559,
color: Colors.transparent,
),
),
),
],
)
];
}
}
当我运行代码时,我得到“getter 'length' 被调用为 null。在我的 IDE 中,我得到“返回类型 'List' 不是 'Widget',如方法 'build 所定义的那样'."
最佳答案
您无法使用 build
方法返回列表。 build
方法只能返回一个Widget
。
如果您想返回一个列表,那么您可以创建一个函数并返回一个列表,该列表可以在任何多个子容器的子属性中使用。
这是代码。
_returnList (BuildContext context) {
return [
InfoCard(),
InfoCard(),
InfoCard(),
Stack(
children: <Widget>[
IconButton(
icon: Icon(Icons.more_horiz),
iconSize:
35.0, // TODO: scale icons based on mediaquery height and width
color: Colors.black,
onPressed: () {
print("More Pressed!");
},
),
Padding(
padding: EdgeInsets.fromLTRB(
MediaQuery.of(context).size.width * 0.034,
MediaQuery.of(context).size.height * 0.02839,
0.0,
0.0),
child: Material(
color: Colors.transparent,
elevation: 7.5,
borderRadius: BorderRadius.circular(200.0),
child: Container(
height: MediaQuery.of(context).size.height * 0.009675,
width: MediaQuery.of(context).size.width * 0.0559,
color: Colors.transparent,
),
),
),
],
)
];
}
关于dart - Flutter stateleswidget 返回列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53880504/