簡體   English   中英

Flutter:在列中完全包含一個 ListView(無擴展)

[英]Flutter: fully include a ListView inside a Column (no Expanded)

在滾動視圖中,我想顯示一個小部件,然后是一個列表,然后是一個小部件。 有以下幾行:

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: <Widget>[
          Text("Top of ListView"),
          ListView.builder(
              itemCount: 100,
              itemBuilder: (context, index) => Text('Line $index')),
          Text("Below of ListView")
        ],
      ),
    );
  }

SingleChildScrollView的內容在哪里:

Text("Top of ListView"),
Text('Line 0'),
Text('Line 1'),
...
Text('Line 98'),
Text('Line 99'),
Text("Bottom of ListView")

當然這段代碼不起作用,因為 ListView 高度未定義。 Expanded包裝ListView不是解決方案,因為我希望Text("Top of ListView")在我開始滾動時消失,而Text("Bottom of ListView")僅在到達底部時出現(就好像它們是第一個一樣) item 和列表的最后一項)。

我的想法:

  • Text("Top of ListView")Text("Bottom of ListView")放在ListView :對於這個例子很容易做到,但在現實世界中,列表的前面或后面不會只有一個小部件。 凌亂且難以維護。
  • 直接將列表項添加到Column :也就是重新實現ListView
  • ListView包裝成一個Container ,固定高度對應計算出的ListView高度:不知道怎么測量childs。

有任何想法嗎 ?

您必須使用CustomScrollView來實現這一點:

CustomScrollView(
  slivers: [
    SliverList(
      delegate: SliverChildListDelegate([Text("Top of ListView")]),
    ),
    SliverList(
      delegate: SliverChildBuilderDelegate(
        (context, index) => Text('Line $index')
        childCount: 100,
      ),
    ),
    SliverList(
      delegate: SliverChildListDelegate([Text("Below of ListView")]),
    ),
  ],
),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM