【Flutter】Flutter 布局组件 ( FractionallySizedBox 组件 | Stack 布局组件 | Positioned 组件 )

article/2025/9/28 19:50:46

文章目录

  • 一、FractionallySizedBox 组件
  • 二、Stack 布局组件
  • 三、Positioned 组件
  • 四、 完整代码示例
  • 五、 相关资源





一、FractionallySizedBox 组件



FractionallySizedBox 组件 : 可控制组件在水平/垂直方向上填充满父容器 ;

class FractionallySizedBox extends SingleChildRenderObjectWidget {const FractionallySizedBox({Key key,this.alignment = Alignment.center,this.widthFactor,this.heightFactor,Widget child,}) : assert(alignment != null),assert(widthFactor == null || widthFactor >= 0.0),assert(heightFactor == null || heightFactor >= 0.0),super(key: key, child: child);
}

FractionallySizedBox 组件用法 :

  • 设置宽度充满父容器 : widthFactor 字段设置 ;
  • 设置高度填充满父容器 : heightFactor 字段设置 ;
  • 设置平铺的组件 : child 字段设置 Widget 组件 ;
// 水平/垂直方向平铺组件
FractionallySizedBox(// 设置宽度充满父容器widthFactor: 1,// 设置高度填充满父容器heightFactor: 1,// 要设置的水平 / 垂直方向的平铺操作的组件child: 要控制平铺的组件 ( Widget 类型 ),),
)

代码示例 :

// 水平/垂直方向平铺组件
FractionallySizedBox(// 设置宽度充满父容器widthFactor: 1,// 要设置的水平 / 垂直方向的平铺操作的组件child: Container(decoration: BoxDecoration(color: Colors.black),child: Text("高度自适应, 宽度充满父容器",style: TextStyle(color: Colors.amberAccent),),),
)




二、Stack 布局组件



Stack 布局组件 : 相当于帧布局 ;

class Stack extends MultiChildRenderObjectWidget {/// Creates a stack layout widget.////// By default, the non-positioned children of the stack are aligned by their/// top left corners.Stack({Key key,this.alignment = AlignmentDirectional.topStart,this.textDirection,this.fit = StackFit.loose,this.overflow = Overflow.clip,List<Widget> children = const <Widget>[],}) : super(key: key, children: children);
}

Stack 布局组件用法 : 在 children 字段设置若干 Widget 组件 , 最后一个组件在最顶端显示 , 覆盖前面的组件 ;


代码示例 :

// 帧布局
Stack(children: <Widget>[Image.network("https://img-blog.csdnimg.cn/20210301145757946.png",width: 100,height: 100,),Image.network("https://img-blog.csdnimg.cn/20210228180808133.png",width: 25,height: 25,),],
),




三、Positioned 组件



Positioned 组件 : 用于在 Stack 布局中指定某个组件的位置

class Positioned extends ParentDataWidget<Stack> {/// Creates a widget that controls where a child of a [Stack] is positioned.////// Only two out of the three horizontal values ([left], [right],/// [width]), and only two out of the three vertical values ([top],/// [bottom], [height]), can be set. In each case, at least one of/// the three must be null.////// See also://////  * [Positioned.directional], which specifies the widget's horizontal///    position using `start` and `end` rather than `left` and `right`.///  * [PositionedDirectional], which is similar to [Positioned.directional]///    but adapts to the ambient [Directionality].const Positioned({Key key,this.left, // 设置组件距离左侧距离this.top, // 设置组件距离顶部距离this.right, // 设置组件距离右侧距离this.bottom, // 设置组件距离底部距离this.width, // 设置组件宽度this.height, // 设置组件高度@required Widget child,}) : assert(left == null || right == null || width == null),assert(top == null || bottom == null || height == null),super(key: key, child: child);
}

Positioned 组件用法 :

  • 设置组件宽度 : width 字段 ;
  • 设置组件高度 : height 字段 ;
  • 设置组件距离左侧距离 : left 字段 ;
  • 设置组件距离顶部距离 : top 字段 ;
  • 设置组件距离右侧距离 : right 字段 ;
  • 设置组件距离底部距离 : bottom 字段 ;

代码示例 :

// 帧布局
Stack(children: <Widget>[Image.network("https://img-blog.csdnimg.cn/20210301145757946.png",width: 100,height: 100,),// 设置组件位置在 Stack 的相对位置Positioned(right: 0, // 距离右侧 0 距离bottom: 0, // 距离底部 0 距离// 设置约束的组件位置child: Image.network("https://img-blog.csdnimg.cn/20210228180808133.png",width: 25,height: 25,),),],
),




四、 完整代码示例



完整代码示例 :

import 'package:flutter/material.dart';class LayoutPage extends StatefulWidget {@override_LayoutPageState createState() => _LayoutPageState();
}class _LayoutPageState extends State<LayoutPage> {/// 当前被选中的底部导航栏索引int _currentSelectedIndex = 0;// This widget is the root of your application.@overrideWidget build(BuildContext context) {// 文本组件样式 , 可以设置给 Text 文本组件// 设置字体大小 20, 颜色红色TextStyle textStyle = TextStyle(fontSize: 20, color: Colors.red);return MaterialApp(title: '布局组件示例',theme: ThemeData(primarySwatch: Colors.blue,),home: Scaffold(// 顶部标题栏appBar: AppBar(title: Text('布局组件示例'),),// 底部导航栏 BottomNavigationBar 设置// items 可以设置多个 BottomNavigationBarItembottomNavigationBar: BottomNavigationBar(// 设置当前选中的底部导航索引currentIndex: _currentSelectedIndex,// 设置点击底部导航栏的回调事件 , index 参数是点击的索引值onTap: (index){// 回调 StatefulWidget 组件的 setState 设置状态的方法 , 修改当前选中索引// 之后 BottomNavigationBar 组件会自动更新当前选中的选项卡setState(() {// 改变 int _currentSelectedIndex 变量的状态_currentSelectedIndex = index;});},// 条目items: [// 设置底部导航栏条目, 每个条目可以设置一个图标BottomNavigationBarItem(// 默认状态下的图标icon: Icon(Icons.home, color: Colors.grey,),// 激活状态下的图标activeIcon: Icon(Icons.home, color: Colors.red,),// 设置标题title: Text("主页")),// 设置底部导航栏条目, 每个条目可以设置一个图标BottomNavigationBarItem(// 默认状态下的图标icon: Icon(Icons.settings, color: Colors.grey,),// 激活状态下的图标activeIcon: Icon(Icons.settings, color: Colors.red,),// 设置标题title: Text("设置"))],),// 设置悬浮按钮floatingActionButton: FloatingActionButton(onPressed: (){print("悬浮按钮点击");},child: Text("悬浮按钮组件"),),// Container 容器使用body:_currentSelectedIndex == 0 ?// 刷新指示器组件RefreshIndicator(// 显示的内容child: ListView(children: <Widget>[Container( // 对应底部导航栏设置选项卡// 设置容器的装饰器 , BoxDecoration 是最常用的装饰器// 可以自行查看 BoxDecoration 中可以设置的属性decoration: BoxDecoration(color: Colors.white),// 设置 child 子组件居中方式, 居中放置alignment: Alignment.center,// 子组件, 子组件设置为一个 Column 组件child: Column(// Column 子组件, 这里设置 Text 文本组件children: <Widget>[Text("主页面选项卡, 下拉刷新"),// 水平方向排列的线性布局Row(children: <Widget>[// 原始图片, 用于对比Image.network("https://img-blog.csdnimg.cn/20210301145757946.png",width: 100,height: 100,),// 圆形裁剪组件 , 将 child 布局裁剪成圆形ClipOval(// 使用 SizedBox 组件约束布局大小child: SizedBox(width: 100,height: 100,// 使用 SizedBox 约束该 Image 组件大小child: Image.network("https://img-blog.csdnimg.cn/20210301145757946.png"),),),Padding(// 设置内边距 5padding: EdgeInsets.all(15),// 方形裁剪组件 , 将组件裁剪成方形child: ClipRRect(// 设置裁剪圆角, 四个角设置半径为 10 的圆角borderRadius: BorderRadius.all(Radius.circular(10)),// 修改透明度组件 , 这里设置 50% 透明度child: Opacity(opacity: 0.5,// 设置 100x100 大小的图片组件child: Image.network("https://img-blog.csdnimg.cn/20210301145757946.png",width: 100,height: 100,),),),),],),// 设置一个布局容器 , 用于封装 PageView 组件Container(// 设置高度height: 200,// 设置边距margin: EdgeInsets.all(15),// 设置装饰, 背景深橙色decoration: BoxDecoration(color: Colors.white),// 设置子组件 PageView 的裁剪组件child:PhysicalModel(color: Colors.transparent,// 设置圆角半径 15borderRadius: BorderRadius.circular(50),// 设置裁剪行为 , 抗锯齿clipBehavior: Clip.antiAlias,// 设置 PageView 组件child:PageView(// 设置 PageView 中封装的若干组件children: <Widget>[// 第一个页面组件Container(// 设置居中方式 , 居中显示alignment:Alignment.center,// 设置装饰器 , 绿色背景decoration: BoxDecoration(color: Colors.green),// 显示的主要文字child: Text("页面 0", style: TextStyle(fontSize: 20, color: Colors.black),),),// 第二个页面组件Container(// 设置居中方式 , 居中显示alignment:Alignment.center,// 设置装饰器 , 绿色背景decoration: BoxDecoration(color: Colors.red),// 显示的主要文字child: Text("页面 1", style: TextStyle(fontSize: 20, color: Colors.white),),),// 第三个页面组件Container(// 设置居中方式 , 居中显示alignment:Alignment.center,// 设置装饰器 , 绿色背景decoration: BoxDecoration(color: Colors.black),// 显示的主要文字child: Text("页面 2", style: TextStyle(fontSize: 20, color: Colors.yellow),),),],),),),Container(child: Column(children: <Widget>[// 水平/垂直方向平铺组件FractionallySizedBox(// 设置宽度充满父容器widthFactor: 1,// 要设置的水平 / 垂直方向的平铺操作的组件child: Container(decoration: BoxDecoration(color: Colors.black),child: Text("高度自适应, 宽度充满父容器",style: TextStyle(color: Colors.amberAccent),),),)],),),// 帧布局Stack(children: <Widget>[Image.network("https://img-blog.csdnimg.cn/20210301145757946.png",width: 100,height: 100,),// 设置组件位置在 Stack 的相对位置Positioned(right: 0, // 距离右侧 0 距离bottom: 0, // 距离底部 0 距离// 设置约束的组件位置child: Image.network("https://img-blog.csdnimg.cn/20210228180808133.png",width: 25,height: 25,),),],),],),),],),// 刷新时回调的方法// 列表发生下拉操作时, 回调该方法// 该回调是 Future 类型的onRefresh: _refreshIndicatorOnRefresh,):Container( // 对应底部导航栏设置选项卡// 设置容器的装饰器 , BoxDecoration 是最常用的装饰器// 可以自行查看 BoxDecoration 中可以设置的属性decoration: BoxDecoration(color: Colors.white),// 设置 child 子组件居中方式, 居中放置alignment: Alignment.center,// 子组件, 子组件设置为一个 Column 组件child: Column(// Column 子组件, 这里设置 Text 文本组件children: <Widget>[Text("设置页面选项卡")],),) , // 该设置与 _currentSelectedIndex == 0? 相对应, ?: 三目运算符),);}/// RefreshIndicator 发生下拉操作时, 回调该方法/// 该方啊是一个异步方法 , 在方法体前添加 async 关键字Future<Null> _refreshIndicatorOnRefresh() async{// 暂停 500 ms , 使用 await 关键字实现// 在这 500 ms 之间 , 列表处于刷新状态// 500 ms 之后 , 列表变为非刷新状态await Future.delayed(Duration(milliseconds: 500));return null;}}

运行效果展示 :

在这里插入图片描述





五、 相关资源



参考资料 :

  • Flutter 官网 : https://flutter.dev/
  • Flutter 开发文档 : https://flutter.cn/docs ( 强烈推荐 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社区 : https://flutter.cn/
  • Flutter 实用教程 : https://flutter.cn/docs/cookbook
  • Flutter CodeLab : https://codelabs.flutter-io.cn/
  • Dart 中文文档 : https://dart.cn/
  • Dart 开发者官网 : https://api.dart.dev/
  • Flutter 中文网 ( 非官方 , 翻译的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
  • Flutter 相关问题 : https://flutterchina.club/faq/ ( 入门阶段推荐看一遍 )

博客源码下载 :

  • GitHub 地址 : https://github.com/han1202012/flutter_cmd ( 随博客进度一直更新 , 有可能没有本博客的源码 )

  • 博客源码快照 : https://download.csdn.net/download/han1202012/15484718 ( 本篇博客的源码快照 , 可以找到本博客的源码 )


http://chatgpt.dhexx.cn/article/Ijcl3606.shtml

相关文章

Flutter实战Stack与Positioned使用详解

目录 Stack Positioned 小案例 Stack Stack({Key key,this.alignment AlignmentDirectional.topStart,//未指定区域的排布方式this.textDirection,this.fit StackFit.loose,//没有定位的子组件如何去适应Stack的大小this.overflow Overflow.clip,this.clipBehavior Clip…

Flutter基础学习 13-19 Stack的Positioned属性

前边已经介绍了Stack组件&#xff0c;并且进行了两个组件的层叠布局&#xff0c;但是如果是超过两个组件的层叠该如何进行定位那?这就是我们加今天要学的主角Positioned组件了&#xff0c;这个组件也叫做层叠定位组件。 知识点&#xff1a; Positioned组件的属性&#xff1a…

《Flutter 控件大全》第六十八个:Positioned

如果你对Flutter还有疑问或者技术方面的疑惑,欢迎加入Flutter交流群(微信:laomengit)。同时也欢迎关注我的Flutter公众号【老孟程序员】,公众号首发Flutter的相关内容。Flutter地址:http://laomengit.com 里面包含160多个组件的详细用法。Positioned用于定位Stack子组件,…

Flutter- Positioned

Positioned widget 用于定位 Stack 的子 widget。 Flutter Stack Positioned 仅用作 Stack 的直接(或后代)子部件。在 Positioned 到 Stack 的路径上&#xff0c;它只包含 StatelessWidget 或 StatefulWidget 小部件&#xff0c;不允许使用其他小部件(例如 RenderObjectWidge…

jclasslib插件使用

1、安装 File->setting->plugins->Brower Repositories 安装好后如下图 2、查看二进制码即指令

JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用

文章目录 前言一、JVM基础1、cross platform 跨平台2、cross language 跨语言3、什么是JVM呢&#xff1f;一张图告诉你4、java从编码到执行*****5. 从跨平台的语言到跨语言的平台6. jvm与class文件格式7. JVM8. javac的过程9. 常见的JVM实现10. JDK JRE JVM 二、Class File For…

如何在IDEA中使用 Jclasslib

在插件中安装jclasslib&#xff0c;然后重启IDEA 选中你的java文件&#xff0c;然后View->Show ByteCode With Jclasslib即可

android studio 字节码查看工具jclasslib bytecode viewer

jclasslib bytecode viewer 是一款非常好用的.class文件查看工具&#xff1b; jclasslib bytecode editor is a tool that visualizes all aspects of compiled Java class files and the contained bytecode. Many aspects of class files can be edited in the UI. In addit…

Jclasslib 试用

简述&#xff1a; jclasslib 是一个查看class文件的工具 TestJclasslib.java package com.anialy.test;public class TestJclasslib {private String str "Im TestJclasslib";private void print(){System.out.println(str);}public static void main(String[] arg…

jclasslib修改class文件

今天看到别人写的用工具jclasslib直接修改别人jar包里面的class文件,我自己也学着写了一下&#xff0c;发现果然很强大&#xff0c;但是也遇到一些坑 public class JVMTest {public static void main(String[] args) {long maxMemory Runtime.getRuntime().maxMemory(); //返…

通过jclasslib修改class文件

问题描述&#xff1a;在开发中遇到使用第三方jar时想要修改里边某个class文件的情况 解决方法&#xff1a; 通过jclasslib直接修改class文件 安装jclasslib 可以通过下载jclasslib软件来安装&#xff08;不推荐&#xff09; 在IEDA插件中搜索安装jclasslib Bytecode Viewer&a…

IDEA利用jclasslib 修改class文件

IDEA利用jclasslib 修改class文件 idea安装jclasslib-bytecode-viewer插件准备好class文件使用jclasslib使用下列代码更改内容。其他 idea安装jclasslib-bytecode-viewer插件 file–>settings–>plugis &#xff0c;搜索安装jclasslib-bytecode-viewer&#xff0c;重启i…

jclasslib

JClassLib不但是一个字节码阅读器而且还包含一个类库允许开发者读取,修改,写入Java Class文件与字节码。 https://github.com/ingokegel/jclasslib jclasslib bytecode viewer Purpose jclasslib bytecode viewer is a tool that visualizes all aspects of compiled Java cl…

利用jclasslib工具直接修改第三方jar包里面的class文件(亲测可用)

如果出于某些原因&#xff0c;需要修改第三方jar包里的class文件&#xff0c;我们能有什么办法呢&#xff1f; 直接修改肯定是运行不了的&#xff0c;这里我给大家介绍一个小工具jclasslib,因为我的电脑是64位的&#xff0c;所以这里就安装64位版 下载地址&#xff1a;https:…

jclasslib的使用

作用&#xff1a; JClassLib不但是一个字节码阅读器而且还包含一个类库允许开发者读取,修改,写入Java Class文件与字节码 jclasslib下载&#xff1a;https://bintray.com/ingokegel/generic/jclasslib/view 我们在这里使用jclasslib查看局部变量表&#xff08;保存java中方法…

【jvm系列-02】jvm的类加载子系统以及jclasslib的基本使用

JVM系列整体栏目 内容链接地址【一】初识虚拟机与java虚拟机https://blog.csdn.net/zhenghuishengq/article/details/129544460【二】jvm的类加载子系统以及jclasslib的基本使用https://blog.csdn.net/zhenghuishengq/article/details/129610963【三】运行时私有区域之虚拟机栈…

如何利用JClassLib修改.class文件

最近在学习逆向分析和反编译&#xff0c;无意之中了解到了JClassLib。JClassLib不但是一个字节码阅读器而且还包含一个类库允许开发者读取,修改,写入Java Class文件与字节码。其他的用途我就不说了&#xff0c;先看一下效果。 第一步、准备下载工具&#xff0c;一个是jd-gui&am…

直接修改别人jar包里面的class文件 工具:jclasslib

出于某些原因 需要把别人jar包里面的class修改一下信息 配置文件*.properties MANIFEST.MF 这些东西可以直接用记事本打开修改 然后替换掉就OK.. 在网上游荡了半天&#xff0c;没有找到合适的方法 开始我是先用jd-gui反编译 把我需要修改的那个A.class文件反编译出来把代码保…

Idea中jclasslib的安装与使用

我们学习JVM的时候常常需要查看字节码指令&#xff0c;而idea中就可以下载jclasslib插件&#xff0c;进行字节码指令的查看。下面我来带大家jclasslib的安装。 安装 安装之后重启即可 使用 使用的时候只需要点开view选中下图的选项即可&#xff0c;但是要注意是编译后再使用…

jclasslib安装

学习一个jvm的知识的时候总要去研究一些字节码指令&#xff0c; 但是每一次都把class文件打开到jclasslib里面很是麻烦&#xff0c;后来google发现有人已经写好了这个插件 1、 按住 ALTCTRLS 打开setting 2、 选择 plugins 3、选择 Browse Repositories ,搜索 jclasslib 由于…