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;
}
2.5 将此 Delegate 添加到 MaterialApp
MaterialApp(
title: ‘Flutter IntlApp’,
localizationsDelegates: [
AppLocalizationsDelegate(),
],
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 效果图
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’,
localizationsDelegates: [
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale(‘zh’),
const Locale(‘en’),
],
home: _HomePage(),
)
3.4 效果图
4.1 将MaterialApp中title替换
MaterialApp(
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/48c731a2e88a50a48a493daeaed9d76b.jpeg)
最后
我一直以来都有整理练习大厂面试题的习惯,有随时跳出舒服圈的准备,也许求职者已经很满意现在的工作,薪酬,觉得习惯而且安逸。
不过如果公司突然倒闭,或者部门被裁减,还能找到这样或者更好的工作吗?
我建议各位,多刷刷面试题,知道最新的技术,每三个月可以去面试一两家公司,因为你已经有不错的工作了,所以可以带着轻松的心态去面试,同时也可以增加面试的经验。
我可以将最近整理的一线互联网公司面试真题+解析分享给大家,大概花了三个月的时间整理2246页,帮助大家学习进步。
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是部分内容截图:
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
,觉得习惯而且安逸。
不过如果公司突然倒闭,或者部门被裁减,还能找到这样或者更好的工作吗?
我建议各位,多刷刷面试题,知道最新的技术,每三个月可以去面试一两家公司,因为你已经有不错的工作了,所以可以带着轻松的心态去面试,同时也可以增加面试的经验。
我可以将最近整理的一线互联网公司面试真题+解析分享给大家,大概花了三个月的时间整理2246页,帮助大家学习进步。
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是部分内容截图:
[外链图片转存中…(img-ZkHlFd6w-1712270329952)]
[外链图片转存中…(img-XaK7SemI-1712270329953)]