Flutter 国际化
在pubspec.yaml中添加配置
dependencies:flutter:sdk: flutterflutter_localizations:sdk: flutter
配置MaterialApp
return MaterialApp(......localizationsDelegates: [GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate,],supportedLocales: [const Locale("zh","CH"),const Locale("en","US")],locale: Locale("zh"),);
示例
void main() {runApp(MyPage());
}class MyPage extends StatelessWidget { Widget build(BuildContext context) {return MaterialApp(home: FulPage(),localizationsDelegates: [GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate,],supportedLocales: [const Locale("zh","CH"),const Locale("en","US")],locale: Locale("zh"),);}
}class FulPage extends StatefulWidget { State<StatefulWidget> createState() {return MyState();}
}class MyState extends State {String time = TimeOfDay.fromDateTime(DateTime.now()).toString();Future<TimeOfDay?> showTime(context) async {TimeOfDay? timeOfDay = await showTimePicker(context: context,initialTime: TimeOfDay.fromDateTime(DateTime.now()),);setState(() {time = timeOfDay.toString();});return timeOfDay;} Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("选择时间")),body: Text(time),floatingActionButton: FloatingActionButton(onPressed: () {showTime(context);},child: Icon(Icons.account_box_rounded),),);}
}