Flutter 常用滚动组件使用场景

1. 使用 SingleChildScrollView

如果页面的内容较少并且你希望整体可滚动,可以使用 SingleChildScrollView 来包裹页面的所有内容。

示例:

dart 复制代码
SingleChildScrollView(
  child: Column(
    children: [
      Container(
        height: 200,
        color: Colors.blue,
        child: const Center(child: Text('部分内容')),
      ),
      Container(
        height: 200,
        color: Colors.green,
        child: const Center(child: Text('其他内容')),
      ),
      // 可以继续添加更多内容
    ],
  ),
)

2. 使用 ListView

如果你的内容是一个列表项,ListView 是最适合的滚动组件。ListView 会根据列表项的数量自动滚动,可以通过 ListView.builder 来动态构建列表项。

示例:

dart 复制代码
ListView(
  children: [
    Container(
      height: 200,
      color: Colors.blue,
      child: const Center(child: Text('部分内容')),
    ),
    Container(
      height: 200,
      color: Colors.green,
      child: const Center(child: Text('其他内容')),
    ),
    // 可以继续添加更多内容
  ],
)

3. 混合使用滚动组件

你可以根据页面布局需求,结合 SingleChildScrollViewListView 等进行嵌套使用。

示例:

dart 复制代码
SingleChildScrollView(
  child: Column(
    children: [
      Container(
        height: 200,
        color: Colors.blue,
        child: const Center(child: Text('顶部内容')),
      ),
      ListView.builder(
        shrinkWrap: true,  // 设置为 true 让 ListView 在嵌套滚动时不占据全部空间
        physics: NeverScrollableScrollPhysics(),  // 禁止内嵌的 ListView 滚动
        itemCount: 10,
        itemBuilder: (context, index) {
          return Container(
            height: 100,
            color: Colors.orange,
            child: Center(child: Text('列表项 $index')),
          );
        },
      ),
      Container(
        height: 200,
        color: Colors.green,
        child: const Center(child: Text('底部内容')),
      ),
    ],
  ),
)

4. 滚动时禁用顶部栏(SliverAppBar)

如果页面有一个动态的顶部栏(例如 SliverAppBar),可以通过使用 CustomScrollView 来实现与滚动内容的配合。

示例:

dart 复制代码
CustomScrollView(
  slivers: [
    SliverAppBar(
      expandedHeight: 200.0,
      floating: true,
      pinned: true,
      flexibleSpace: FlexibleSpaceBar(
        title: Text('标题'),
        background: Image.network('https://example.com/image.jpg', fit: BoxFit.cover),
      ),
    ),
    SliverList(
      delegate: SliverChildBuilderDelegate(
        (context, index) {
          return Container(
            height: 100,
            color: Colors.orange,
            child: Center(child: Text('列表项 $index')),
          );
        },
        childCount: 50,
      ),
    ),
  ],
)

小结:

  • SingleChildScrollView:适用于页面内容较少的场景。
  • ListView:适用于有多个列表项且需要滚动的场景。
  • CustomScrollView :适用于结合 SliverAppBar 或自定义滚动效果的场景。
相关推荐
coooliang8 小时前
Flutter 中的单例模式
javascript·flutter·单例模式
coooliang8 小时前
Flutter项目中设置安卓启动页
android·flutter
JIngles1238 小时前
flutter将utf-8编码的字节序列转换为中英文字符串
java·javascript·flutter
B.-11 小时前
在 Flutter 中实现文件读写
开发语言·学习·flutter·android studio·xcode
freflying111920 小时前
使用jenkins构建Android+Flutter项目依赖自动升级带来兼容性问题及Jenkins构建速度慢问题解决
android·flutter·jenkins
机器瓦力1 天前
Flutter应用开发:对象存储管理图片
flutter
江上清风山间明月1 天前
Flutter最简单的路由管理方式Navigator
android·flutter·ios·路由·页面管理·navigator
weixin_411191841 天前
FlutterAssetsGenerator插件的使用
flutter
神秘_博士2 天前
自制AirTag,支持安卓/鸿蒙/PC/Home Assistant,无需拥有iPhone
arm开发·python·物联网·flutter·docker·gitee
陈皮话梅糖@2 天前
Flutter 网络请求与数据处理:从基础到单例封装
flutter·网络请求