Flutter倒计时按钮插件timer_button_fork的使用
Flutter倒计时按钮插件timer_button_fork的使用
简介
Timer Button for Flutter
是一个Flutter包,提供了一个交互式的、自禁用按钮,该按钮在指定的超时时间后重新启用。在4.0.0版本中,我们引入了 TimerButton.builder
以提供更多自定义选项。
开始使用
1. 添加依赖
打开项目的 pubspec.yaml
文件,添加 timer_button_fork
依赖,并将 [version]
替换为最新版本:
dependencies:
flutter:
sdk: flutter
timer_button_fork: ^[version]
2. 安装依赖
运行以下命令来安装依赖:
flutter pub get
3. 导入包
在Dart文件中导入 timer_button_fork
包:
import 'package:timer_button_fork/timer_button_fork.dart';
使用方法
按钮类型
可以选择三种类型的按钮:
ElevatedButton
:buttonType: ButtonType.ElevatedButton
TextButton
:buttonType: ButtonType.TextButton
OutlinedButton
:buttonType: ButtonType.OutlinedButton
基本属性
- 标签文本:
label: "Try Again"
- 超时时间(秒):
timeOutInSeconds: 20
- 按钮颜色:
color: Colors.deepPurple
- 禁用时的颜色:
disabledColor: Colors.red
- 禁用时的文本样式:
disabledTextStyle: TextStyle(fontSize: 20.0)
- 启用时的文本样式:
activeTextStyle: TextStyle(fontSize: 20.0, color: Colors.white)
高级自定义
可以使用 TimerButton.builder
来自定义按钮在超时期间的外观和行为。
示例代码
传统用法
TimerButton(
label: "Send OTP Again",
timeOutInSeconds: 20,
onPressed: () {},
disabledColor: Colors.red,
color: Colors.deepPurple,
disabledTextStyle: TextStyle(fontSize: 20.0),
activeTextStyle: TextStyle(fontSize: 20.0, color: Colors.white),
)
使用 TimerButton.builder
TimerButton.builder(
timeOutInSeconds: 20,
timeBuilder: (BuildContext context, int seconds) {
return Text("$seconds seconds");
},
onPressed: () {
print('Button Pressed!');
},
)
完整示例Demo
以下是一个完整的示例应用,展示了如何使用 timer_button_fork
插件创建不同类型的倒计时按钮。
import 'package:flutter/material.dart';
import 'package:timer_button_fork/timer_button_fork.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Timer Button Fork Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
[@override](/user/override)
MyHomePageState createState() {
return MyHomePageState();
}
}
class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Timer Button Demo'),
),
body: Material(
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
// ElevatedButton 类型的倒计时按钮
TimerButton(
label: "Elevated: Try Again",
timeOutInSeconds: 5,
onPressed: () {},
buttonType: ButtonType.ElevatedButton,
color: Colors.green,
),
// OutlinedButton 类型的倒计时按钮
TimerButton(
label: "Outlined: Try Again",
timeOutInSeconds: 5,
onPressed: () {},
buttonType: ButtonType.OutlinedButton,
disabledColor: Colors.deepOrange,
color: Colors.green,
activeTextStyle: TextStyle(color: Colors.yellow),
disabledTextStyle: TextStyle(color: Colors.pink),
),
// TextButton 类型的倒计时按钮
TimerButton(
label: "Text: Try Again",
timeOutInSeconds: 5,
onPressed: () {
print("Time for some action!");
},
buttonType: ButtonType.TextButton,
disabledColor: Colors.deepOrange,
color: Colors.green,
),
// 使用 TimerButton.builder 自定义倒计时按钮
TimerButton.builder(
onPressed: () {
print("Hey, a custom button is clicked!");
},
timeOutInSeconds: 5,
timeBuilder: (BuildContext context, int sec) {
return Container(
padding: EdgeInsets.all(10.0),
color: sec > 0 ? Colors.red : Colors.green,
child: Text(
sec > 0 ? "Custom Wait for $sec seconds" : "Click Me",
style: TextStyle(color: Colors.white),
),
);
},
),
],
),
),
),
),
);
}
}
更多关于Flutter倒计时按钮插件timer_button_fork的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter倒计时按钮插件timer_button_fork的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何使用 timer_button_fork
插件在 Flutter 中实现倒计时按钮的示例代码。这个插件允许你创建一个带有倒计时功能的按钮,这在需要用户等待一段时间后再执行操作的场景中非常有用,比如发送验证码、重新获取数据等。
首先,你需要在你的 pubspec.yaml
文件中添加对 timer_button_fork
的依赖:
dependencies:
flutter:
sdk: flutter
timer_button_fork: ^最新版本号 # 请替换为实际的最新版本号
然后运行 flutter pub get
来获取依赖。
接下来,在你的 Dart 文件中,你可以这样使用 TimerButton
:
import 'package:flutter/material.dart';
import 'package:timer_button_fork/timer_button.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Timer Button Example'),
),
body: Center(
child: MyTimerButton(),
),
),
);
}
}
class MyTimerButton extends StatefulWidget {
@override
_MyTimerButtonState createState() => _MyTimerButtonState();
}
class _MyTimerButtonState extends State<MyTimerButton> {
final int initialCountdown = 60; // 初始倒计时秒数
@override
Widget build(BuildContext context) {
return TimerButton(
initialCountdown: initialCountdown,
autoStart: true,
onPressed: () {
// 当倒计时结束时执行的回调
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Button clicked after countdown!')),
);
},
builder: (context, state) {
return TextButton(
onPressed: state == TimerButtonState.countdown
? null // 倒计时期间禁用按钮
: () {}, // 当倒计时结束时启用按钮(这里实际点击逻辑在onPressed中处理)
style: TextButton.styleFrom(
primary: state == TimerButtonState.countdown
? Colors.grey // 倒计时期间按钮变为灰色
: Colors.blue,
),
child: Text(
state == TimerButtonState.countdown
? '${state.remainingTime!.inSeconds}' // 显示剩余时间
: 'Send Code', // 倒计时结束后的按钮文本
),
);
},
);
}
}
在这个示例中:
- 我们创建了一个简单的 Flutter 应用,其中包含一个使用
TimerButton
的自定义按钮组件MyTimerButton
。 TimerButton
的initialCountdown
属性设置了初始倒计时秒数。autoStart
属性设置为true
,表示按钮创建后会立即开始倒计时。onPressed
回调在倒计时结束后被调用,这里我们简单地显示了一个 SnackBar。builder
属性允许我们自定义按钮的外观和行为。在倒计时期间,按钮被禁用且显示为灰色,同时显示剩余时间。倒计时结束后,按钮文本变为 “Send Code” 并恢复可点击状态。
这个示例展示了如何使用 timer_button_fork
插件创建一个基本的倒计时按钮,你可以根据自己的需求进一步自定义和扩展。