1、时间
DateTime dateTime = DateTime.now();
print(dateTime);
print(dateTime.microsecondsSinceEpoch);
//时间加减
print(dateTime.add(const Duration(days: 5)));
print(dateTime.add(const Duration(days: -5)));
//日期转换成时间 时间戳 时间戳转为时间
DateTime dateTime2 = DateTime(2023, 08, 07);
print(dateTime2);
print(dateTime2.microsecondsSinceEpoch);
print(DateTime.fromMillisecondsSinceEpoch(dateTime2.millisecondsSinceEpoch));
//获取当前时间
TimeOfDay timeOfDay = TimeOfDay.now();
print(timeOfDay);
2、日期选择弹窗及时间选择弹窗
class _TimePagerState extends State<TimePager> {
DateTime dateTime = DateTime.now();
TimeOfDay timeOfDay = TimeOfDay.now();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("时间")),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
InkWell(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("${dateTime.year}-${dateTime.month}-${dateTime.day}"),
const Icon(Icons.arrow_drop_down),
],
),
onTap: () async {
DateTime? time = await showDatePicker(
context: context,
initialDate: dateTime,
//设置中文
locale: const Locale('zh'),
firstDate: DateTime(1900),
lastDate: DateTime(2035));
if (time != null) {
setState(() {
dateTime = time;
});
}
},
),
const SizedBox(
height: 40,
),
InkWell(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("${timeOfDay.hour}-${timeOfDay.minute}"),
const Icon(Icons.arrow_drop_down),
],
),
onTap: () async {
TimeOfDay? timeOfDay2 = await showTimePicker(
context: context, initialTime: timeOfDay);
if (timeOfDay2 != null) {
setState(() {
timeOfDay = timeOfDay2;
});
}
},
)
]),
),
);
}
}
3、国际化
1、配置flutter_localizations依赖
找到pubspec.yaml配置flutter_localizations
flutter_localizations:
sdk: flutter
2、main.dart导入国际化的包 flutter_localizations
import 'package:flutter_localizations/flutter_localizations.dart';
3、设置国际化
localizationsDelegates: const [
//此处设置国际化
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: const [
//此处设置国际化
Locale('zh', 'CH'),
Locale('en', 'US'),
],
网友评论