Flutter笔记:发布一个Flutter头像模块 easy_avatar

Flutter笔记
发布一个头像Flutter模块 easy_avatar

作者李俊才 (jcLee95):https://blog.csdn.net/qq_28550263
邮箱 :291148484@163.com
本文地址:https://blog.csdn.net/qq_28550263/article/details/133912019
模块地址:https://pub.dev/packages/easy_avatar
(国内访问:https://pub-web.flutter-io.cn/packages/easy_avatar)



1. 概述

在这里插入图片描述

当你看到这篇文章的时候,先不要误以为笔者不知道 Flutter 中提供了一个 近乎与没有提供没太大区别的 CircleAvatar组件。对,它真的仅仅就是圆形头像——除非,你使用 Clip 类组件当然剪裁成一个你想要的样子。不过它的功能还是太弱了。其实,最初我想做的仅仅是淘宝消息的好友头像,这非常简单:

在这里插入图片描述

不过考虑到实际需求,和更多的项目场景,比如——图片缺失的处理方案、图片出错的错误方案、图片背景的处理方案,以及一些特殊效果。例如,有一张原始的头像是这样的:

在这里插入图片描述
配合下面的背景图:

https://gitee.com/jacklee1995/example-pictures/raw/master/scenery/jonathanvasquez8950_scenery_2f6031d1-c4fe-41d7-8abf-d1c9c40d9981.png
经过简单配置,就可以得到下面爬出相框的效果:

在这里插入图片描述
另外,一些图片、背景的调整都是自动的,你要配置的可能真的很少。比如,当指定头像图片时,你不需要考虑时网络图片还是本地图片——通常在Flutter中,需要使用 Image 的两个不同构造函数,即 networkasset 来加载。但是这都处理好了,如果时网络地址,则直接传入 图片的 url 字符串;如果是 assets 下的静态资源图片,也可以直接传入资源位置字符串,并且都是通过 image 属性指定。

而且,你不需要考虑,网络地址或者本地资源是否有效,如果图像资源失效,将会更换为我画好的默认头像,看起来就像这样:
在这里插入图片描述

2. 安装

你可以使用 pub 包管理工具在你的项目中运行如下命令添加到你的 Flutter 项目中:

flutter pub add easy_avatar

这将在你的项目的 pubspec.yaml 中添加一行(并运行一个隐式的dart pub get):

dependencies:easy_avatar: ^1.0.0

版本号可能随时间变化。

3. 快速入门

The simplest example is to use the default avatar and default parameters:
最简单的例子是使用默认头像及默认参数:

const Avatar()

The effect of this code is as follows:
其运行效果如下:

在这里插入图片描述

你也可以指定一些头像参数,但是如果你指定一个错误的头像地址,将自动地使用默认头像,例如:

Avatar(width: 100,height: 100,backgroundColor: Colors.indigo,borderRadius: 40,padding: EdgeInsets.all(20),image: 'https://example.com/avatar.jpg',
),

The effect of this code is as follows:
其运行效果如下:

在这里插入图片描述

如果要可以设置一个真实的网络图片URL作为头像,则它必须以http开头,例如:

const Avatar(width: 200,height: 200,borderRadius: 100,image:'https://profile-avatar.csdnimg.cn/bb869bb0b79b48209c6206043890c985_qq_28550263.jpg',
),

The effect of this code is as follows:
其运行效果如下:

在这里插入图片描述

Oh, this is me. An active user of the open source community!
哦,这就是我了。一个开源社区的活跃用户!

Actually, you can also use animation, which doesn’t matter:
其实你还可以使用动图,这没关系的:

const Avatar(width: 200,height: 200,margin: EdgeInsets.all(6),borderRadius: 60,image:'https://github.githubassets.com/images/mona-loading-dimmed.gif',
),

The effect of this code is as follows:
其运行效果如下:

在这里插入图片描述

If the avatar itself has a transparent background, you can see the default background color or background picture. Also, you can add an outer border for your avatar, such as:
如果头像本身是透明背景的,可以看到默认背景色 或 背景图片。并且,你还可以为你的头像添加外边框,例如:

Avatar(width: 200,height: 200,padding: const EdgeInsets.all(10),margin: const EdgeInsets.all(10),borderRadius: 80,backgroundColor: Colors.white, // 如不设置,则为灰色// 可以为你的头像添加边框border: Border.all(color: const Color.fromARGB(255, 0, 0, 0),width: 6.0,style: BorderStyle.solid,strokeAlign: BorderSide.strokeAlignInside,),image:'https://gitee.com/jacklee1995/example-pictures/raw/master/asian/asian-girl-avatar.png',
),

The effect of this code is as follows:
其运行效果如下:

在这里插入图片描述

Next is an example of using a network picture as a background picture, which will cover the background color effect:
接下来是一个使用网络图片作为背景图的例子,这个网络图片将覆盖背景颜色效果:

const Avatar(width: 200,height: 200,margin: EdgeInsets.all(10),borderRadius: 80,backgroundImage:'https://gitee.com/jacklee1995/example-pictures/raw/master/scenery/jonathanvasquez8950_scenery_2f6031d1-c4fe-41d7-8abf-d1c9c40d9981.png',image:'https://gitee.com/jacklee1995/example-pictures/raw/master/asian/asian-girl-avatar.png',
),

The effect of this code is as follows:
其运行效果如下:

在这里插入图片描述

Now, let’s explore some special effects - Avatar out of the box.

The Avatar class provides an intermediate layer border property called interlayerBorder. Using this property instead of the border property will help you achieve the effect of characters coming out of the frame. Here’s an example code:
下面我们玩一点特效——人物出框。
Avatar 类提供了一个中间层边框属性 interlayerBorder,通过该属性而不是 border 属性设置的边框将帮助你完成人物出框的效果。示例代码如下:

Avatar(width: 200,height: 200,interlayerBorder: Border.all(color: const Color.fromARGB(255, 255, 251, 3),width: 20.0,style: BorderStyle.solid,strokeAlign: BorderSide.strokeAlignInside,),// margin: const EdgeInsets.all(6),borderRadius: 100,backgroundImage:'https://gitee.com/jacklee1995/example-pictures/raw/master/scenery/jonathanvasquez8950_scenery_2f6031d1-c4fe-41d7-8abf-d1c9c40d9981.png',image: 'assets/asian-boy-avatar.png', // 也可以使用本地图片资源
)

其效运行果如下:

在这里插入图片描述

尽情探索吧!~

3. API 编程接口

class Avatar

A widget that displays an avatar with various customization options.
此类是一个显示带有各种自定义选项的头像的组件。

This widget allows you to display avatars with custom dimensions, border radius, background color, and an optional background image. You can specify both network and local images for the avatar.

该组件允许您显示具有自定义尺寸、边框半径、背景颜色和可选背景图像的头像。可以为头像指定网络和本地图像。

Example Usage 示例

Avatar(width: 150,height: 150,borderRadius: 75,backgroundImage: 'https://example.com/avatar-background.jpg',backgroundColor: Colors.blue,image: 'assets/avatar.jpg',margin: EdgeInsets.all(10),padding: EdgeInsets.all(5),
)

Constructor 构造函数

默认构造函数 Avatar

Creates an Avatar widget.

创建Avatar组件。

  • width (double, optional): The width of the avatar. Default is 100.

    • 头像的宽度。默认为100。
  • height (double, optional): The height of the avatar. Default is 100.

    • 头像的高度。默认为100。
  • borderRadius (double, optional): The border radius of the avatar. Default is 50.

    • 头像的边框半径。默认为50。
  • backgroundImage (String, optional): The URL for the background image of the avatar.

    • 头像的背景图像的URL。
  • backgroundColor (Color, optional): The background color of the avatar if backgroundImage is not provided. Default is a gray color.

    • 如果没有提供backgroundImage,则设置头像的背景颜色。默认为灰色。
  • image (String, required): The image for the avatar, which can be either a network URL or a local asset path.

    • 用于设置头像的图像,可以是网络URL或本地资源路径。
  • margin (EdgeInsetsGeometry, optional): The margin around the entire avatar. Default is no margin.

    • 控制整个头像周围的边距。默认没有边距。
  • padding (EdgeInsetsGeometry, optional): The padding within the avatar, affecting only the avatar image. Default is no padding.

    • 应用于头像内部,仅影响头像图像的填充。默认没有填充。
  • border (Border, optional): The border for the avatar.

    • 头像的边框。
  • interlayerBorder (Border, optional): The interlayer border for the avatar.

    • 头像的中间层边框。

Issue Tracker

你可以在 Github 上报告错误:https://github.com/jacklee1995/flutter_easy_avatar/issues

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/110002.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[Leetcode学习-C语言]Two Sum

题目连接:LeetCode - The Worlds Leading Online Programming Learning Platform leetcode写C真的很多坑...... 用 hash 拉链法 构造hash函数,才能写这么题小题。 typedef struct Node { // 得自己做个hash 拉链法struct Node *next;int val;int sign…

利用PHP快速抓取音频数据的方法与技巧

目录 使用cURL库抓取音频数据 优点 潜在问题及解决方案 使用file_get_contents函数抓取音频数据 优点 潜在问题及解决方案 总结 随着互联网的发展,音频内容在网络上的应用越来越广泛,如音乐播放、语音通信等。有时,我们需要从特定的音…

ModStartCMS v7.4.0 公共图片库支持,安全功能升级

ModStart 是一个基于 Laravel 模块化极速开发框架。模块市场拥有丰富的功能应用,支持后台一键快速安装,让开发者能快的实现业务功能开发。 系统完全开源,基于 Apache 2.0 开源协议,免费且不限制商业使用。 功能特性 丰富的模块市…

7-1 求解马鞍点问题

若矩阵Anm中某个元素A[i][j]是矩阵第i行中值最小的元素,同时又是第j列中值最大的元素,则称元素A[i][j]是矩阵中的一个马鞍点。 设以二维数组存储矩阵,编写算法求矩阵A中的所有马鞍点,算法的时间复杂度要尽量的低。 注意当最大值…

【Linux-常用命令-基础命令-删除文件夹以及内容-rm--r-命令-笔记】

【Linux-常用命令-基础命令-删除文件夹以及内容-rm--r-命令-笔记】 1、前言2、操作3、自己的操作 1、前言 最近,在使用Linux的时,使用相关基础命令是,总是容易忘记,上网一搜,大部分都写的比较繁琐,关于删除…

Python学习笔记——存储容器

食用说明:本笔记适用于有一定编程基础的伙伴们。希望有助于各位! 列表 列表类似数组,其中可以包含不同类型的元素,写法如下: list1 [Google, Runoob, 1997, 2000] list2 [1, 2, 3, 4, 5 ] list3 ["a", …

Qt工具开发,该不该跳槽?

Qt工具开发,该不该跳槽? 就这样吧,我怕你跳不动。 嵌入式UI,目前趋势是向着LVGL发展。QT已经在淘汰期了。很多项目还在用,但技术上已经落后。QT短期内不会全面淘汰,但退位让贤的大趋势已经很清楚了。 最近很多小伙伴…

liunx下软链接和硬链接的用法

什么是软链接(类似快捷方式): 符号连接(Symbolic Link),也叫软连接。 软链接文件有类似于 Windows 的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件&#xff0c…

嵌入式开发学习之STM32F407串口(USART)收发数据(三)

嵌入式开发学习之STM32F407串口(USART)收发数据(三) 开发涉及工具一、选定所使用的串口二、配置串口1.配置串口的I/O2.配置串口参数属性3.配置串口中断4.串口中断在哪里处理5.串口如何发送字符串 三、封装串口配置库文件1.创建头文…

【OpenVINO】OpenVINO C# API 常用 API 详解与演示

OpenVINO C# API 常用 API 详解与演示 1 安装OpenVINO C# API2 导入程序集 3 初始化OpenVINO 运行时内核4 加载并获取模型信息4.1 加载模型4.2 获取模型信息 5 编译模型并创建推理请求6 张量Tensor6.1 张量的获取与设置6.2 张量的信息获取与设置 7 加载推理数据7.1 获取输入张量…

循环日程安排问题(分治法)

函数接口定义&#xff1a; void Plan(int a[][N],int k); 裁判测试程序样例&#xff1a; #include #include <math.h> #include using namespace std; #define N 100 void Plan(int a[][N],int k); int main() { int i,j,a[N][N],k,size; cin>>k; sizepow(2,k)…

金山终端安全系统V9.0 SQL注入漏洞复现

0x01 产品简介 金山终端安全系统是一款为企业提供终端防护的安全产品&#xff0c;针对恶意软件、病毒和外部攻击提供防范措施&#xff0c;帮助维护企业数据和网络。 0x02 漏洞概述 金山终端安全系统V9.0 /inter/update_software_info_v2.php页面存在sql注入漏洞&#xff0c;该…

VsCode通过Git History插件查看某个页面的版本修改记录

首先需要安装插件Git History 方式一&#xff1a;通过 点击File History 查看某个文件变更&#xff1b;即通过commit的提交记录去查看某个文件的修改 方式二&#xff1a;通过点击选择toggle File Blame 查看当前页面每一行所有提交修改记录

通讯协议学习之路:USART协议理论

通讯协议之路主要分为两部分&#xff0c;第一部分从理论上面讲解各类协议的通讯原理以及通讯格式&#xff0c;第二部分从具体运用上讲解各类通讯协议的具体应用方法。 后续文章会同时发表在个人博客(jason1016.club)、CSDN&#xff1b;视频会发布在bilibili(UID:399951374) 一、…

手机抬手亮屏解锁,用到了哪些硬件?

随着时代发展&#xff0c;智能手机以丰富的功能及便利性&#xff0c;成为了人们必不可少的物品&#xff0c;其中人脸解锁功能是非常有用的功能&#xff0c;广受年轻人的喜爱&#xff0c;那么你知道她是如何实现吗&#xff1f;今天凡小亿带你们探索&#xff01; 手机抬手亮屏解锁…

不懂就要问:线上的流量卡和营业厅的流量卡是一样的吗?

营业厅里有卖流量卡吗&#xff1f;当然有&#xff0c;但是营业厅里的流量卡和线上申请的流量卡完全不一样。 ​ 针对这两种流量卡&#xff0c;小编和大家一一介绍一下&#xff1a; 一、营业厅里的流量卡。 营业厅里办理的流量卡&#xff0c;一般都是官方运营商推出的流量卡&…

CSS动画实现节流

目录 介绍: 实现代码: 介绍: 节流指的避免过于频繁的执行一个函数&#xff0c;例如&#xff1a;一个保存按钮&#xff0c;为了避免重复提交或者服务器考虑&#xff0c;往往需要对点击行为做一定的限制&#xff0c;不然会频繁的请求接口&#xff0c;之前基本上是通过js去控制节…

SQL Select(选择) 语法

SQL SELECT 语法 SELECT 语法用于从数据库中选择数据。 返回的数据存储在结果表中&#xff0c;称为结果集。 基本语法&#xff1a;SELECT和FROM 在任何SQL查询语句中都&#xff1a;SELECT和FROM他们必须按顺序排列。SELECT指示要查看哪些列&#xff0c;FROM标识它们所在的表。…

mysql—表单二

一、查询环境 1.创建student和score表 CREATE TABLE student ( id INT(10) NOT NULL UNIQUE PRIMARY KEY , name VARCHAR(20) NOT NULL , sex VARCHAR(4) , birth YEAR, department VARCHAR(20) , address VARCHAR(50) ); 创建score表。SQL代码如下&#xff1a; CR…

MySQL启动错误总结

centos7中出现mysql启动失败排查方法&#xff1a;首先找到/var/log/mysqd.log 第一种启动失败&#xff1a; 查看包含最后几行包含error的行&#xff1b; [ERROR] Unix socket lock file is empty /tmp/mysql.sock.lock.[ERROR] Unable to setup unix socket lock file.[ERROR] …