Flutter加载按钮美化插件nice_loading_button的使用
Flutter加载按钮美化插件nice_loading_button的使用
nice_loading_button
插件可以帮助你创建带有加载动画的漂亮按钮。它基于 ElevatedButton
小部件,因此你可以使用通常的参数,并且还具有一些额外的功能。
Demo
使用方法
Nice Loading Button with CircularProgress Widget
LoadingButton(
height: 50,
borderRadius: 8,
animate: true,
color: Colors.indigo,
width: MediaQuery.of(context).size.width * 0.45,
loader: Container(
padding: const EdgeInsets.all(10),
width: 40,
height: 40,
child: const CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
),
child: const Text("Login"),
onTap: (startLoading, stopLoading, buttonState) async {
if (buttonState == ButtonState.idle) {
startLoading();
// Do something here
await Future.delayed(const Duration(seconds: 5));
stopLoading();
}
},
)
Nice Loading Button with Custom Text
LoadingButton(
height: 50,
borderRadius: 8,
roundLoadingShape: false,
color: Colors.blueAccent,
width: MediaQuery.of(context).size.width * 0.45,
minWidth: MediaQuery.of(context).size.width * 0.30,
loader: const Text("Loading..."),
child: const Text("Login"),
onTap: (startLoading, stopLoading, btnState) async {
if (btnState == ButtonState.idle) {
startLoading();
// Do something here
await Future.delayed(const Duration(seconds: 5));
stopLoading();
}
},
)
Nice Loading Button with Custom Loading
LoadingButton(
height: 50,
borderRadius: 8,
animate: true,
color: Colors.deepOrange,
width: MediaQuery.of(context).size.width * 0.45,
loader: Container(
padding: const EdgeInsets.all(10),
child: const Center(
child: SpinKitDoubleBounce(
color: Colors.white,
),
),
),
child: const Text("Login"),
onTap: (startLoading, stopLoading, btnState) async {
if (btnState == ButtonState.idle) {
startLoading();
// Do something here
await Future.delayed(const Duration(seconds: 5));
stopLoading();
}
},
)
属性
- animate (默认为 false): 当点击按钮时是否使用宽度进行动画。
- roundLoadingShape (默认为 true): 是否在忙碌/加载状态下使用圆角。
- width: 按钮在空闲状态下的宽度。
- minWidth: 按钮在忙碌/加载状态下的最小宽度。默认值等于高度,以便创建完全圆形的加载按钮。
- borderRadius: 按钮的圆角半径。
- borderSide: 按钮边框的颜色和宽度。
- child: 按钮在空闲状态下的内容。
- loader: 按钮在忙碌/加载状态下的内容。
- onTap: (startLoading, stopLoading, btnState): 点击按钮时调用的函数。
- duration: 动画持续时间。
- curve: 动画曲线。
- reverseCurve: 反向动画曲线。
完整示例代码
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:nice_loading_button/nice_loading_button.dart';
void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Nice Loading Button with CircularProgress widget
LoadingButton(
height: 50,
borderRadius: 8,
animate: true,
color: Colors.indigo,
width: MediaQuery.of(context).size.width * 0.44,
loader: Container(
padding: const EdgeInsets.all(10),
width: 40,
height: 40,
child: const CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
),
child: const Text("Login"),
onTap: (startLoading, stopLoading, buttonState) async {
if (buttonState == ButtonState.idle) {
startLoading();
// Do something here
await Future.delayed(const Duration(seconds: 5));
stopLoading();
}
},
),
const SizedBox(height: 50),
// Nice Loading Button with custom Text
LoadingButton(
height: 50,
borderRadius: 8,
roundLoadingShape: false,
color: Colors.red,
width: MediaQuery.of(context).size.width * 0.44,
minWidth: MediaQuery.of(context).size.width * 0.29,
loader: const Text("Logging in..."),
child: const Text("Login"),
onTap: (startLoading, stopLoading, buttonState) async {
if (buttonState == ButtonState.idle) {
startLoading();
// Do something here
await Future.delayed(const Duration(seconds: 5));
stopLoading();
}
},
),
const SizedBox(height: 50),
// Nice Loading Button with Custom Loading
LoadingButton(
height: 50,
borderRadius: 8,
animate: true,
color: Colors.cyan,
width: MediaQuery.of(context).size.width * 0.44,
loader: Container(
padding: const EdgeInsets.all(10),
child: const Center(
child: SpinKitDoubleBounce(
color: Colors.white,
),
),
),
child: const Text("Login"),
onTap: (startLoading, stopLoading, buttonState) async {
if (buttonState == ButtonState.idle) {
startLoading();
// Do something here
await Future.delayed(const Duration(seconds: 5));
stopLoading();
}
},
),
],
),
),
);
}
}
更多关于Flutter加载按钮美化插件nice_loading_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter加载按钮美化插件nice_loading_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter项目中使用nice_loading_button
插件来美化加载按钮的示例代码。这个插件允许你创建一个带有加载指示器的按钮,非常适合在提交表单或进行网络请求时使用。
首先,你需要在你的pubspec.yaml
文件中添加nice_loading_button
依赖:
dependencies:
flutter:
sdk: flutter
nice_loading_button: ^x.y.z # 请替换为最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,你可以在你的Dart文件中使用NiceLoadingButton
。下面是一个完整的示例,展示了如何在一个简单的Flutter应用中实现这个功能:
import 'package:flutter/material.dart';
import 'package:nice_loading_button/nice_loading_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Nice Loading Button Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isLoading = false;
void handleSubmit() async {
setState(() {
isLoading = true;
});
// 模拟一个异步操作,比如网络请求
await Future.delayed(Duration(seconds: 2));
setState(() {
isLoading = false;
});
// 提交成功后的操作,比如显示Snackbar
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('提交成功!')),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Nice Loading Button Demo'),
),
body: Center(
child: NiceLoadingButton(
status: isLoading ? ButtonStatus.loading : ButtonStatus.idle,
onPressed: (status) {
if (status == ButtonStatus.idle) {
handleSubmit();
}
},
width: 200,
height: 50,
color: Colors.blue,
borderColor: Colors.blueAccent,
borderRadius: 25,
child: Text(
isLoading ? '加载中...' : '提交',
style: TextStyle(color: Colors.white, fontSize: 18),
),
loadingIndicator: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
),
),
);
}
}
代码说明:
- 依赖添加:在
pubspec.yaml
中添加nice_loading_button
依赖。 - 按钮状态管理:在
_MyHomePageState
中定义一个isLoading
布尔变量来管理按钮的加载状态。 - 按钮点击事件:在
handleSubmit
函数中模拟一个异步操作(例如网络请求),并在操作完成后更新isLoading
状态。 - NiceLoadingButton配置:
status
:根据isLoading
的值设置按钮的状态。onPressed
:当按钮状态为idle
时,调用handleSubmit
函数。width
和height
:设置按钮的宽度和高度。color
和borderColor
:设置按钮的背景色和边框色。borderRadius
:设置按钮的圆角。child
:按钮的文字内容,根据加载状态显示不同的文本。loadingIndicator
:加载指示器,这里使用CircularProgressIndicator
。
这样,你就成功地在Flutter项目中使用了nice_loading_button
插件来美化你的加载按钮。