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

【Flutter学习】基本组件之容器组件Container

前言  

 Flutter控件本身通常由许多小型、单用途的控件组成,结合起来产生强大的效果,例如,Container是一种常用的控件,由负责布局、绘画、定位和大小调整的几个控件组成,具体来说,Container是由LimitedBox、ConstrainedBox、 Align、Padding、DecoratedBox和Transform控件组成,而不是将Container子类化来产生自定义效果,您可以用这种新颖的方式组合这些以及其他简单的控件。

二,基本组件 -- Container

  容器,一个常用的控件,由基本的绘制位置大小控件组成。负责创建矩形的可视元素,可以用BoxDecoration来设计样式,比如背景边框阴影,Container也有边距填充大小限制,另外,还可以在三维空间利用矩阵进行变换。

没有子控件的容器尽可能大,除非传入的大小约束是无限的,在这种情况下,它们尽可能小。有子控件的容器将自己的尺寸给他们的孩子。我们可以通过widthheightconstraints属性控制size。

  • 继承关系
    Object > Diagnosticable > DiagnosticableTree > Widget > StatelessWidget > Container
  • 行为

    由于Container结合了许多其他Widget,每个Widget都有自己的布局行为,因此Container的布局行为有点复杂。

    依次是:

    1.采用alignment
    2.以child调整自身大小
    3.采用了width,height和constraints
    4.扩大以适应父Widget
    5.要尽可能小

    具体情况来说:

    1· 如果Container没有子Widget,没有height,没有width,没有constraints,并且父窗口提供无限制约束,则Container尝试尽可能小。
    2· 如果Container没有子Widget,没有alignment,而是一个height,width或 constraints提供,Container试图给出这些限制和父Widget的约束相结合,以尽可能小。
    3· 如果Container没有子Widget,没有height,没有width,没有constraints,没有alignment,但是父窗口提供了有界约束,那么Container会扩展以适应父窗口提供 的约束。
    4· 如果Container具有alignment,并且父窗口提供无限制约束,则constraints会尝试围绕子Widget的alignment自身大小。
    5· 如果Container具有alignment,并且父窗口提供有界约束,则constraints会尝试展开以适合父窗口,然后根据alignment将子项置于其自身内。
    6· Container具有子Widget,但没有height,没有width,没有constraints,没有alignment,将父级constraints传递给子级,自身调整大小。
  • 构造方法讲解

    Container({
        Key key,
        this.alignment,//控制child的对齐方式
        this.padding, //设置内边距
        Color color, //设置背景颜色
        Decoration decoration,//绘制在child下层的装饰,不能与color同时使用
        this.foregroundDecoration,//绘制在child上层的装饰
        double width, //宽
        double height, //高
        BoxConstraints constraints,添加到child上额外的约束条件
        this.margin,//外边距
        this.transform,//设置container的变换矩阵,类型为Matrix4
        this.child, //子组件
      }) : assert(margin == null || margin.isNonNegative),
           assert(padding == null || padding.isNonNegative),
           assert(decoration == null || decoration.debugAssertIsValid()),
           assert(constraints == null || constraints.debugAssertIsValid()),
           assert(color == null || decoration == null,
             'Cannot provide both a color and a decoration\n'
             'The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".')
    /**
        const BoxDecoration({
        this.color,
        this.image,
        this.border,
        this.borderRadius,
        this.boxShadow,//border四周添加阴影效果
        this.gradient,//装饰器的过度效果,比如可以用来给组件添加一个蒙层
        this.backgroundBlendMode,
        this.shape = BoxShape.rectangle,
        })
     */
  • 参数详解
    • alignment(这个属性是针对容器中的child的对其方式)

      • topCenter:顶部居中对齐
      • topLeft:顶部左对齐
      • topRight:顶部右对齐
      • center:水平垂直居中对齐
      • centerLeft:垂直居中水平居左对齐
      • centerRight:垂直居中水平居右对齐
      • bottomCenter底部居中对齐
      • bottomLeft:底部居左对齐
      • bottomRight:底部居右对齐  
    • decoration容器的修饰器,用于背景或者border)

      如果在decoration中用了color,就把容器中设置的color去掉,不要重复设置color,设置的边框样式是让四条边框的宽度为8px,颜色为黑色

    • margin(margin属性是表示Container与外部其他组件的距离)
    • padding(指Container边缘与Child之间的距离
      padding: EdgeInsets.all(20.0),
    • transform(可以根据自己的需要调整旋转的角度
      transform: Matrix4.rotationZ(0.2)

三,示例demo  

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Container组件demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new CenterDemoPage() ,
    );
  }
}

class CenterDemoPage extends StatefulWidget{
   @override
   createState() => new CenterDemoPageState();
}

class CenterDemoPageState extends State<CenterDemoPage>{
   @override
   Widget build(BuildContext context) {
    return new Scaffold(
       appBar: new AppBar(
         title: new Text('Container Widget Demo'),
       ),
       body:_buildDemo() ,
    );
  }

  Widget _buildDemo(){
    return new Center(
      child: Container(
        child: new Text('Hello Wolrd !'),
        alignment: Alignment.center, //设置对齐方式
        width: 300, //
        height:300, //// color: Colors.redAccent,
        //设置边框
        decoration: new BoxDecoration(
          gradient: new LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment(0.8, 0),
            colors: [const Color(0xFFFFFFEE), const Color(0xFF999999)]
          ),
          color: Colors.blue,
          border: Border.all(
            color: Colors.black,
            width: 8.0,
          ),
        ),
        //内边距
        padding: EdgeInsets.all(20.0),
        margin: EdgeInsets.all(10.0),
        //旋转
        transform: Matrix4.rotationZ(0.4),
      ),
    );
  }
}

效果:

四,官网用法

Container Widget

 

转载于:https://www.cnblogs.com/lxlx1798/p/11058794.html

相关文章:

  • Spring Boot - Web综合开发(转)
  • 总结一些常用功能源码
  • 16.04 下 ufw 防火墙的的开启、禁用、开放端口、关闭端口
  • 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)...
  • POJ 1159 Palindrome (滚动数组 DP)
  • TCP服务端
  • IBatis.Net学习笔记九--动态选择Dao的设计分析
  • 强化学习基础:蒙特卡罗和时序差分
  • golang 浮点数 取精度的效率对比
  • OpenCV入门指南 人脸检测 haar分类器
  • MySQL主从延时这么长,要怎么优化?
  • 使用 NPOI 导出数据示例
  • WPF Browser 中如何获取当前路径(临时文件中)?
  • 10+优秀“分步引导”jQuery插件(转)
  • 用processing画李萨如曲线
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 2017年终总结、随想
  • es的写入过程
  • Facebook AccountKit 接入的坑点
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • Java多线程(4):使用线程池执行定时任务
  • js学习笔记
  • Map集合、散列表、红黑树介绍
  • Next.js之基础概念(二)
  • Python进阶细节
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • Vultr 教程目录
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 看域名解析域名安全对SEO的影响
  • 写给高年级小学生看的《Bash 指南》
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • 阿里云移动端播放器高级功能介绍
  • 湖北分布式智能数据采集方法有哪些?
  • ​Java并发新构件之Exchanger
  • ​比特币大跌的 2 个原因
  • ​水经微图Web1.5.0版即将上线
  • #13 yum、编译安装与sed命令的使用
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • #我与Java虚拟机的故事#连载15:完整阅读的第一本技术书籍
  • (7)STL算法之交换赋值
  • (LeetCode C++)盛最多水的容器
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (多级缓存)多级缓存
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (九)c52学习之旅-定时器
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (转)Linq学习笔记
  • .NET Core跨平台微服务学习资源
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题