Flutter系列重学Container示例详解

作者:SoaringHeart 时间:2023-07-19 00:46:10 

一、Container 简介

flutter 开发中最核心的是用最少的组件(层次)完成功能开发;Container 前端的盒子模型实现,类似 div 标签,掌握其他组件前,深入学习理解 Container 的使用是必要的。

Container是一个组合类容器,由DecoratedBoxConstrainedBox、TransformPaddingAlign等组件组合的一个多功能容器,所以我们只需通过一个Container组件可以实现同时需要装饰、变换、约束限制的场景。

二、示例

  • 示例包含透明度,背景装饰,前景装饰,child 模糊度,红色部分是 child;

Flutter系列重学Container示例详解

buildSection() {
 return Opacity(
   opacity: 1,
   child: Container(
     margin: EdgeInsets.all(20),
     padding: EdgeInsets.all(20),
     decoration: BoxDecoration(
       borderRadius: BorderRadius.all(Radius.circular(20)),
       gradient: LinearGradient(
         colors: [Colors.green, Colors.yellow],
         begin: Alignment.topCenter,
         end: Alignment.bottomCenter,
       ),
       boxShadow: [
         BoxShadow(
           color: Colors.red.withOpacity(0.5),
           spreadRadius: 5,
           blurRadius: 7,
           offset: Offset(0, 3), // changes position of shadow
         ),
       ],
       image: DecorationImage(
         image: NetworkImage('https://tenfei02.cfp.cn/creative/vcg/800/new/VCG21409037867.jpg'),
         fit: BoxFit.cover,
       ),
     ),
     foregroundDecoration: BoxDecoration(
       color: Colors.yellow,
       border: Border.all(color: Colors.green, width: 5),
       borderRadius: BorderRadius.all(Radius.circular(400)),
       image: DecorationImage(
         image: NetworkImage('https://pic.616pic.com/bg_w1180/00/04/08/G5Bftx5ZDI.jpg'),
         fit: BoxFit.cover,
       ),
     ),
     child: BackdropFilter(
       filter: ui.ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
       child: Container(
         constraints: BoxConstraints.expand(),
         decoration: BoxDecoration(
           borderRadius: BorderRadius.all(Radius.circular(20)),
           color: Colors.red,
         ),
         child: Text('VCG21409037867'),
       ),
     ),
   ),
 );
}
  • 实现背景图 background-repeat 显示,flutter支持四种类型:

enum ImageRepeat {
 repeat,
 repeatX,
 repeatY,
 noRepeat,
}

这里以第一种 ImageRepeat.repeat 为例:

Flutter系列重学Container示例详解

Container(
 decoration: BoxDecoration(
   image: DecorationImage(
     image: AssetImage('images/img_update.png'),
     repeat: ImageRepeat.repeat,
     alignment: Alignment.topLeft,
   )
 ),
 child: Container(
   constraints: BoxConstraints.expand(),
   child: OutlinedButton(
     onPressed: () { print("ImageRepeat.repeat"); },
     child: Text('ImageRepeat.repeat',
       style: TextStyle(fontWeight: FontWeight.bold, color: Colors.red),
     ),
   ),
 ),
),

transform && alignment

Flutter系列重学Container示例详解

Container(
 decoration: BoxDecoration(
   image: DecorationImage(
     image: AssetImage('images/img_update.png'),
     repeat: ImageRepeat.repeat,
     alignment: Alignment.topLeft,
   )
 ),
 transform: Matrix4.rotationZ(.2),//变化
 alignment: Alignment.centerRight, //卡片文字对齐
 child: Text(
   "5.20", style: TextStyle(color: Colors.red, fontSize: 40.0),
 ),
),

三、源码分析

class Container extends StatelessWidget {
 Container({
   Key? key,
   this.alignment,
   this.padding,
   this.color,
   this.decoration,
   this.foregroundDecoration,
   double? width,
   double? height,
   BoxConstraints? constraints,
   this.margin,
   this.transform,
   this.transformAlignment,
   this.child,
   this.clipBehavior = Clip.none,
 }):super(key: key);
 // 子项
 final Widget? child;
 // 内边距
 final EdgeInsetsGeometry? padding;
 // 背景色,和 decoration 只能二选一
 final Color? color;
 // 背景装饰
 final Decoration? decoration;
 // 前景装饰(在 child 之上)
 final Decoration? foregroundDecoration;
 // 约束条件
 final BoxConstraints? constraints;
 // 外边距
 final EdgeInsetsGeometry? margin;
 // 变化
 final Matrix4? transform;
 // 变化之后  child 对齐方式
 final AlignmentGeometry? transformAlignment;
 // 裁剪方式
 final Clip clipBehavior;
 EdgeInsetsGeometry? get _paddingIncludingDecoration {
   if (decoration == null || decoration!.padding == null)
     return padding;
   final EdgeInsetsGeometry? decorationPadding = decoration!.padding;
   if (padding == null)
     return decorationPadding;
   return padding!.add(decorationPadding!);
 }
 @override
 Widget build(BuildContext context) {
   Widget? current = child;
   if (child == null && (constraints == null || !constraints!.isTight)) {
     current = LimitedBox(
       maxWidth: 0.0,
       maxHeight: 0.0,
       child: ConstrainedBox(constraints: const BoxConstraints.expand()),
     );
   }
   if (alignment != null)
     current = Align(alignment: alignment!, child: current);
   final EdgeInsetsGeometry? effectivePadding = _paddingIncludingDecoration;
   if (effectivePadding != null)
     current = Padding(padding: effectivePadding, child: current);
   if (color != null)
     current = ColoredBox(color: color!, child: current);
   if (clipBehavior != Clip.none) {
     assert(decoration != null);
     current = ClipPath(
       clipper: _DecorationClipper(
         textDirection: Directionality.maybeOf(context),
         decoration: decoration!,
       ),
       clipBehavior: clipBehavior,
       child: current,
     );
   }
   if (decoration != null)
     current = DecoratedBox(decoration: decoration!, child: current);
   if (foregroundDecoration != null) {
     current = DecoratedBox(
       decoration: foregroundDecoration!,
       position: DecorationPosition.foreground,
       child: current,
     );
   }
   if (constraints != null)
     current = ConstrainedBox(constraints: constraints!, child: current);
   if (margin != null)
     current = Padding(padding: margin!, child: current);
   if (transform != null)
     current = Transform(transform: transform!, alignment: transformAlignment, child: current);
   return current!;
 }
 ...
}

源码很简单,组件层层包裹,但是我们必须清楚每一个参数的作用和使用场景。它可以帮你用更少的层级完成功能,这很重要。

来源:https://juejin.cn/post/7177603348193869879

标签:Flutter,Container
0
投稿

猜你喜欢

  • java emoji表情存储的解决方法

    2023-07-10 20:19:06
  • MyBatis关闭一级缓存的两种方式(分注解和xml两种方式)

    2023-09-02 10:18:59
  • Java中的内部类你了解吗

    2022-12-11 08:29:10
  • c#文件助手类分享(读取文件内容 操作日志文件)

    2023-05-25 10:32:54
  • Android异步消息机制详解

    2023-08-07 09:42:52
  • java日期操作工具类(获取指定日期、日期转换、相隔天数)

    2023-11-28 06:42:53
  • Java中Function的使用及说明

    2023-08-12 03:04:29
  • Flutter手机权限检查与申请实现方法详解

    2022-01-15 23:52:38
  • java多线程之停止线程的方法实例代码详解

    2023-03-23 04:35:21
  • 如何查找YUM安装的JAVA_HOME环境变量详解

    2023-04-01 11:48:22
  • Java的Struts框架简介与环境配置教程

    2023-10-29 05:23:03
  • Struts2中Action三种接收参数形式与简单的表单验证功能

    2022-11-26 08:55:20
  • mybatis-plus3.0.1枚举返回为null解决办法

    2023-11-07 16:59:50
  • 深入解析Java多态进阶学习

    2022-05-16 16:06:55
  • Android UI实现SlidingMenu侧滑菜单效果

    2021-12-08 16:03:34
  • Java结构性设计模式中的装饰器模式介绍使用

    2022-04-08 06:30:00
  • Android Studio使用recyclerview实现展开和折叠功能(在之前的微信页面基础之上)

    2023-10-31 23:08:55
  • 使用java实现telnet-client工具分享

    2023-10-18 10:56:59
  • 基于C#的socket编程的TCP异步的实现代码

    2023-04-13 06:42:05
  • C#中IEnumerable、ICollection、IList、List之间的区别

    2022-07-27 18:15:07
  • asp之家 软件编程 m.aspxhome.com