Flutter BoxDecoration 实现圆形、圆角、下划线、阴影、渐变色背景


1 BoxDecoration 介绍

BoxDecoration为提供各种装饰Widget的方法,如下图所示:

在这里插入图片描述

本文使用到的BoxDecoration的属性介绍

  • color 设置背景颜色
  • shape 设置形状
  • border 设置边框
  • borderRadius 设置边框的圆角半径
  • boxShadow 设置阴影
  • gradient 设置渐变色背景

2 下划线 or 分隔线

    Container(
      decoration: const BoxDecoration(
        border: Border(
          // 划线位置、线宽、颜色
          bottom: BorderSide(width: 2.0, color: Colors.blue),
        ),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: const [Icon(Icons.text_fields), Text('我是下划线')],
      ),
    )

在这里插入图片描述


    Container(
      width: double.infinity,
      height: 40,
      alignment: Alignment.centerLeft,
      decoration: const BoxDecoration(
        border: Border(
          // 划线位置、线宽、颜色
          bottom: BorderSide(width: 1.0, color: Colors.blue),
        ),
      ),
      child: const Text('我是一个Item,我自带分隔线'),
    )

在这里插入图片描述


2 圆角背景 or 圆角线框

    Container(
      height: 36,
      width: 150,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        //背景颜色
        color: Colors.blue,
        //圆角半径
        borderRadius: BorderRadius.all(Radius.circular(18.0)),
      ),
      child: const Text('我是圆角背景', style: TextStyle(color: Colors.white)),
    )

在这里插入图片描述


    Container(
      height: 36,
      width: 150,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        //圆角半径
        borderRadius: const BorderRadius.all(Radius.circular(18.0)),
        //边框线宽、颜色
        border: Border.all(width: 1.0, color: Colors.blue),
      ),
      child: const Text('我是圆角线框', style: TextStyle(color: Colors.blue)),
    )

在这里插入图片描述


3 圆形背景 or 圆形线框

   Container(
      width: 100,
      height: 100,
      alignment: Alignment.center,
      decoration:
          const BoxDecoration(shape: BoxShape.circle, color: Colors.blue),
      child: const Text('我是圆形背景', style: TextStyle(color: Colors.white)),
    )

在这里插入图片描述


    Container(
      width: 100,
      height: 100,
      alignment: Alignment.center,
      decoration: BoxDecoration(
          shape: BoxShape.circle, border: Border.all(color: Colors.blue)),
      child: const Text('我是圆形线框', style: TextStyle(color: Colors.blue)),
    )

在这里插入图片描述


4 添加阴影

    Container(
      width: 100,
      height: 100,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        color: Colors.white,
        shape: BoxShape.circle,
        // 阴影的颜色,模糊半径
        boxShadow: [BoxShadow(color: Colors.grey, blurRadius: 6)],
      ),
      child: const Text('我是阴影', style: TextStyle(color: Colors.blue)),
    )

在这里插入图片描述


5 渐变背景

5.1 线性渐变

    Container(
      width: 100,
      height: 100,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        shape: BoxShape.circle,
        // 线性渐变,分别设置渐变的颜色,起始点
        gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: [Colors.red, Colors.blue]),
      ),
      child: const Text('线性渐变', style: TextStyle(color: Colors.white)),
    )

在这里插入图片描述


5.2 弧形渐变

    // import 'dart:math' as math;
    Container(
      height: 100,
      width: 100,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        shape: BoxShape.circle,
        gradient: SweepGradient(
          center: FractionalOffset.center,
          startAngle: 0.0,
          endAngle: math.pi * 2,
          colors: <Color>[
            Colors.red,
            Colors.yellow,
            Colors.blue,
            Colors.green,
            Colors.red,
          ],
          stops: <double>[0, 0.25, 0.5, 0.75, 1],
        ),
      ),
      child: const Text('弧形渐变', style: TextStyle(color: Colors.white)),
    )

在这里插入图片描述


5.3 扩散性渐变

    Container(
      width: 100,
      height: 100,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        shape: BoxShape.circle,
        // 扩散性渐变,通过调整 radius 、stops 来查看不同的效果
        gradient: RadialGradient(
          radius: 0.6,
          colors: <Color>[Colors.red, Colors.blue],
          stops: <double>[0.2, 0.9],
        ),
      ),
      child: const Text('扩散性渐变', style: TextStyle(color: Colors.white)),
    )

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ww897532167/article/details/111933624
今日推荐