Flutter时间选择器插件flutter_spinner_time_picker的使用
Flutter时间选择器插件flutter_spinner_time_picker的使用
flutter_spinner_time_picker
是一个用于在Flutter应用程序中轻松选择时间的自定义时间选择器插件。本文将介绍如何安装和使用这个插件,并提供一个完整的示例。
预览
深色模式
浅色模式
安装
在您的 pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter_spinner_time_picker: [latest]
然后运行 flutter pub get
来安装该插件。
使用方法
首先,导入该插件:
import 'package:flutter_spinner_time_picker/flutter_spinner_time_picker.dart';
如何使用
以下是基本用法示例:
TimeOfDay selectedTime = TimeOfDay.now();
void _showTimePicker() async {
final pickedTime = await showSpinnerTimePicker(
context,
initTime: selectedTime,
);
if (pickedTime != null) {
setState(() {
selectedTime = pickedTime;
});
}
}
自定义选项
该插件提供了许多自定义选项,例如标题、背景颜色、前景颜色等:
showSpinnerTimePicker(
context,
title: 'Custom Time Picker',
backgroundColor: Colors.grey[100],
foregroundColor: Colors.black,
titleStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
buttonStyle: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
buttonTextStyle: TextStyle(fontSize: 16, color: Colors.white),
barrierDismissible: true,
// ... other customization options
);
完整示例
以下是一个简单的完整示例,展示了如何使用自定义时间选择器小部件:
import 'package:flutter/material.dart';
import 'package:flutter_spinner_time_picker/flutter_spinner_time_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Time Picker Example',
darkTheme: ThemeData(
primarySwatch: Colors.deepPurple,
useMaterial3: true,
brightness: Brightness.dark,
),
theme: ThemeData(
primarySwatch: Colors.deepPurple,
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TimeOfDay selectedTime = TimeOfDay.now();
void _showTimePicker() async {
final pickedTime = await showSpinnerTimePicker(
context,
initTime: selectedTime,
is24HourFormat: false,
);
if (pickedTime != null) {
setState(() {
selectedTime = pickedTime;
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Time Picker Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Selected Time:',
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 10),
Text(
'${selectedTime.hour}:${selectedTime.minute.toString().padLeft(2, '0')}',
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
FilledButton(
onPressed: _showTimePicker,
child: const Text('Pick a Time'),
),
],
),
),
);
}
}
更多关于Flutter时间选择器插件flutter_spinner_time_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter时间选择器插件flutter_spinner_time_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter应用中使用flutter_spinner_time_picker
插件的示例代码。这个插件允许你以旋转选择器的方式选择时间。
首先,你需要在你的pubspec.yaml
文件中添加这个插件的依赖:
dependencies:
flutter:
sdk: flutter
flutter_spinner_time_picker: ^2.0.0 # 请检查最新版本号
然后运行flutter pub get
来获取依赖。
接下来,你可以在你的Flutter应用中使用这个插件。以下是一个简单的示例,展示如何集成和使用flutter_spinner_time_picker
:
import 'package:flutter/material.dart';
import 'package:flutter_spinner_time_picker/flutter_spinner_time_picker.dart';
import 'package:flutter_spinner_time_picker/src/constants.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Spinner Time Picker Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TimeOfDay? selectedTime;
void _selectTime(BuildContext context) async {
final TimeOfDay? picked = await showTimePicker(
context: context,
initialTime: selectedTime ?? TimeOfDay.now(),
builder: (BuildContext context, Widget? child) {
return Theme(
data: ThemeData.light().copyWith(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
).copyWith(
background: Colors.white,
),
),
child: FlutterSpinnerTimePicker.dialog(
context,
TimeOfDay.now(),
onChanged: (TimeOfDay time) {
// 这里可以处理时间变化时的逻辑,如果需要的话
},
locale: Localizations.localeOf(context),
),
);
},
);
if (picked != null && picked != selectedTime) {
setState(() {
selectedTime = picked;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Spinner Time Picker Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
selectedTime == null
? 'No time selected'
: '${selectedTime!.format(context)}',
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () => _selectTime(context),
child: Text('Select Time'),
),
],
),
),
);
}
}
在这个示例中:
- 我们首先定义了
MyApp
和MyHomePage
,其中MyHomePage
是一个有状态的小部件,用于存储和更新选中的时间。 _selectTime
函数用于显示时间选择器对话框。我们使用了showTimePicker
函数,并传入了一个自定义的builder
,它使用FlutterSpinnerTimePicker.dialog
来显示旋转选择器。- 当用户选择了一个时间后,我们更新
selectedTime
状态,这会触发UI的重新构建,显示选中的时间。
这个示例展示了如何集成和使用flutter_spinner_time_picker
插件来在Flutter应用中显示和选择时间。你可以根据需要进一步自定义和扩展这个示例。