Flutter加载过渡动画按钮插件loading_transition_button的使用
Flutter加载过渡动画按钮插件loading_transition_button的使用
Loading Transition button
Getting Started(开始使用)
要使用此插件,在您的 pubspec.yaml
文件中添加以下依赖:
dependencies:
...
loading_transition_button: ^0.0.1
然后运行 flutter pub get
来获取该依赖。
How to use(如何使用)
在项目中导入该包:
import 'package:loading_transition_button/loading_transition_button.dart';
为了使用此小部件,您需要一个 LoadingButtonController
来处理不同的状态:
final _controller = LoadingButtonController();
当按钮被点击时,它会启动加载动画。您可以通过控制器来触发错误动画或停止加载动画。
示例代码如下:
LoadingButton(
color: Colors.blue, // 按钮的颜色
onSubmit: () => print('onSubmit'), // 提交事件
controller: _controller, // 控制器
errorColor: Colors.red, // 错误颜色
transitionDuration: Duration(seconds: 1), // 动画持续时间
child: Text(
'Hit me!', // 按钮文字
style: Theme.of(context)
.textTheme
.bodyText1!
.copyWith(color: Colors.white),
),
),
如果需要导航到其他页面,可以调用 moveToScreen
方法:
_controller.moveToScreen(
context: context, // 当前上下文
page: SearchPage(), // 目标页面
stopAnimation: true, // 是否停止当前动画
navigationCallback: (route) => Navigator.of(context).push(route), // 导航回调
),
navigationCallback
将为您提供一个新的路由,您可以根据需要使用 push
或 replace
等方法进行导航。
完整示例代码
以下是一个完整的示例代码,展示了如何使用 loading_transition_button
插件:
import 'package:flutter/material.dart';
import 'package:loading_transition_button/loading_transition_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
final _controller = LoadingButtonController();
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(
builder: (BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(
width: double.infinity,
color: Colors.black26,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 44,
width: 300,
child: LoadingButton(
color: Colors.blue, // 按钮背景颜色
onSubmit: () => print('onSubmit'), // 提交事件
controller: _controller, // 控制器
errorColor: Colors.red, // 错误状态颜色
transitionDuration: Duration(seconds: 1), // 动画持续时间
child: Text(
'Hit me!', // 按钮文字
style: Theme.of(context)
.textTheme
.bodyText1!
.copyWith(color: Colors.white),
),
),
),
SizedBox(height: 100),
Row(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: () => _controller.startLoadingAnimation(), // 开始加载动画
child: Text('Start'), // 按钮文字
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: () => _controller.stopLoadingAnimation(), // 停止加载动画
child: Text('Stop'), // 按钮文字
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: () => _controller.onError(), // 触发错误动画
child: Text('Error'), // 按钮文字
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: () => _controller.moveToScreen( // 跳转到下一个页面
context: context,
page: SearchPage(),
stopAnimation: true,
navigationCallback: (route) =>
Navigator.of(context).push(route),
),
child: Text('Next screen'), // 按钮文字
)
],
)
],
),
),
);
},
),
);
}
}
class SearchPage extends StatelessWidget {
[@override](/user/override)
Widget build(context) {
return Scaffold(
backgroundColor: Colors.pinkAccent, // 页面背景颜色
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.pink, // 导航栏背景颜色
title: TextField(
decoration: InputDecoration.collapsed(hintText: "Search"), // 输入框提示文字
),
),
);
}
}
更多关于Flutter加载过渡动画按钮插件loading_transition_button的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter加载过渡动画按钮插件loading_transition_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
loading_transition_button
是一个 Flutter 插件,用于在用户点击按钮时显示加载动画,并在加载完成后切换到下一个状态。这通常用于表单提交、网络请求等场景,以提供更好的用户体验。
安装插件
首先,你需要在 pubspec.yaml
文件中添加 loading_transition_button
插件的依赖:
dependencies:
flutter:
sdk: flutter
loading_transition_button: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
使用示例
以下是一个简单的示例,展示了如何使用 loading_transition_button
:
import 'package:flutter/material.dart';
import 'package:loading_transition_button/loading_transition_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Loading Transition Button Example')),
body: Center(
child: LoadingTransitionButtonExample(),
),
),
);
}
}
class LoadingTransitionButtonExample extends StatefulWidget {
@override
_LoadingTransitionButtonExampleState createState() =>
_LoadingTransitionButtonExampleState();
}
class _LoadingTransitionButtonExampleState
extends State<LoadingTransitionButtonExample> {
ButtonState _buttonState = ButtonState.idle;
@override
Widget build(BuildContext context) {
return LoadingTransitionButton(
state: _buttonState,
onPressed: () async {
setState(() {
_buttonState = ButtonState.loading;
});
// 模拟网络请求
await Future.delayed(Duration(seconds: 2));
setState(() {
_buttonState = ButtonState.success;
});
// 你可以在这里添加更多的逻辑,例如导航到下一个页面
},
child: Text('Submit'),
loadingWidget: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
successWidget: Icon(Icons.check, color: Colors.white),
);
}
}
参数说明
state
: 按钮的当前状态,可以是ButtonState.idle
、ButtonState.loading
或ButtonState.success
。onPressed
: 当按钮被点击时触发的回调函数。通常在这里执行异步操作。child
: 按钮的内容,通常是一个Text
或Icon
。loadingWidget
: 在加载状态下显示的 widget,通常是一个CircularProgressIndicator
。successWidget
: 在成功状态下显示的 widget,通常是一个Icon
。
自定义样式
你可以通过传递 style
参数来自定义按钮的样式。例如:
LoadingTransitionButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
padding: MaterialStateProperty.all(EdgeInsets.all(16.0)),
),
// 其他参数...
)