AnyAnimatedButton 是一个 Flutter 包,提供可自定义的按钮,支持在空闲、进度、成功和错误状态之间的平滑动画。它能直观地显示后台进程的进行情况,从而在表单提交或数据加载等操作中增强用户反馈。该按钮支持多种自定义选项,包括颜色、图标、标签和动画持续时间。专为跨平台使用设计,可在 Android、iOS、Web、macOS、Windows 和 Linux 上无缝运行。非常适合需要实时反馈的应用程序来提升用户体验。
适合场景
表单提交并加载状态
文件上传进度指示器
带有成功/错误反馈的认证流程
带有动画过渡的入门流程
后台任务状态可视化
主要特点
平滑的状态转换动画
可自定义的成功和错误状态
支持按状态更改图标和文本
跨平台兼容性
可配置的动画持续时间和缓动
可扩展的样式选项
适合对象
寻求直观用户界面反馈的开发者
频繁执行异步操作的应用程序
以用户体验为中心的移动和Web应用
快速原型化交互式按钮
需要最少样板代码的进度指示器项目
使用建议
需要理解 Flutter 的状态管理
可能需要额外的逻辑来触发状态变化
多个实例的性能影响
限于预定义的动画类型
不适合超出所提供状态的复杂自定义动画
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
any_animated_button
Very often after tapping a button we send some data or form to remote API and we need to signalize it to the user. This package makes it easy for us by creating expandable AnyAnimatedButton and AnyAnimatedButtonBloc.
Easy usage
AnyAnimatedButton depends on bloc pattern. To create custom button we need to create new bloc and
widget.
AnyAnimatedButtonBloc
We need to create class, which extends AnyAnimatedButtonBloc and override asyncAction.
AnyAnimatedButton is based on AnimatedContainer. To create our own button we need to create class, which extends CustomAnyAnimatedButton. CustomAnyAnimatedButton consists of 2 fields, which needs to be overridden:
bloc - AnyAnimatedButtonBloc? - bloc which should be connected with the button. If we won't pass it, the button won't animate
defaultParams - AnyAnimatedButtonParams - params object, which describes how button should look and behave
and 3 optional fields:
progressParams - AnyAnimatedButtonParams - params object for describing button in progress state
successParams - AnyAnimatedButtonParams - params object for describing button in success state
errorParams - AnyAnimatedButtonParams - params object for describing button in error state
AnyAnimatedButtonParams
The class that holds all the data about button look and behavior. All properties that we want to animate should be put directly inside all corresponding fields. Rest of them (like Text) should go to child field, which takes Widget.
The only required field is height, rest of them are optional.
We have got also 3 factory constructors, which describe default progress, error and success button state. We can reuse them with changed colors and size.
double.infinity - the button expands as much as he can
null - the button is as small as it can - it fits its content
fixed - we can set fixed width i.e. 200 and the button will always be this wide
Splash effect
There is one problem with InkWell splash effect. If we want the splash effect to work we need to put in child field firstly Material with transparent color → InkWell → rest of the child.
The package has built-in BlocListener, which makes it easier for you to listen to the state changes. AnyAnimatedButtonBlocListener<Input, Output, Failure> takes 3 generic types,Input is type of data that goes into the bloc, Output is type of data returned on success and Failure is the error which will be returned, when any error occurs in bloc.
updating text on button with width set to null does not work properly. The button width will adjust only to the first text width. The workaround is to set fixed width of the longer text instead of null.