91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Flutter Sliver滾動組件怎么用

發布時間:2021-11-18 09:07:36 來源:億速云 閱讀:546 作者:小新 欄目:開發技術

這篇文章主要介紹了Flutter Sliver滾動組件怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Flutter Sliver滾動組件

 SliverList & SliverGrid

需要同時滾動ListView和GridView時可以使用SliverList和SliverGrid。

Flutter Sliver滾動組件怎么用

CustomScrollView(
    slivers: [
        SliverList(
            delegate: SliverChildBuilderDelegate(
                (context, index) {
                    return Container(
                        height: 50,
                        color: Colors.primaries[index % Colors.primaries.length],
                    );
                },
                childCount: 5,
            ),
        ),
        SliverGrid(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: 5,
                mainAxisSpacing: 5,
            ),
            delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                    return Container(
                        color: Colors.primaries[index % Colors.primaries.length],
                    );
                },
                childCount: 20,
            ),
        ),
    ],
)

SliverAppBar

pinned:是否固定在屏幕頂部。

expandedHeight:展開區域的高度。

flexibleSpace:展開取消顯示內容。

Flutter Sliver滾動組件怎么用

CustomScrollView(
    slivers: [
        SliverAppBar(
            pinned: true,
            expandedHeight: 200,
            flexibleSpace: FlexibleSpaceBar(
                title: const Text("SliverAppBar"),
                background: Image.asset("images/avatar.jpg", fit: BoxFit.cover),
            ),
        ),
        SliverFixedExtentList(
            delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                    return Container(
                        alignment: Alignment.center,
                        color: Colors.primaries[index % Colors.primaries.length],
                        child: Text("$index"),
                    );
                },
            ),
            itemExtent: 50.0,
        ),
    ],
)

SliverPersistentHeader

SliverPersistentHeader組件可以控制滾動的最大高度和最小高度,類似SliverAppBar效果。

build:顯示內容。

maxExtent & minExtent:滾動的高度范圍。

shouldRebuild:是否需要更新。

Flutter Sliver滾動組件怎么用

CustomScrollView(
    slivers: [
        SliverPersistentHeader(
            pinned: true,
            delegate: MySliverPersistentHeaderDelegate(),
        ),
        SliverGrid(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: 5,
                mainAxisSpacing: 5,
            ),
            delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                    return Container(
                        color: Colors.primaries[index % Colors.primaries.length],
                    );
                },
            ),
        ),
    ],
)
class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
    @override
    Widget build(
        BuildContext context, double shrinkOffset, bool overlapsContent) {
        return Container(
            color: Colors.blue,
            alignment: Alignment.center,
            child: Text(
                "hello world",
                style: TextStyle(color: Colors.white),
            ),
        );
    }

    @override
    double get maxExtent => 200;

    @override
    double get minExtent => 50;

    @override
    bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
        return false;
    }
}

SliverToBoxAdapter

CustomScrollView只能包含Sliver組件,如果需要使用普通組件可以使用SliverToBoxAdapter。

Flutter Sliver滾動組件怎么用

CustomScrollView(
    slivers: [
        SliverToBoxAdapter(
            child: Container(
                height: 200,
                color: Colors.black26,
                alignment: Alignment.center,
                child: Text("hello world"),
            ),
        ),
        SliverList(
            delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                    return Container(
                        height: 60,
                        color: Colors.primaries[index % Colors.primaries.length],
                    );
                },
                childCount: 50,
            ),
        ),
    ],
)

CustomScrollView & NestedScrollView

CustomScrollView組件可以將多個組件組合在一起,具有統一的滾動效果,但是CustomScrollView只能嵌套Sliver系列的組件,如SliverList、SliverGrid、SliverPadding、SliverAppBar等。

NestedScrollView可以協調兩個滾動組件滑動。NestedScrollView在邏輯上將可滾動組件分為header和body兩部分,heade部分只能接收Sliver類型的組件,而body部分可以接收任意類型的組件。

NestedScrollView+SliverAppBar+SliverFixedExtentList+ListView

Flutter Sliver滾動組件怎么用

NestedScrollView(
    //Sliver組件
    headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
            SliverAppBar(
                title: const Text("嵌套ListView"),
                pinned: true, //固定AppBar             
                forceElevated: true,
            ),
            SliverFixedExtentList(
                itemExtent: 50,
                delegate: SliverChildBuilderDelegate(
                    (BuildContext context, int index) {
                        return ListTile(title: Text("$index"));
                    },
                    childCount: 5,
                ),
            ),
        ];
    },
    //滾動組件
    body: ListView.builder(
        padding: const EdgeInsets.all(8),
        physics: const ClampingScrollPhysics(), //需要
        itemCount: 30,
        itemBuilder: (BuildContext context, int index) {
            return SizedBox(
                height: 50,
                child: Center(child: Text("item $index")),
            );
        },
    ),
)
NestedScrollView+SliverAppBar+CustomScrollView

Flutter Sliver滾動組件怎么用

NestedScrollView(
    headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
            SliverAppBar(
                floating: true,
                snap: true,
                expandedHeight: 200,
                forceElevated: innerBoxIsScrolled,
                flexibleSpace: FlexibleSpaceBar(
                    background: Image.asset(
                        "images/logo.png",
                        fit: BoxFit.cover,
                    ),
                ),
            ),
        ];
    },
    body: CustomScrollView(
        slivers: [buildSliverList(50)],
    ),
)
優化聯動效果

SliverAppBar+CustomScrollView組合,當反向滑動時,SliverAppBar就會整體回到屏幕頂部,出現遮擋問題,為了解決該問題,可以用在header里用SliverOverlapAbsorber組件包裹SliverAppBar,body里Sliver列表最前面添加一個SliverOverlapInjector。

NestedScrollView(
    headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
            SliverOverlapAbsorber(
                handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                sliver: SliverAppBar(
                    floating: true,
                    snap: true,
                    expandedHeight: 200,
                    forceElevated: innerBoxIsScrolled,
                    flexibleSpace: FlexibleSpaceBar(
                        background: Image.asset(
                            "images/logo.png",
                            fit: BoxFit.cover,
                        ),
                    ),
                ),
            ),
        ];
    },
    body: Builder(
        builder: (BuildContext context) {
            return CustomScrollView(
                slivers: [
                    SliverOverlapInjector(
                        handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                    ),
                    buildSliverList(50),
                ],
            );
        },
    ),
)
NestedScrollView+TabBarView

Flutter Sliver滾動組件怎么用

class MyPageView extends StatefulWidget {
    late List<String> tabs;

    MyPageView({Key? key, required this.tabs}) : super(key: key);

    @override
    State<StatefulWidget> createState() {
        return _MyPageViewState();
    }
}

class _MyPageViewState extends State<MyPageView>
    with SingleTickerProviderStateMixin {
    late TabController _controller;

    @override
    void initState() {
        super.initState();
        _controller = TabController(length: widget.tabs.length, vsync: this);
    }

    @override
    void dispose() {
        super.dispose();
        _controller.dispose();
    }

    @override
    Widget build(BuildContext context) {
        return NestedScrollView(
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                return [
                    SliverOverlapAbsorber(
                        handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                        sliver: SliverAppBar(
                            title: const Text("hi Flutter"),
                            floating: true,
                            snap: true,
                            forceElevated: innerBoxIsScrolled,
                            bottom: TabBar(
                                controller: _controller,
                                tabs: widget.tabs.map((e) => Tab(text: e)).toList(),
                            ),
                        ),
                    ),
                ];
            },
            body: TabBarView(
                controller: _controller,
                children: widget.tabs.map((e) {
                    return Builder(builder: (BuildContext context) {
                        return CustomScrollView(
                            key: PageStorageKey(e),
                            slivers: [
                                SliverOverlapInjector(
                                    handle:
                                    NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                                ),
                                SliverPadding(
                                    padding: const EdgeInsets.all(9),
                                    sliver: buildSliverList(50),
                                ),
                            ],
                        );
                    });
                }).toList(),
            ),
        );
    }
}

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Flutter Sliver滾動組件怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

商丘市| 湛江市| 威远县| 吉安市| 宜州市| 大足县| 吴川市| 商水县| 迁西县| 定安县| 麟游县| 原阳县| 玛纳斯县| 金沙县| 青冈县| 清苑县| 伊宁市| 奉新县| 庄浪县| 西华县| 道孚县| 彰化市| 五台县| 嘉鱼县| 漯河市| 岳阳县| 乳源| 黑龙江省| 安乡县| 定远县| 即墨市| 盐池县| 滨州市| 阳谷县| 宜阳县| 米泉市| 简阳市| 万安县| 河曲县| 沙坪坝区| 渑池县|