Flutter动画进度按钮插件animate_progress_button的使用
Flutter动画进度按钮插件animate_progress_button的使用
animate_progress_button
是一个用于在 Flutter 应用中创建具有动画效果的进度按钮的插件。通过该插件,您可以轻松地为按钮添加加载状态、成功状态和失败状态,并且可以自定义这些状态下的文本和图标。
使用步骤
-
添加依赖 在您的
pubspec.yaml
文件中添加animate_progress_button
依赖项:dependencies: animate_progress_button: ^0.0.1
-
导入包 在需要使用进度按钮的 Dart 文件中导入
animate_progress_button
包:import 'package:animate_progress_button/animate_progress_button.dart';
-
创建进度按钮 使用
ProgressButton.icon
创建一个带有图标的进度按钮,或者使用ProgressButton.text
创建一个仅有文本的进度按钮。
完整示例代码
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:animate_progress_button/animate_progress_button.dart';
import 'package:animate_progress_button/icon_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// 这个小部件是你的应用的根组件。
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: '进度按钮示例',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: ProgressButtonHomePage(title: '进度按钮'),
);
}
}
class ProgressButtonHomePage extends StatefulWidget {
ProgressButtonHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
_ProgressButtonHomePageState createState() => _ProgressButtonHomePageState();
}
class _ProgressButtonHomePageState extends State<ProgressButtonHomePage> {
ButtonState stateOnlyText = ButtonState.idle;
ButtonState stateTextWithIcon = ButtonState.idle;
ButtonState stateTextWithIconMinWidthState = ButtonState.idle;
Widget buildTextWithIcon() {
return ProgressButton.icon(
iconedButtons: {
ButtonState.idle: IconedButton(
text: "登录",
color: Color.fromRGBO(84, 157, 255, 1),
icon: Icon(Icons.send, color: Colors.white),
),
ButtonState.loading: IconedButton(
text: "加载中",
color: Color.fromRGBO(84, 157, 255, 1),
),
ButtonState.fail: IconedButton(
text: "失败",
color: Colors.red,
),
ButtonState.success: IconedButton(
text: "",
icon: Icon(Icons.check_circle, color: Colors.white),
color: Color.fromRGBO(84, 157, 255, 1),
),
},
onPressed: onPressedIconWithText,
state: stateTextWithIcon,
);
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 可以在这里添加其他类型的按钮
Container(
height: 32,
),
buildTextWithIcon(),
Container(
height: 32,
),
// 可以在这里添加其他类型的按钮
],
),
),
);
}
void onPressedCustomButton() {
setState(() {
switch (stateOnlyText) {
case ButtonState.idle:
stateOnlyText = ButtonState.loading;
break;
case ButtonState.loading:
stateOnlyText = ButtonState.fail;
break;
case ButtonState.success:
stateOnlyText = ButtonState.idle;
break;
case ButtonState.fail:
stateOnlyText = ButtonState.success;
break;
}
});
}
void onPressedIconWithText() {
switch (stateTextWithIcon) {
case ButtonState.idle:
stateTextWithIcon = ButtonState.loading;
Future.delayed(Duration(seconds: 1), () {
setState(() {
stateTextWithIcon = Random.secure().nextBool()
? ButtonState.success
: ButtonState.fail;
});
});
break;
case ButtonState.loading:
break;
case ButtonState.success:
stateTextWithIcon = ButtonState.idle;
break;
case ButtonState.fail:
stateTextWithIcon = ButtonState.idle;
break;
}
setState(() {
stateTextWithIcon = stateTextWithIcon;
});
}
void onPressedIconWithMinWidthStateText() {
switch (stateTextWithIcon) {
case ButtonState.idle:
stateTextWithIconMinWidthState = ButtonState.loading;
Future.delayed(Duration(seconds: 1), () {
setState(() {
stateTextWithIconMinWidthState = Random.secure().nextBool()
? ButtonState.success
: ButtonState.fail;
});
});
break;
case ButtonState.loading:
break;
case ButtonState.success:
stateTextWithIconMinWidthState = ButtonState.idle;
break;
case ButtonState.fail:
stateTextWithIconMinWidthState = ButtonState.idle;
break;
}
setState(() {
stateTextWithIconMinWidthState = stateTextWithIconMinWidthState;
});
}
}
更多关于Flutter动画进度按钮插件animate_progress_button的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter动画进度按钮插件animate_progress_button的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
animate_progress_button
是一个用于 Flutter 的插件,它允许你创建一个带有动画进度指示的按钮。这个插件通常用于需要显示任务进度的情况,比如文件上传、下载或其他耗时的操作。
安装
首先,你需要在 pubspec.yaml
文件中添加依赖项:
dependencies:
flutter:
sdk: flutter
animate_progress_button: ^1.0.0
然后运行 flutter pub get
来安装依赖。
使用示例
下面是一个简单的示例,展示了如何使用 animate_progress_button
插件来创建一个带有动画进度指示的按钮。
import 'package:flutter/material.dart';
import 'package:animate_progress_button/animate_progress_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Animate Progress Button Example'),
),
body: Center(
child: ProgressButtonExample(),
),
),
);
}
}
class ProgressButtonExample extends StatefulWidget {
[@override](/user/override)
_ProgressButtonExampleState createState() => _ProgressButtonExampleState();
}
class _ProgressButtonExampleState extends State<ProgressButtonExample> {
double _progress = 0.0;
void _startProgress() async {
for (int i = 0; i <= 100; i++) {
await Future.delayed(Duration(milliseconds: 50));
setState(() {
_progress = i / 100.0;
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AnimatedProgressButton(
progress: _progress,
onPressed: _startProgress,
child: Text('Start Progress'),
backgroundColor: Colors.blue,
progressColor: Colors.green,
borderColor: Colors.blue,
borderRadius: 20.0,
height: 50.0,
width: 200.0,
),
SizedBox(height: 20),
Text('Progress: ${(_progress * 100).toStringAsFixed(0)}%'),
],
);
}
}