[英]Add dynamic list of Slivers returned by a function in CustomScrollView in flutter
I am trying to add a dynamic list of Sliverlist in CustomScrollView but it doesn't seem to work.我正在尝试在 CustomScrollView 中添加 Sliverlist 的动态列表,但它似乎不起作用。 The method correctly returns the list of slivers, which I verified by debugging.该方法正确返回了我通过调试验证的条子列表。 Here is the sample code of what I am trying to achieve这是我想要实现的示例代码
CustomScrollView(
key: PageStorageKey<String>(myKey),
slivers: _getSlivers(model.data, context),
),
Here is the _getSlivers metod:这是 _getSlivers 方法:
List<Widget> _getSlivers(List myList, BuildContext context)
{
List<Widget> slivers = myList.map((key) => SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
buildRow(index)
}
},
childCount: myList.length,
),
),
).toList();
return slivers;
}
}
Your _getSlivers
is not correct,你的_getSlivers
不正确,
buildRow
你应该返回你的buildRow
SliverList
你应该返回SliverList
Not sure what your model.data
or buildRow
looks like but here is a quick example,不确定您的model.data
或buildRow
是什么样子,但这是一个简单的示例,
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
List<String> list = ['Title 1', 'Title 2', 'Title 3', 'Title 4', 'Title 5', 'Title 6', 'Title 7', 'Title 8', 'Title 9', 'Title 10', 'Title 11', 'Title 12', 'Title 13', 'Title 14', 'Title 15', 'Title 16', 'Title 17', 'Title 18', 'Title 19', 'Title 20'];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: CustomScrollView(
//key: PageStorageKey<String>(myKey),
slivers: <Widget>[
SliverAppBar(
title: Text('Test'),
),
_getSlivers(list, context),
]
),
));
}
SliverList _getSlivers(List myList, BuildContext context) {
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return buildRow(myList[index]);
},
childCount: myList.length,
),
);
}
buildRow(String title) {
return Padding(padding: const EdgeInsets.all(15.0),child: Text(title, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18.0)));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.