],
supportedLocales: [
const Locale(‘zh’),
const Locale(‘en’),
],
home: _HomePage(),
)
2.6 整体的代码
import ‘package:flutter/foundation.dart’;
import ‘package:flutter/material.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
localizationsDelegates: [
AppLocalizationsDelegate(),
],
supportedLocales: [
const Locale(‘en’),
const Locale(‘zh’)
],
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}
class AppLocalizations {
final Locale locale;
AppLocalizations(this.locale);
static AppLocalizations? of(BuildContext context) {
return Localizations.of(context, AppLocalizations);
}
static Map<String, Map<String, String>> _localizedValues = {
‘en’: {
‘title’: ‘Hello World’,
},
‘zh’: {
‘title’: ‘你好’,
},
};
String? get title {
return _localizedValues[locale.languageCode]?[‘title’];
}
}
class AppLocalizationsDelegate extends LocalizationsDelegate {
const AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) => [‘en’, ‘zh’].contains(locale.languageCode);
@override
Future load(Locale locale) {
return SynchronousFuture(AppLocalizations(locale));
}
@override
bool shouldReload(AppLocalizationsDelegate old) => false;
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
//appBar: AppBar(title: Text(widget.title),),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(‘国际化:${AppLocalizations.of(context)!.title}’)
],
),
),
);
}
}
2.7 注意事项-Scaffold 不要添加 AppBar 数据,否则报错(切换到zh时)
异常信息
======== Exception caught by widgets library =======================================================
The following assertion was thrown building AppBar(dirty, dependencies: [_LocalizationsScope-[GlobalKey#c2dbf], _ModalScopeStatus, MediaQuery, _InheritedTheme], state: _AppBarState#8c982):
No MaterialLocalizations found.
AppBar widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.
To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
2.8 效果图
[外链图片转存中…(img-aCKKXwyD-1710967604998)]
3.1 说明
Scaffold添加 AppBar 数据,切换到中文环境时,会出现错误
======== Exception caught by widgets library =======================================================
The following assertion was thrown building AppBar(dirty, dependencies: [_LocalizationsScope-[GlobalKey#c2dbf], _ModalScopeStatus, MediaQuery, _InheritedTheme], state: _AppBarState#8c982):
No MaterialLocalizations found.
原因:MaterialLocalizations 找不到
3.2 pubspec.yaml文件中添加包依赖
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
3.3 MaterialApp 修改如下
import ‘package:flutter_localizations/flutter_localizations.dart’;
MaterialApp(
title: ‘Flutter IntlApp’,
结尾
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,在这里免费分享给大家,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-r82nCIm7-1710967604999)]
[外链图片转存中…(img-up7rtC1P-1710967605000)]
[外链图片转存中…(img-0T0CicXP-1710967605000)]
[外链图片转存中…(img-COyWXyE8-1710967605000)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-jNECJ5HW-1710967605000)]