Flutter之Decoration

简介: Flutter之Decoration

20200814165409295.png

20200814165409358.png20200908195733472.png


2、Decoration介绍

Flutter的Decoration可以设置:背景色 背景图 边框 圆角 阴影 渐变色 的等属性,有点像android里面的shape,Decoration 是基类,它的子类有下面这些


BoxDecoration:

实现边框、圆角、阴影、形状、渐变、背景图像


ShapeDecoration:

实现四边分别指定颜色和宽度、底部线、矩形边色、圆形边色、体育场(竖向椭圆)、 角形(八边角)边色


FlutterLogoDecoration:

Flutter图片


UnderlineTabindicator:

下划线


这里先介绍常用的 BoxDecoration,构造方法

const BoxDecoration({
  this.color,//背景色
  this.image,//图片
  this.border,//描边
  this.borderRadius,//圆角大小
  this.boxShadow,//阴影
  this.gradient,//渐变色
  this.backgroundBlendMode,//图像混合模式
  this.shape = BoxShape.rectangle,//形状,BoxShape.circle和borderRadius不能同时使用
})

boxShadow 阴影

color - 阴影颜色

offset - 阴影相偏移量

blurRadius - 高斯模糊数值

spreadRadius - 阴影膨胀量,这个值我是真不知有啥用,没场景啊,一般不写这个值


gradient渐变

支持2种渐变:LinearGradient线性渐变 和 RadialGradient扫描渐变

LinearGradient :

begin - 渐变开始的位置

end - 渐变结束的位置

colors - 渐变颜色,是数组

stops - 值列表,装有0.0到1.0的数值

tileMode - 平铺模式


shape形状

rectangle是矩形,BoxShape.circle是圆形,BoxShape.circle和borderRadius不能一起使用


3、代码测试

效果1测试代码

  @override
  Widget build(BuildContext context) {
      return MaterialApp(
          title: 'open url',
          home: Scaffold(
            appBar: AppBar(
              // Here we take the value from the MyHomePage object that was created by
              // the App.build method, and use it to set our appbar title.
              title: Text('hello flutter'),
            ),
            body: Center(
              child: DecoratedBox(
//              padding: EdgeInsets.all(16),
//            padding: EdgeInsets.fromLTRB(10, 20, 30, 40),
//            padding: EdgeInsets.only(left: 10, right: 30),
                decoration: BoxDecoration(
                  // 背景色
                    color: Colors.lightBlueAccent,
                    // 边框,
                    border: Border.all(color: Colors.yellowAccent, style: BorderStyle.solid, width: 5),
                    // 背景图
                    image: new DecorationImage(
                        image: new NetworkImage(
                            'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1484037605,2864708693&fm=11&gp=0.jpg'),
                        fit: BoxFit.cover
                    ),
                    borderRadius: BorderRadius.all(Radius.circular(30)),
                    boxShadow:[
                      BoxShadow(
                        color: Colors.redAccent,
                        offset: Offset(20, 20),
                        blurRadius: 10,
                      ),
                    ]
                ),
                child: Container(
                  height: 200,
                  width: 200,
                ),
              ),
            ),
          ),
      );
  }
}


效果2测试代码

  @override
  Widget build(BuildContext context) {
      return MaterialApp(
          title: 'open url',
          home: Scaffold(
            appBar: AppBar(
              // Here we take the value from the MyHomePage object that was created by
              // the App.build method, and use it to set our appbar title.
              title: Text('hello flutter'),
            ),
            body: Center(
              child: DecoratedBox(
//              padding: EdgeInsets.all(16),
//            padding: EdgeInsets.fromLTRB(10, 20, 30, 40),
//            padding: EdgeInsets.only(left: 10, right: 30),
                decoration: BoxDecoration(
                  // 背景色
                    gradient: LinearGradient(colors:[Colors.blue, Colors.green]), //背景渐变
                    color: Colors.lightBlueAccent,
                    // 背景图
                    borderRadius: BorderRadius.all(Radius.circular(3)),
                    boxShadow: [ //阴影
                      BoxShadow(
                          color:Colors.black54,
                          offset: Offset(2.0,2.0),
                          blurRadius: 4.0
                      )
                    ]
                ),
                child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: 80.0, vertical: 18.0),
                    child: Text("chenyu", style: TextStyle(color: Colors.white),
                  ),
                )
              ),
            ),
          ),
      );
  }
}


效果3测试代码

  @override
  Widget build(BuildContext context) {
      return MaterialApp(
          title: 'open url',
          home: Scaffold(
            appBar: AppBar(
              // Here we take the value from the MyHomePage object that was created by
              // the App.build method, and use it to set our appbar title.
              title: Text('hello flutter'),
            ),
            body: Center(
              child: DecoratedBox(
//              padding: EdgeInsets.all(16),
//            padding: EdgeInsets.fromLTRB(10, 20, 30, 40),
//            padding: EdgeInsets.only(left: 10, right: 30),
                decoration: BoxDecoration(
                  // 背景色
                    border: Border.all(
                        color: Colors.yellowAccent, style: BorderStyle.solid, width: 5),
                    // 背景图
                    image: new DecorationImage(
                        image: new NetworkImage(
                            'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1484037605,2864708693&fm=11&gp=0.jpg'),
                        fit: BoxFit.cover
                    ),
                    shape: BoxShape.circle,
                ),
                child: Container(
                  width: 200,
                  height: 200,
                ),
              ),
            ),
          ),
      );
  }
}


相关文章
|
6月前
|
Dart 开发工具 Android开发
Flutter PC 应用开发指南:从环境搭建到实战避坑
本文系统介绍如何在 Windows 平台使用 Flutter 开发 PC 应用,涵盖环境搭建、项目创建、插件兼容性、原生功能调用、签名发布、常见问题解决及性能优化等全流程,助你高效构建跨平台桌面应用,少走弯路。
2382 5
|
存储 算法 固态存储
【计算机组成原理】存储系统
【计算机组成原理】存储系统
8380 0
【计算机组成原理】存储系统
|
Dart Android开发 iOS开发
比较 Flutter 日期选择器库【Flutter 专题 6】
日期选择器是对 UI 的有用补充,它使您的应用程序用户可以轻松地从日历中选择日期。无论您是在注册表单中添加出生日期字段,还是为用户提供预约时间段,您都可以使用日期选择器库来简化流程。 在本教程中,我们将探索 Flutter 的三个流行日期选择器库 Flutter、Flutter Datetime Picker、Flutter Date Range Picker 和date_time_picker。我们将检查每个库的功能并将每个库安装在一个简单的移动应用程序中。
1576 0
比较 Flutter 日期选择器库【Flutter 专题 6】
|
Java Maven Android开发
Android 阿里云镜像整理
Android 阿里云镜像整理
8518 0
|
2月前
|
人工智能 安全 Linux
小龙虾AI🦞 OpenClaw理性使用指南(阿里云/本地部署+免费Coding Plan API成本控制+安全防护+避坑手册)
“睡一觉赚大钱”“一人公司坐拥10个AI员工”“500元上门安装”——2026年开春,OpenClaw(曾用名Clawdbot)被流量裹挟成“暴富神话”。社交平台上,代安装服务报价从几百元飙升至数千元,大厂甚至下场举办“公益装机”活动;但另一面,真实用户面对每月1.5万甚至2.6万的API账单崩溃发问:“为什么不直接雇实习生?”
673 10
|
Java Maven 开发工具
记录一次Maven无法打包的排查过程
【5月更文挑战第3天】记录一次WhatTheFuck经历
1044 2
记录一次Maven无法打包的排查过程
|
Android开发 iOS开发
Flutter中获取监听屏幕方向、锁定屏幕方向
Flutter中获取监听屏幕方向、锁定屏幕方向
832 2
|
Web App开发 自然语言处理 iOS开发
GitHub 中文化插件
这款插件专为GitHub设计,实现菜单栏、标题及按钮等元素的汉化,并提供项目描述的人机翻译,助力新手快速上手。相较于浏览器自带翻译,准确性更高。安装需先配备Tampermonkey或Violentmonkey,随后从GreasyFork安装插件,重启GitHub即现中文界面。
1831 0
|
Dart 开发者
【Flutter】Dart 面向对象 ( get 方法 | set 方法 | 静态方法
【Flutter】Dart 面向对象 ( get 方法 | set 方法 | 静态方法
1250 0
|
Java Maven Android开发
安卓项目使用阿里云镜像加速构建过程
安卓项目使用阿里云镜像加速构建过程
4439 0