简体   繁体   English

在 CustomScrollView 中添加函数返回的 Slivers 动态列表

[英]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不正确,

  • few errors with extra braces (could be typos)额外大括号的错误很少(可能是拼写错误)
  • you should return your buildRow你应该返回你的buildRow
  • you should return SliverList你应该返回SliverList

Not sure what your model.data or buildRow looks like but here is a quick example,不确定您的model.databuildRow是什么样子,但这是一个简单的示例,

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.

相关问题 Flutter CustomScrollView slivers 堆叠 - Flutter CustomScrollView slivers stacking 自定义 Flutter 小部件以返回两个小部件以与 CustomScrollView / slivers 一起使用 - Custom Flutter widget to return two widgets for use with CustomScrollView / slivers 在条子上添加 Flutter AlertDialog? - Add Flutter AlertDialog over slivers? 未来建设者<dynamic>在 CustomScrollView 内颤动 - FutureBuilder<dynamic> inside CustomScrollView flutter 具有 SliverFillRemaining 内容的 CustomScrollView 条不滚动 - CustomScrollView slivers with contents in SliverFillRemaining not scrolling 如何通过更改 UI 向 Slivers Flutter 添加新元素? - how to add new element to Slivers Flutter with changing the UI? Flutter:customScrollView 内可滚动内容的动态高度(具有粘性行为) - Flutter : dynamic height for scrollable content inside customScrollView (with sticky behaviors) 将条子添加到反向 CustomScrollView 或 ListView 时将粘性标题固定在顶部 - Pin sticky headers on top when adding slivers to a reverse CustomScrollView or ListView 如何在 Flutter 的 CustomScrollView 中拥有 ListView.builder(大列表)? - How to have a ListView.builder (big list) in CustomScrollView in Flutter? 带有 Flutter 的 CustomScrollView 中的页面的 PageView - PageView with pages in CustomScrollView with Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM