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)),
    )

在这里插入图片描述

  • 11
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现一个立体的彩色渐变按钮,可以按照以下步骤进行: 1. 创建一个自定义的按钮控件,可以继承自FlatButton或RaisedButton等现有的Flutter控件。 2. 在控件的build方法中,使用Container包裹一个InkWell组件来实现按钮的点击效果。 3. 创建一个Gradient对象,用于定义按钮的颜色渐变效果。可以使用LinearGradient、RadialGradient或SweepGradient等不同的渐变方式。 4. 在Container的decoration属性中,设置一个BoxDecoration对象,使用Gradient作为其color属性,实现彩色渐变的效果。 5. 为了实现立体的效果,可以在BoxDecoration中设置boxShadow属性,定义按钮的阴影效果。 6. 最后,根据需要添加文字或图标等内容到按钮中。 以下是一个简单的代码示例: ``` import 'package:flutter/material.dart'; class GradientButton extends StatelessWidget { final String text; final VoidCallback onPressed; GradientButton({@required this.text, @required this.onPressed}); @override Widget build(BuildContext context) { return Container( height: 50.0, decoration: BoxDecoration( gradient: LinearGradient( colors: [Colors.blue, Colors.purple], begin: Alignment.topLeft, end: Alignment.bottomRight, ), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.5), spreadRadius: 2, blurRadius: 5, offset: Offset(0, 3), ), ], borderRadius: BorderRadius.circular(30.0), ), child: Material( color: Colors.transparent, child: InkWell( onTap: onPressed, child: Center( child: Text( text, style: TextStyle( color: Colors.white, fontSize: 18.0, fontWeight: FontWeight.bold, ), ), ), ), ), ); } } ``` 使用时,可以像使用普通的FlatButton或RaisedButton一样调用GradientButton控件,并传入需要的参数。例如: ``` GradientButton( text: '立体彩色渐变按钮', onPressed: () { print('点击了按钮!'); }, ), ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值