flutter-渐变色边框和渐变色文字和渐变色背景

server/2025/2/28 20:39:38/

文章目录

  • 1. 介绍
  • 2. 代码实现
    • 2-1. 渐变色背景
    • 2-2. 渐变色边框
    • 2-3. 宽高由内容撑起的渐变色边框
    • 2-4. 渐变色文本
  • 3. 完整例子

1. 介绍

flutter 中,渐变有三种,线性渐变 LinearGradient、放射状渐变 RadialGradient、扇形渐变 SweepGradient。一般都是使用线性渐变 LinearGradient,下面是本文带你实现的几个渐变色效果:

2. 代码实现

LinearGradient 类接受三个参数:

  • begin:渐变的起始点,例如 Alignment.centerLeft 表示从左侧开始渐变
  • end:渐变的结束点,例如 Alignment.centerRight 表示渐变到右侧结束
  • colors:渐变过程中的颜色数组

2-1. 渐变色背景

Container(height: 200,alignment: Alignment.center,margin: const EdgeInsets.symmetric(vertical: 30),decoration: BoxDecoration(borderRadius: BorderRadius.circular(12),gradient: const LinearGradient(begin: Alignment.topLeft,end: Alignment.bottomRight,colors: colorList)),child: const Text('渐变色背景',style:TextStyle(color: Colors.white, fontSize: 16)))

渐变色背景

2-2. 渐变色边框

需要先安装gradient_borders库。

flutter pub add gradient_borders
import 'package:gradient_borders/box_borders/gradient_box_border.dart';Container(height: 200,alignment: Alignment.center,margin: const EdgeInsets.only(bottom: 30),decoration: BoxDecoration(borderRadius: BorderRadius.circular(12),border: const GradientBoxBorder(gradient: LinearGradient(colors: colorList),width: 2)),child: const Text('渐变色边框',style:TextStyle(color: Colors.white, fontSize: 16)))

渐变色边框

2-3. 宽高由内容撑起的渐变色边框

Container(padding: const EdgeInsets.all(10),margin: const EdgeInsets.only(bottom: 30),decoration: BoxDecoration(borderRadius: BorderRadius.circular(12),border: const GradientBoxBorder(gradient: LinearGradient(colors: colorList),width: 2)),child: const Text('宽高不固定由内容撑起来的渐变色边框',style:TextStyle(color: Colors.white, fontSize: 16)))

宽高由内容撑起的渐变色边框

2-4. 渐变色文本

Container(height: 50,alignment: Alignment.center,child: ShaderMask(shaderCallback: (rect) {return const LinearGradient(begin: Alignment.topLeft,end: Alignment.bottomRight,colors: colorList,).createShader(rect);},child: const Text('渐变色文字',style: TextStyle(color: Colors.white, fontSize: 16))))

渐变色文字

3. 完整例子

import 'package:flutter/material.dart';
import 'package:gradient_borders/box_borders/gradient_box_border.dart';/// 渐变色背景和边框和文字
class GradientColorPage extends StatelessWidget {const GradientColorPage({super.key});Widget build(BuildContext context) {const List<Color> colorList = [Colors.red, Colors.yellow, Colors.blue];return Scaffold(backgroundColor: Colors.black,body: SafeArea(child: Container(alignment: Alignment.center,width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.height,padding: const EdgeInsets.all(20),child: Column(mainAxisSize: MainAxisSize.max,children: [/// 渐变色背景Container(height: 200,alignment: Alignment.center,margin: const EdgeInsets.symmetric(vertical: 30),decoration: BoxDecoration(borderRadius: BorderRadius.circular(12),gradient: const LinearGradient(begin: Alignment.topLeft,end: Alignment.bottomRight,colors: colorList)),child: const Text('渐变色背景',style:TextStyle(color: Colors.white, fontSize: 16))),/// 渐变色边框Container(height: 200,alignment: Alignment.center,margin: const EdgeInsets.only(bottom: 30),decoration: BoxDecoration(borderRadius: BorderRadius.circular(12),border: const GradientBoxBorder(gradient: LinearGradient(colors: colorList),width: 2)),child: const Text('渐变色边框',style:TextStyle(color: Colors.white, fontSize: 16))),/// 宽高不固定由内容撑起来的渐变色边框Container(padding: const EdgeInsets.all(10),margin: const EdgeInsets.only(bottom: 30),decoration: BoxDecoration(borderRadius: BorderRadius.circular(12),border: const GradientBoxBorder(gradient: LinearGradient(colors: colorList),width: 2)),child: const Text('宽高不固定由内容撑起来的渐变色边框',style:TextStyle(color: Colors.white, fontSize: 16))),/// 渐变色文字Container(height: 50,alignment: Alignment.center,child: ShaderMask(shaderCallback: (rect) {return const LinearGradient(begin: Alignment.topLeft,end: Alignment.bottomRight,colors: colorList,).createShader(rect);},child: const Text('渐变色文字',style: TextStyle(color: Colors.white, fontSize: 16))))],))));}
}

往期文章

  • flutter学习-day1-环境搭建和启动第一个项目
  • flutter学习-day12-可滚动组件和监听
  • Vue2全家桶+Element搭建的PC端在线音乐网站
  • vue3+element-plus配置cdn
  • 助你上手Vue3全家桶之Vue3教程
  • 助你上手Vue3全家桶之VueX4教程
  • 助你上手Vue3全家桶之Vue-Router4教程
  • 超详细!Vue的九种通信方式
  • 超详细!Vuex手把手教程
  • 使用nvm管理node.js版本以及更换npm淘宝镜像源
  • vue中利用.env文件存储全局环境变量,以及配置vue启动和打包命令
  • 超详细!Vue-Router手把手教程

个人主页

  • CSDN
  • GitHub
  • 简书
  • 博客园
  • 掘金

http://www.ppmy.cn/server/171378.html

相关文章

OpenCV(11):人脸检测、物体识别

1 人脸检测 人脸检测是计算机视觉中的一个经典问题&#xff0c;而 OpenCV 提供了基于 Haar 特征分类器的人脸检测方法&#xff0c;简单易用且效果显著。本文将详细介绍如何使用 OpenCV 中的 cv2.CascadeClassifier() 进行人脸检测。 1.1 Haar 特征分类器 Haar 特征分类器是一种…

docker简介-学习与参考

docker Docker 是一个开源的应用容器引擎&#xff0c;基于 Go 语言并遵从 Apache2.0 协议开源。 Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中&#xff0c;然后发布到任何流行的 Linux 机器上&#xff0c;也可以实现虚拟化。 容器是完全使用沙箱…

DavGo简单部署WebDAV服务

目录 功能特性使用方法1. 下载2. 配置 config.yaml3. 运行服务器4. 可以用来挂载WebDav的软件 反向代理 DavGo 是一个用 Go 语言实现的轻量级 WebDAV 服务器&#xff0c;支持动态配置多个 WebDAV 服务实例&#xff0c;每个实例可以独立设置根目录、认证信息和读写模式。 功能特…

git使用之git stash

使用场景&#xff1a; 1.其他分支提交代码到主分支&#xff0c;导致项目报错&#xff0c;想拉取远程代码但是本地的代码暂时不想提交&#xff1b; 2.切换分支处理其他任务 概括&#xff1a;不想提交本地代码时使用 使用&#xff1a; 1.查看stash记录 git stash list 2.暂…

25护理综合研究生复试面试问题汇总 护理综合专业知识问题很全! 护理综合面试流程攻略 护理综合考研复试调剂真题汇总

25护理综合考研复试&#xff0c;专业面试该如何应对&#xff1f;学姐来支招&#xff01; 亲爱的宝子们&#xff0c;一提到护理综合考研复试&#xff0c;你是不是瞬间感觉自己要崩溃&#xff0c;担心老师问出一堆超级难的问题&#xff1f;放心&#xff0c;复试其实没那么可怕&a…

SpringBoot + Vue 项目创建详细步骤

目录 一、本地开发环境 二、后端创建 三、前端创建 一、本地开发环境 后端开发软件&#xff1a;IntelliJ IDEA 2022.1.3后端框架&#xff1a;SpringBoot前端框架&#xff1a;vue/cli 5.0.8 Element UI后端语言&#xff1a;Javajdk版本&#xff1a;1.8.0_371数据库&#xf…

深度优先搜索(DFS)在 Spark 中的应用与实现

深度优先搜索&#xff08;DFS&#xff09;在 Spark 中的应用与实现 深度优先搜索&#xff08;Depth-First Search, DFS&#xff09;是一种经典的图遍历算法&#xff0c;广泛应用于图论、路径搜索、连通性检测等场景。在 Spark 中&#xff0c;DFS 可以用于处理图数据&#xff0…

Spring Boot 集成 Kafka

在现代软件开发中&#xff0c;分布式系统和微服务架构越来越受到关注。为了实现系统之间的异步通信和解耦&#xff0c;消息队列成为了一种重要的技术手段。Kafka 作为一种高性能、分布式的消息队列系统&#xff0c;被广泛应用于各种场景。而 Spring Boot 作为一种流行的 Java 开…