Flutter时间选择器插件from_to_time_picker的使用
Flutter时间选择器插件from_to_time_picker的使用
From To Time Picker
From To Time Picker 是一个简单的交互式时间范围选择器,允许用户通过选择开始时间和结束时间来确定一天中的特定时间段,并定义是上午(AM)还是下午(PM),最终返回所选持续时间的24小时格式。
使用方法
添加依赖
在项目的 pubspec.yaml
文件中添加以下依赖:
dependencies:
...
from_to_time_picker: ^1.2.2
导入包
在 Dart 文件中导入该库:
import 'package:from_to_time_picker/from_to_time_picker.dart';
示例代码
默认模式
默认模式下使用 showDialog
方法展示时间选择器对话框:
showDialog(
context: context,
builder: (_) => FromToTimePicker(
onTab: (from, to) {
print('from $from to $to');
},
),
);
自定义模式
自定义模式下可以设置颜色、文本等样式属性:
showDialog(
context: context,
builder: (_) => FromToTimePicker(
onTab: (from, to) {
print('from $from to $to');
},
dialogBackgroundColor: Color(0xFF121212),
fromHeadlineColor: Colors.white,
toHeadlineColor: Colors.white,
upIconColor: Colors.white,
downIconColor: Colors.white,
timeBoxColor: Color(0xFF1E1E1E),
timeHintColor: Colors.grey,
timeTextColor: Colors.white,
dividerColor: Color(0xFF121212),
doneTextColor: Colors.white,
dismissTextColor: Colors.white,
defaultDayNightColor: Color(0xFF1E1E1E),
defaultDayNightTextColor: Colors.white,
colonColor: Colors.white,
showHeaderBullet: true,
headerText: 'Time available from 01:00 AM to 11:00 PM',
),
);
完整示例Demo
以下是完整的 Demo 代码,包含两个按钮分别用于展示浅色和深色主题的时间选择器:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:from_to_time_picker/from_to_time_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'From To Time Picker',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String startTime = 'from';
String endTime = 'to';
void showLightTimePicker() {
showDialog(
context: context,
builder: (_) => FromToTimePicker(
maxWidth: 600,
onTab: (from, to) {
if (kDebugMode) {
print('from $from to $to');
}
setState(() {
startTime = from.hour.toString().padLeft(2, '0');
endTime = to.hour.toString().padLeft(2, '0');
});
Navigator.pop(context);
},
),
);
}
void showDarkTimePicker() {
showDialog(
context: context,
builder: (_) => FromToTimePicker(
onTab: (from, to) {
if (kDebugMode) {
print('from $from to $to');
}
setState(() {
startTime = from.hour.toString().padLeft(2, '0');
endTime = to.hour.toString().padLeft(2, '0');
});
Navigator.pop(context);
},
dialogBackgroundColor: const Color(0xFF121212),
fromHeadlineColor: Colors.white,
toHeadlineColor: Colors.white,
upIconColor: Colors.white,
downIconColor: Colors.white,
timeBoxColor: const Color(0xFF1E1E1E),
timeHintColor: Colors.grey,
timeTextColor: Colors.white,
dividerColor: const Color(0xFF1E1E1E),
doneTextColor: Colors.white,
dismissTextColor: Colors.white,
defaultDayNightColor: const Color(0xFF1E1E1E),
defaultDayNightTextColor: Colors.white,
colonColor: Colors.white,
showHeaderBullet: true,
headerText: 'Time available from 01:00 AM to 11:00 PM',
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Selected duration:'),
const SizedBox(height: 10),
Text('$startTime - $endTime'),
const SizedBox(height: 40),
ElevatedButton(
onPressed: () => showLightTimePicker(),
child: const Text('Show Light Time Picker'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => showDarkTimePicker(),
child: const Text('Show Dark Time Picker'),
),
],
),
),
);
}
}
贡献者
我们欢迎并接受以下贡献:
- 改进文档
- 报告问题
- 修复漏洞
维护者
- Ahmed Elsarag
更多关于Flutter时间选择器插件from_to_time_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter时间选择器插件from_to_time_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用from_to_time_picker
插件的一个示例代码案例。这个插件允许用户选择一个起始时间和一个结束时间。首先,你需要确保在pubspec.yaml
文件中添加了该插件的依赖:
dependencies:
flutter:
sdk: flutter
from_to_time_picker: ^^最新版本号(请检查pub.dev获取最新版本)
然后运行flutter pub get
来安装依赖。
接下来,在你的Flutter应用中,你可以按照以下步骤来使用from_to_time_picker
插件:
- 导入插件:
在你的Dart文件中导入插件:
import 'package:from_to_time_picker/from_to_time_picker.dart';
- 使用FromToTimePicker:
以下是一个完整的示例,展示如何在Flutter中使用FromToTimePicker
:
import 'package:flutter/material.dart';
import 'package:from_to_time_picker/from_to_time_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter FromToTimePicker Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TimeOfDay? fromTime;
TimeOfDay? toTime;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('FromToTimePicker Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'From Time: ${fromTime != null ? fromTime!.format(context) : 'Not selected'}',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20),
Text(
'To Time: ${toTime != null ? toTime!.format(context) : 'Not selected'}',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 40),
ElevatedButton(
onPressed: () {
showFromToTimePicker(
context: context,
initialFromTime: fromTime,
initialToTime: toTime,
locale: Localizations.localeOf(context),
).then((result) {
if (result != null) {
setState(() {
fromTime = result.fromTime;
toTime = result.toTime;
});
}
});
},
child: Text('Select Time Range'),
),
],
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮,点击该按钮将显示一个时间选择器对话框,允许用户选择一个起始时间和一个结束时间。选择完成后,时间将显示在屏幕上。
showFromToTimePicker
函数用于显示时间选择器对话框,并返回用户选择的时间范围。我们通过setState
方法更新状态,以便在UI中显示选择的时间。
确保你已经正确安装并导入了from_to_time_picker
插件,并且你的Flutter环境已经设置好。运行这个示例代码,你应该能够看到一个简单的时间选择器演示应用。