Flutter 中避免不了使用日期、時間選擇器,這裏咱們使用官方showDatePicker
與showTimePicker
。ui
要想使得這兩個組件爲中文的先決條件爲項目加入國際化(flutter_localizations
)。code
在/pubspec.yaml
中:ci
... dependencies: flutter: sdk: flutter flutter_localizations: # 添加 sdk: flutter # 添加 ...
/lib/main.dart
中:get
import 'package:flutter_localizations/flutter_localizations.dart'; // 添加 // MaterialApp中加入 ... localizationsDelegates: [ GlobalMeterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ], supportedLocales: [ const Locale('zh', 'CH'), const Locale('en', 'US'), ], locale: const locale('zh'), ...
默認使用MaterialApp
設置中的。 showDatePicker
提供了locale
參數,若是有須要能夠繼續更改。it
默認使用MaterialApp
設置中的。 showTimePicker
沒有提供locale
參數,若是想改變語言可使用:io
await showTimePicker( context: context, initialTime: TimeOfDay.now(), builder: (BuildContext context, Widget child) { return Localizations( locale: const Locale('zh'), child: child, delegates: <LocalizationsDelegate>[ GlobalMeterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ] ) } )