Flutter---SingleChildScrollView

基础用法

Dart 复制代码
 body: SingleChildScrollView(  // ← 包裹需要滚动的内容
          child: Column(
            children: [
              for (int i = 1; i <= 50; i++)
                Container(
                  margin: EdgeInsets.all(8),
                  padding: EdgeInsets.all(20),
                  color: Colors.blue[100 * (i % 9 + 1)],
                  child: Text(
                    "项目 $i",
                    style: TextStyle(fontSize: 18),
                  ),
                ),
            ],
          ),
        ),

效果图

SingleChildScrollView中的参数

Dart 复制代码
    super.key,
    this.scrollDirection = Axis.vertical, //滚动方向
    this.reverse = false, //是否反向滚动
    this.padding, //内边距
    this.primary, //是否使用主滚动视图 ,参数为true,false
    this.physics, //滚动物理效果
    this.controller, //滚动控制器
    this.child, //子组件
    this.dragStartBehavior = DragStartBehavior.start,
    this.clipBehavior = Clip.hardEdge,
    this.hitTestBehavior = HitTestBehavior.opaque,
    this.restorationId, //状态恢复ID
    this.keyboardDismissBehavior, //键盘行为
①改变滚动方向

把垂直滚动改为水平滑动,Column改为Row

代码

Dart 复制代码
return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("SingleChildScrollView")),
        body: SingleChildScrollView(  // ← 包裹需要滚动的内容
          scrollDirection: Axis.horizontal, //水平滑动
          child: Row(
            children: [
              for (int i = 1; i <= 50; i++)
                Container(
                  margin: EdgeInsets.all(8),
                  padding: EdgeInsets.all(20),
                  color: Colors.blue[100 * (i % 9 + 1)],
                  child: Text(
                    "项目 $i",
                    style: TextStyle(fontSize: 18),
                  ),
                ),
            ],
          ),
        ),
      ),
    );

效果图

②设置反向滚动
Dart 复制代码
return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("SingleChildScrollView")),
        body: SingleChildScrollView( 
          reverse: true, //反向滚动
          child: Column(
            children: [
              for (int i = 1; i <= 50; i++)
                Container(
                  margin: EdgeInsets.all(8),
                  padding: EdgeInsets.all(20),
                  color: Colors.blue[100 * (i % 9 + 1)],
                  child: Text(
                    "项目 $i",
                    style: TextStyle(fontSize: 18),
                  ),
                ),
            ],
          ),
        ),
      ),
    );

效果图:一开始就显示滑动到了底部

③设置内边距

代码

Dart 复制代码
return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("SingleChildScrollView")),
        body: SingleChildScrollView(
          padding: EdgeInsets.only(top: 200), //反向滚动
          child: Column(
            children: [
              for (int i = 1; i <= 50; i++)
                Container(
                  margin: EdgeInsets.all(8),
                  padding: EdgeInsets.all(20),
                  color: Colors.blue[100 * (i % 9 + 1)],
                  child: Text(
                    "项目 $i",
                    style: TextStyle(fontSize: 18),
                  ),
                ),
            ],
          ),
        ),
      ),
    );

效果图

④使用物理滚动效果

根据自己需要的效果做相关的选择

Dart 复制代码
// 1. 默认物理效果(Android 风格)
ScrollPhysics physics = ClampingScrollPhysics();

// 2. 弹跳物理效果(iOS 风格)
ScrollPhysics physics = BouncingScrollPhysics();

// 3. 始终可滚动
ScrollPhysics physics = AlwaysScrollableScrollPhysics();

// 4. 永远不可滚动
ScrollPhysics physics = NeverScrollableScrollPhysics();

// 5. 固定效果(无惯性)
ScrollPhysics physics = FixedExtentScrollPhysics();
⑤滚动控制器

后面再写

相关推荐
用户059540174465 分钟前
Redis持久化踩坑实录:这个数据丢失Bug让我排查了6小时
前端·css
用户2136610035727 分钟前
VueRouter进阶-动态路由与嵌套路由
前端·vue.js
梯度不陡9 分钟前
Signal #17:Agent 开始进入组织系统
前端·javascript
何智超12 分钟前
AI 微前端性能优化之旅(上):复盘
前端·vibecoding
许我半盏清茶14 分钟前
前端路由:理解 hash 路由和 history 路由原理
前端·react.js
胡萝卜术20 分钟前
从暴力到Z字形消元:力扣240「搜索二维矩阵II」的降维打击之路
前端·javascript·面试
比老马还六24 分钟前
Bipes-Blockly项目二次开发/Coze智能体(十)
前端·嵌入式
27 分钟前
Vue 3 组件封装与使用:保姆级教程
前端
星辰30 分钟前
深入浅出 Android AOA 协议:通信流程与设备切换附着机制解析
前端
恋猫de小郭1 小时前
Amper 正式转正 Kotlin Toolchain ,Gradle 未来何去何从
android·前端·flutter