当前位置: 首页 > news >正文

Flutter常用组件实践

Flutter常用组件实践

  • 1、MaterialApp 和 Center(组件居中)
  • 2、Scaffold
  • 3、Container(容器)
  • 4、BoxDecoration(装饰器)
  • 5、Column(纵向布局)及Icon(图标)
  • 6、Column/Row(横向/横向布局)+CloseButton/BackButton/IconButton(简单按钮)
  • 7、Expanded和Flexible
  • 8、Stack和Positioned(层叠布局)
  • 9、页面底部切换BottomNavigationBar
  • 10、RefreshIndicator和ListView(下拉刷新)
  • 11、FloatingActionButton(悬浮按钮)
  • 12、Text(文本)
  • 13、TextField(功能较多)
  • 14、PageView(滑动视图)
  • 15、Image(加载图片)
  • 16、Chip(有趣的小组件)和 Divider(分隔符)
  • 17、Card(卡片式布局)
  • 18、AlertDialog(弹出框)
  • 19、LinearGradient(颜色渐变)
  • 20、RichText(富文本)
  • 21、GestureDetector(手势监控)
  • 22、Opacity(透明度)
  • 23、MediaQuery.removePadding(去除组件之间空格)
  • 24、Slider(滑动进度条)
  • 25、ReorderableListView(拖拽排序组件)

1、MaterialApp 和 Center(组件居中)

class MyApp extends StatelessWidget {
   
  const MyApp({
   super.key});

  // This widget is the root of your application.
  
  Widget build(BuildContext context) {
   
    return MaterialApp(
      title: '我的应用',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: Center(
        child: Text("测试文本")
      ),
    );
  }
}

2、Scaffold

Scaffold 实现了基本的 MaterialApp 布局。只要是在 Material 中定义了的单个界面显示的布局控件元素,都可以使用 Scaffold 来绘制。
在这里插入图片描述

class MyApp extends StatelessWidget {
   
  const MyApp({
   super.key});

  // This widget is the root of your application.
  
  Widget build(BuildContext context) {
   
    return MaterialApp(
        title: '我的应用',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
        ),
        home: Scaffold(
          appBar: AppBar(
            title: Text('我是页面标题'),
            centerTitle: true,
            elevation: 0,
            bottom: null,
            backgroundColor: Colors.orangeAccent,
            bottomOpacity: 0,
          ),
          //头部导航条区域
          body: Center(
            child: Text('我是主体内容'),
          ),
          //页面主题内容区域
          floatingActionButton: FloatingActionButton(
            onPressed: () {
   },
            child: Icon(Icons.add),
          ),
          //右下角浮动按钮区域
          drawer: Drawer(),
          //侧边栏抽屉区域
          bottomNavigationBar: BottomNavigationBar(
            items: [
              BottomNavigationBarItem(
                icon: Icon(
                  Icons.home,
                  color: Colors.grey,
                ),
                activeIcon: Icon(
                  Icons.home,
                  color: Colors.blue,
                ),
                label: '首页',
              ),
              BottomNavigationBarItem(
                icon: Icon(
                  Icons.list,
                  color: Colors.grey,
                ),
                activeIcon: Icon(
                  Icons.list,
                  color: Colors.blue,
                ),
                label: "列表",
              )
            ],
          ),
        ) //底部tabBar区域,
    );
  }
}

3、Container(容器)

将Container容器放在上面的Scaffold对象的body部分,则到如下
在这里插入图片描述

Container(
    width: 200, //宽度
    height: 200, //长度
    child: Text("我是body部分的内容"), //子组件
    decoration: BoxDecoration(
      color: Colors.blue,
    ), //装饰器
    padding: EdgeInsets.all(10),
    //内容距离盒子边界的距离
    margin: EdgeInsets.all(10) //盒子边界之外的距离
)

4、BoxDecoration(装饰器)

在这里插入图片描述

Center(
  child: Container(
    width: 270,
    height: 470,
    decoration: BoxDecoration(
        color: Colors.blue, //颜色背景
        image: DecorationImage(
          image: NetworkImage("https://inews.gtimg.com/om_bt/O_DyA7LF3uL3wZ9zYVo8ZhI_IMUOn_NJ_Pgj2IhAuRApoAA/641"), //背景图片
          fit: BoxFit.cover, //图片充满组件
        ),
        border: Border.all(
          color: Colors.red,
          width: 5.0,
        )
    ), //设置边框
  ),
)

5、Column(纵向布局)及Icon(图标)

Column(
    children: <Widget>[
      Expanded(child: Text('主体内容1'), flex: 3,),
      Expanded(child: Text('主体内容2'), flex

相关文章:

  • python+requests接口自动化测试框架实例教程
  • C#容器源码分析 --- Queue<T>
  • 2025届蓝桥杯JavaB组个人题解(暂时不全,没题目)
  • 【AI】AI大模型发展史:从理论探索到技术爆发
  • [创业之路-366]:投资尽职调查 - 尽调核心逻辑与核心影响因素:价值、估值、退出、风险、策略
  • webpack vite
  • 基于 Termux 在移动端配置 Ubuntu 系统并搭建工作环境
  • DeepSeek在应急救援领域的应用解决方案
  • docker测试镜像源
  • 如何在运行时获取硬件信息
  • day24 学习笔记
  • Linux:35.其他IPC和IPC原理+信号量入门
  • 自动驾驶的数据集以及yolov8和yolop
  • Oracle 复制表结构(含索引、主键)操作指南
  • 池式结构---内存池
  • 企业年报问答RAG挑战赛冠军方案:从零到SotA,一战封神
  • AI 大语言模型 (LLM) 平台的整体概览与未来发展
  • #关于数据库中的时间存储
  • 006.Gitlab CICD流水线触发
  • Python实现链接KS3,并批量下载KS3文件数据到本地
  • 歪歪小站 wordpress/最好最全的搜索引擎
  • 网站商城开发公司/淘宝宝贝关键词排名查询工具
  • 网站备案信息保护/网页搭建
  • 怎么查工程中标单位/流程优化的七个步骤
  • 做logo有哪些网站/重庆网络seo
  • nodejs可以做网站吗/今日新闻最新消息50字