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();
⑤滚动控制器

后面再写

相关推荐
子兮曰5 小时前
Bun v1.3.14 深度解析:Image API、HTTP/3、全局虚拟存储与五十项变革
前端·后端·bun
kyriewen6 小时前
今天,百年巨头一次砍了9200人,而一个离职科学家的实话让全网睡不着觉
前端·openai·ai编程
问心无愧05136 小时前
ctf show web 入门42
android·前端·android studio
kyriewen6 小时前
老板逼我上AI,我偷偷在浏览器里跑LLaMA,省下20万API费
前端·react.js·llm
Beginner x_u7 小时前
前端八股整理(手写 02)|数组转树、数组扁平化、随机打乱一个数组
前端·数组·数组转树·数组扁平化
KaMeidebaby7 小时前
卡梅德生物技术快报|禽类成纤维细胞 FISH 实验:鸟类性别染色体基因定位技术实现与数据验证
前端·数据库·其他·百度·新浪微博
天若有情6737 小时前
前端高阶性能优化:跳出传统懒加载与预加载,基于用户行为做轻量预判加载
前端·性能优化
小小小小宇7 小时前
前端转后端:SQL 是什么
前端
张元清8 小时前
React Observer Hooks:7 种监听 DOM 而不写样板代码的方式
前端·javascript·面试
广州华水科技8 小时前
单北斗GNSS变形监测是什么?主要有怎样的应用与优势?
前端