Flutter尼泊尔年月选择器插件nepali_month_year_picker的使用
Flutter尼泊尔年月选择器插件nepali_month_year_picker的使用
nepali_month_year_picker
是一个基于尼泊尔日历的年月选择器插件。它受到了 zulfahmi.dev
的 month year picker
的启发,并且借助了 nepali_utils
包来实现。
快速开始
以下是使用该插件的基本步骤:
-
打开你的
pubspec.yaml
文件并添加以下依赖项:nepali_month_year_picker: ^0.0.1+1
或者直接在终端运行以下命令:
flutter pub add nepali_month_year_picker
-
运行
flutter pub get
来获取新的依赖项。 -
在你的 Dart 代码中导入该库:
import 'package:nepali_month_year_picker/nepali_month_year_picker.dart';
同时确保你的
MaterialApp
包含以下localizationsDelegates
:MaterialApp( ... localizationsDelegates: const [ GlobalMaterialLocalizations.delegate, MonthYearPickerLocalizations.delegate, ], )
-
在代码中使用该插件:
final selected = await showNepaliMonthYearPicker( context: context, initialDate: NepaliDateTime.now(), firstDate: NepaliDateTime(2019), lastDate: NepaliDateTime(2023), );
参数
以下是一些关键参数及其描述:
参数 | 描述 |
---|---|
context |
不得为空。将传递给内部的 showDialog 函数调用。 |
initialDate |
不得为空,并且必须在 firstDate 和 lastDate 之间。initialDate 将被截断为它的 year 和 month 组件。当第一次显示月/年选择器时,它将显示 initialDate 的月份/年份,initialDate 被选中。 |
firstDate |
不得为空。firstDate 将被截断为它的 year 和 month 组件。这是最早的允许月份/年份。 |
lastDate |
不得为空。lastDate 将被截断为它的 year 和 month 组件。这是最新的允许月份/年份。 |
selectableMonthYearPredicate |
可以为 null 。提供对哪些月份/年份可以被选中的完全控制。如果提供,则只有 selectableMonthYearPredicate 返回 true 的月份/年份才可被选择。 |
locale |
可以为 null 。如果提供,将用于设置月/年选择器的区域设置。否则,默认使用由 Localizations 提供的环境区域设置。只能设置为 en (默认)和 ne (尼泊尔语)。 |
language |
可以为 null 。如果提供,将使用 nepali_utils 包的尼泊尔格式化程序。可以设置为 Language.english 或 Language.nepali 。 |
useRootNavigator |
可以为 null 。将传递给内部的 showDialog 函数调用。 |
routeSettings |
可以为 null 。将传递给内部的 showDialog 函数调用。 |
textDirection |
可以为 null 。如果提供,将用于设置月/年选择器的文本方向。否则,默认使用由 Directionality 提供的环境文本方向。 |
builder |
可以为 null 。此参数可用于包装对话框小部件。 |
initialMonthYearPickerMode |
必须不得为空。可以用来让年选择器最初出现在 MonthYearPickerMode.year 模式。默认模式为 MonthYearPickerMode.month 。 |
示例 Demo
你可以通过以下示例代码了解如何使用该插件:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:nepali_month_year_picker/nepali_month_year_picker.dart';
import 'package:nepali_utils/nepali_utils.dart';
void main() {
runApp(const ExampleApp());
}
class ExampleApp extends StatelessWidget {
const ExampleApp({
Key? key,
}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Nepali Month Year Picker Example',
home: const MyHomePage(),
theme: ThemeData(useMaterial3: true),
localizationsDelegates: const [
GlobalWidgetsLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
MonthYearPickerLocalizations.delegate,
],
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
NepaliDateTime? _selected;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Nepali Month Year Picker Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_selected == null)
const Text('No month year selected.')
else ...[
CalendarIconButton(
onNext: onNextPressed,
onPrev: onPrevPressed,
onTap: () => _onPressed(context: context),
title: NepaliDateFormat(
'MMMM yyyy',
).format(_selected!)),
Text(NepaliDateFormat('MMMM yyyy', Language.nepali)
.format(_selected!)),
],
TextButton(
child: const Text('DEFAULT LOCALE'),
onPressed: () => _onPressed(context: context),
),
TextButton(
child: const Text('नेपाली'),
onPressed: () => _onPressed(
context: context,
locale: 'ne',
),
),
],
),
),
);
}
void onNextPressed() {
if (_selected!.month + 1 > 12) {
_selected = NepaliDateTime(_selected!.year + 1, 1);
} else {
_selected = NepaliDateTime(_selected!.year, _selected!.month + 1);
}
setState(() {});
}
void onPrevPressed() {
if (_selected!.month - 1 < 1) {
_selected = NepaliDateTime(_selected!.year + 1, 12);
} else {
_selected = NepaliDateTime(_selected!.year, _selected!.month - 1);
}
setState(() {});
}
Future<void> _onPressed({
required BuildContext context,
String? locale,
}) async {
final localeObj = locale != null ? Locale(locale) : const Locale("en");
final selected = await showNepaliMonthYearPicker(
context: context,
initialDate: _selected ?? NepaliDateTime.now(),
firstDate: NepaliDateTime(2070),
lastDate: NepaliDateTime(2090),
locale: localeObj,
);
if (selected != null) {
setState(() {
_selected = selected;
});
}
}
}
class CalendarIconWidget extends StatelessWidget {
const CalendarIconWidget({super.key, required this.icon, required this.func});
final Widget icon;
final VoidCallback func;
[@override](/user/override)
Widget build(BuildContext context) {
return GestureDetector(
onTap: func,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: icon,
),
);
}
}
class CalendarIconButton extends StatelessWidget {
const CalendarIconButton(
{super.key,
required this.onNext,
required this.onPrev,
required this.onTap,
required this.title});
final VoidCallback onNext;
final VoidCallback onPrev;
final VoidCallback onTap;
final String title;
[@override](/user/override)
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CalendarIconWidget(
icon: const Icon(
Icons.arrow_back_ios,
),
func: onPrev,
),
const Icon(
Icons.calendar_month,
),
Text(
title,
),
CalendarIconWidget(
icon: const Icon(
Icons.arrow_forward_ios,
),
func: onNext,
),
],
),
),
);
}
}
更多关于Flutter尼泊尔年月选择器插件nepali_month_year_picker的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter尼泊尔年月选择器插件nepali_month_year_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中集成和使用nepali_month_year_picker
插件的一个代码案例。这个插件允许用户选择尼泊尔的年月。
首先,确保你已经在pubspec.yaml
文件中添加了nepali_month_year_picker
依赖:
dependencies:
flutter:
sdk: flutter
nepali_month_year_picker: ^最新版本号 # 请替换为最新版本号
然后,运行flutter pub get
来获取依赖。
接下来,在你的Dart文件中,你可以按照以下方式使用NepaliMonthYearPicker
:
import 'package:flutter/material.dart';
import 'package:nepali_month_year_picker/nepali_month_year_picker.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Nepali Month Year Picker Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
DateTime? selectedDate;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Nepali Month Year Picker Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
selectedDate == null
? 'Select a date'
: 'Selected Date: ${selectedDate!.toLocal()}',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
final DateTime? result = await showModalBottomSheet<DateTime?>(
context: context,
builder: (BuildContext context) {
return Container(
height: 250,
child: NepaliMonthYearPicker(
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2100),
onChanged: (DateTime? date) {
// This is called when the user selects a date.
print('Date selected: $date');
},
onConfirm: (DateTime? date) {
Navigator.of(context).pop(date);
},
),
);
},
);
if (result != null) {
setState(() {
selectedDate = result;
});
}
},
child: Text('Select Date'),
),
],
),
),
);
}
}
代码解释
-
依赖导入:
import 'package:flutter/material.dart'; import 'package:nepali_month_year_picker/nepali_month_year_picker.dart';
-
主应用入口:
void main() { runApp(MyApp()); }
-
无状态组件
MyApp
:class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Nepali Month Year Picker Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } }
-
有状态组件
MyHomePage
:- 包含一个
DateTime?
类型的selectedDate
状态变量,用于存储用户选择的日期。 build
方法构建UI,包含一个显示选择日期的文本和一个按钮,点击按钮会弹出选择器。
- 包含一个
-
弹出选择器:
- 使用
showModalBottomSheet
显示选择器。 NepaliMonthYearPicker
组件用于选择尼泊尔的年月。onChanged
回调在用户选择日期时调用。onConfirm
回调在用户确认选择后调用,并将选择的日期返回给父组件。
- 使用
-
更新状态:
- 当用户确认选择后,更新
selectedDate
状态变量,并刷新UI显示选择的日期。
- 当用户确认选择后,更新
希望这段代码能帮助你理解如何在Flutter项目中使用nepali_month_year_picker
插件。如果有任何问题或需要进一步的帮助,请随时告诉我!