日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Flutter】Flutter 布局组件 ( 布局组件简介 | Row 组件 | Column 组件 | SizedBox 组件 | ClipOval 组件 )

發(fā)布時間:2025/6/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flutter】Flutter 布局组件 ( 布局组件简介 | Row 组件 | Column 组件 | SizedBox 组件 | ClipOval 组件 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 一、Flutter 布局相關(guān)的組件簡介
  • 二、Row 和 Column 組件
  • 三、SizedBox 組件
  • 四、ClipOval 組件
  • 五、 完整代碼示例
  • 六、 相關(guān)資源





一、Flutter 布局相關(guān)的組件簡介



Flutter 與布局相關(guān)的組件 :

  • Container : 容器組件 ;
  • RenderObjectWidget : 布局渲染相關(guān)組件 ;
    • SingleChildRenderObjectWidget : 單節(jié)點布局組件 ;
      • Opacity : 常用于修改組件透明度 ;
      • ClipOval : 裁剪布局組件 , 可以將布局裁剪成圓形 ;
      • ClipRRect : 裁剪布局組件 , 可以將布局裁剪成方形 ;
      • PhysicalModel : 將布局顯示成不同的形狀 ;
      • Align : 布局設(shè)置組件 , 一般設(shè)置布局居中操作 ;
      • Padding : 設(shè)置內(nèi)邊距的組件 ;
      • SizeBox : 用于約束布局大小的組件 ;
      • FractionallySizedBox : 約束布局水平 / 垂直方向的平鋪操作 ;
    • MultiChildRenderObjectWidget : 多節(jié)點布局組件 ;
      • Stack : 相當(dāng)于幀布局 FrameLayout ;
      • Flex :
        • Column : 相當(dāng)于線性布局 , 垂直方向布局 , 組件從上到下擺放 ;
        • Row : 相當(dāng)于線性布局 , 水平方向布局 , 組件從左到右 ;
      • Wrap : 該組件與 Row 組件類似 , Wrap 組件可以換行 ;
      • Flow : 不常用 ;
  • ParentDataWidget :
    • Positioned : 用于固定組件位置的組件 ;
    • Flexible : 用于約束組件在父容器中展開大小的組件 ;




二、Row 和 Column 組件



Row 組件相關(guān)參數(shù) : Row 組件相當(dāng)于線性布局 , 水平方向布局 , 組件從左到右 ;

class Row extends Flex {Row({Key key,MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,MainAxisSize mainAxisSize = MainAxisSize.max,CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,TextDirection textDirection,VerticalDirection verticalDirection = VerticalDirection.down,TextBaseline textBaseline,List<Widget> children = const <Widget>[],}) : super(children: children,key: key,direction: Axis.horizontal,mainAxisAlignment: mainAxisAlignment,mainAxisSize: mainAxisSize,crossAxisAlignment: crossAxisAlignment,textDirection: textDirection,verticalDirection: verticalDirection,textBaseline: textBaseline,); }

Column 組件相關(guān)參數(shù) : Column 組件相當(dāng)于線性布局 , 垂直方向布局 , 組件從上到下擺放 ;

class Column extends Flex {Column({Key key,MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,MainAxisSize mainAxisSize = MainAxisSize.max,CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,TextDirection textDirection,VerticalDirection verticalDirection = VerticalDirection.down,TextBaseline textBaseline,List<Widget> children = const <Widget>[],}) : super(children: children,key: key,direction: Axis.vertical,mainAxisAlignment: mainAxisAlignment,mainAxisSize: mainAxisSize,crossAxisAlignment: crossAxisAlignment,textDirection: textDirection,verticalDirection: verticalDirection,textBaseline: textBaseline,); }

Row 和 Column 組件使用時 , 設(shè)置其對應(yīng)的 children: [] 即可 , 在中括號 [] 中是多個組件的集合 , 使用逗號隔開 ;

示例代碼 :

// 水平方向排列的線性布局 Row(children: <Widget>[組件1,組件2,組件3,] )// 垂直方向排列的線性布局 Column(children: <Widget>[組件1,組件2,組件3,] )



三、SizedBox 組件



SizeBox : 用于約束布局大小的組件 ;

class SizedBox extends SingleChildRenderObjectWidget {const SizedBox({ Key key, this.width, this.height, Widget child }): super(key: key, child: child); }

SizeBox 組件使用方法 : 在 width 和 height 字段設(shè)置組件的寬高屬性 , 在 child 字段設(shè)置要設(shè)置大小的組件 ;

// 使用 SizedBox 組件約束布局大小 SizedBox(width: 寬度像素值,height: 高速像素值,// 使用 SizedBox 約束組件大小child: 要約束的組件, ),

代碼示例 :

// 使用 SizedBox 組件約束布局大小 SizedBox(width: 100,height: 100,// 使用 SizedBox 約束該 Image 組件大小child: Image.network("https://img-blog.csdnimg.cn/20210301145757946.png"), ),



四、ClipOval 組件



ClipOval 組件 : 裁剪布局組件 , 可以將布局裁剪成圓形 ;

class ClipOval extends SingleChildRenderObjectWidget {const ClipOval({Key key, this.clipper, this.clipBehavior = Clip.antiAlias, Widget child}): assert(clipBehavior != null),super(key: key, child: child); }

ClipOval 組件使用方法 : 將要裁剪的組件設(shè)置到該 ClipOval 對應(yīng)的 child 字段中 , 即可將該組件裁剪 ;

代碼示例 : 此處 ClipOval 組件對 SizedBox 組件進(jìn)行圓形裁剪 , SizedBox 組件約束 Image 組件的大小 ;

// 圓形裁剪組件 , 將 child 布局裁剪成圓形 ClipOval(// 使用 SizedBox 組件約束布局大小child: SizedBox(width: 100,height: 100,// 使用 SizedBox 約束該 Image 組件大小child: Image.network("https://img-blog.csdnimg.cn/20210301145757946.png"),), ),



五、 完整代碼示例



完整代碼示例 :

import 'package:flutter/material.dart';class LayoutPage extends StatefulWidget {@override_LayoutPageState createState() => _LayoutPageState(); }class _LayoutPageState extends State<LayoutPage> {/// 當(dāng)前被選中的底部導(dǎo)航欄索引int _currentSelectedIndex = 0;// This widget is the root of your application.@overrideWidget build(BuildContext context) {// 文本組件樣式 , 可以設(shè)置給 Text 文本組件// 設(shè)置字體大小 20, 顏色紅色TextStyle textStyle = TextStyle(fontSize: 20, color: Colors.red);return MaterialApp(title: '布局組件示例',theme: ThemeData(primarySwatch: Colors.blue,),home: Scaffold(// 頂部標(biāo)題欄appBar: AppBar(title: Text('布局組件示例'),),// 底部導(dǎo)航欄 BottomNavigationBar 設(shè)置// items 可以設(shè)置多個 BottomNavigationBarItembottomNavigationBar: BottomNavigationBar(// 設(shè)置當(dāng)前選中的底部導(dǎo)航索引currentIndex: _currentSelectedIndex,// 設(shè)置點擊底部導(dǎo)航欄的回調(diào)事件 , index 參數(shù)是點擊的索引值onTap: (index){// 回調(diào) StatefulWidget 組件的 setState 設(shè)置狀態(tài)的方法 , 修改當(dāng)前選中索引// 之后 BottomNavigationBar 組件會自動更新當(dāng)前選中的選項卡setState(() {// 改變 int _currentSelectedIndex 變量的狀態(tài)_currentSelectedIndex = index;});},// 條目items: [// 設(shè)置底部導(dǎo)航欄條目, 每個條目可以設(shè)置一個圖標(biāo)BottomNavigationBarItem(// 默認(rèn)狀態(tài)下的圖標(biāo)icon: Icon(Icons.home, color: Colors.grey,),// 激活狀態(tài)下的圖標(biāo)activeIcon: Icon(Icons.home, color: Colors.red,),// 設(shè)置標(biāo)題title: Text("主頁")),// 設(shè)置底部導(dǎo)航欄條目, 每個條目可以設(shè)置一個圖標(biāo)BottomNavigationBarItem(// 默認(rèn)狀態(tài)下的圖標(biāo)icon: Icon(Icons.settings, color: Colors.grey,),// 激活狀態(tài)下的圖標(biāo)activeIcon: Icon(Icons.settings, color: Colors.red,),// 設(shè)置標(biāo)題title: Text("設(shè)置"))],),// 設(shè)置懸浮按鈕floatingActionButton: FloatingActionButton(onPressed: (){print("懸浮按鈕點擊");},child: Text("懸浮按鈕組件"),),// Container 容器使用body:_currentSelectedIndex == 0 ?// 刷新指示器組件RefreshIndicator(// 顯示的內(nèi)容child: ListView(children: <Widget>[Container( // 對應(yīng)底部導(dǎo)航欄設(shè)置選項卡// 設(shè)置容器的裝飾器 , BoxDecoration 是最常用的裝飾器// 可以自行查看 BoxDecoration 中可以設(shè)置的屬性decoration: BoxDecoration(color: Colors.white),// 設(shè)置 child 子組件居中方式, 居中放置alignment: Alignment.center,// 子組件, 子組件設(shè)置為一個 Column 組件child: Column(// Column 子組件, 這里設(shè)置 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"),),),],),],),),],),// 刷新時回調(diào)的方法// 列表發(fā)生下拉操作時, 回調(diào)該方法// 該回調(diào)是 Future 類型的onRefresh: _refreshIndicatorOnRefresh,):Container( // 對應(yīng)底部導(dǎo)航欄設(shè)置選項卡// 設(shè)置容器的裝飾器 , BoxDecoration 是最常用的裝飾器// 可以自行查看 BoxDecoration 中可以設(shè)置的屬性decoration: BoxDecoration(color: Colors.white),// 設(shè)置 child 子組件居中方式, 居中放置alignment: Alignment.center,// 子組件, 子組件設(shè)置為一個 Column 組件child: Column(// Column 子組件, 這里設(shè)置 Text 文本組件children: <Widget>[Text("設(shè)置頁面選項卡")],),) , // 該設(shè)置與 _currentSelectedIndex == 0? 相對應(yīng), ?: 三目運算符),);}/// RefreshIndicator 發(fā)生下拉操作時, 回調(diào)該方法/// 該方啊是一個異步方法 , 在方法體前添加 async 關(guān)鍵字Future<Null> _refreshIndicatorOnRefresh() async{// 暫停 500 ms , 使用 await 關(guān)鍵字實現(xiàn)// 在這 500 ms 之間 , 列表處于刷新狀態(tài)// 500 ms 之后 , 列表變?yōu)榉撬⑿聽顟B(tài)await Future.delayed(Duration(milliseconds: 500));return null;}}

運行效果展示 : 第二行的整體布局放在 Row 組件中 , 橫向布局中放置了兩個組件 , 第一個 Image 組件顯示原始圖片 , 第二個組件是經(jīng)過 SizedBox 組件約束大小 , 和 ClipOval 組件裁剪成圓形后的效果 ;





六、 相關(guān)資源



參考資料 :

  • Flutter 官網(wǎng) : https://flutter.dev/
  • Flutter 開發(fā)文檔 : https://flutter.cn/docs ( 強(qiáng)烈推薦 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社區(qū) : https://flutter.cn/
  • Flutter 實用教程 : https://flutter.cn/docs/cookbook
  • Flutter CodeLab : https://codelabs.flutter-io.cn/
  • Dart 中文文檔 : https://dart.cn/
  • Dart 開發(fā)者官網(wǎng) : https://api.dart.dev/
  • Flutter 中文網(wǎng) ( 非官方 , 翻譯的很好 ) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
  • Flutter 相關(guān)問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )

博客源碼下載 :

  • GitHub 地址 : https://github.com/han1202012/flutter_cmd ( 隨博客進(jìn)度一直更新 , 有可能沒有本博客的源碼 )

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

總結(jié)

以上是生活随笔為你收集整理的【Flutter】Flutter 布局组件 ( 布局组件简介 | Row 组件 | Column 组件 | SizedBox 组件 | ClipOval 组件 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。