[Flutter]Another exception was thrown: No MaterialLocalizations found.
freddon
发表于2019-08-11
阅读 1531 |
评论 0
当在页面中点击按钮调用showModalBottomSheet报错
底部报错如下:
```
flutter:
flutter: Handler: "onTap"
flutter: Recognizer:
flutter: TapGestureRecognizer#59e95
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: No MaterialLocalizations found.
```
即:__`未配置国际化`__
本例现象:
App支持Android和IOS两种布局,android使用MateriaApp,ios使用CupertinoApp。内部的控件也尽量跟随系统。调用showModalBottomSheet是同一份代码:
```
showModalBottomSheet(
context: context,
builder: (BuildContext context) =>
Scaffold(body:
Stack(children:
<Widget>[
Placeholder(),
Placeholder()]
)));
```
__重现步骤:__
当使用android环境(即MateriaApp风格),showModalBottomSheet正常;使用ios环境报错如上。
检查入口main.dart文件,
```
void main() => runApp(Application());
class Application extends StatelessWidget {
// This widget is the root of your application.
//ios 主页
Widget iosXApp(BuildContext context) => Theme(
data: ThemeData(
fontFamily: '.SF Pro Text',
).copyWith(canvasColor: Colors.transparent),
child: CupertinoApp(
//公共代码略无相关代码略
},
));
Widget androidXApp(BuildContext context) => MaterialApp(
//公共代码略无相关代码略
localizationsDelegates: [
const TranslationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale("en", "US"),
const Locale("zh", "CN"),
],
//略
);
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return new AppENV().isIOSPlatform ? iosXApp(context) : androidXApp(context);
}
}
```
仔细对比发现,果然在ios的脚手架中__`未配置国际化`__:
```
localizationsDelegates: [
const TranslationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale("en", "US"),
const Locale("zh", "CN"),
],
```
同等加上配置即可。
以上。
分类 :日常记录