No MaterialLocalizations found. AppBar widgets require MaterialLocalizations to be provided by a Loc

解决Flutter应用中因自定义多语言简写不匹配系统支持列表导致的NoMaterialLocalizationsFound错误。文章详细介绍了如何正确配置多语言文件,确保与fluttersdk系统多语言简写一致,避免本地化问题。

No MaterialLocalizations found.

AppBar widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.

错误很明显,抓住重点(红色的位置)。意思是什么呢?是说系统的本地化多语言找不到对应的多语言缩写。

如果你的应用刚好也是有多语言适配,那么问题就出在多语言简写的问题上。

找到flutter sdk系统对应的多语言简写文件:generated_material_localizations.dart,你会发现多语言简写的列表:

注意,不同sdk版本的这个多语言肯定是不一样的,因为sdk在不断完善中。新版本所支持的多语言要更丰富。

所以,系统既然只支持这么多明文的简写,那么我们如果自己命名的简写不符合这个列表,那么问题就产生了。

所以我们的*.arb文件(en.arb,zh.arb,nb.arb...)的命名要严格遵守这个规格。

 

举个例子,我自己的项目就是碰到这个问题,自己首先没有参考任何文档,将挪威语(Norwegian)对应简写成no.arb,或者nn.arb,结果查了1天,搞到晚上才明白这个问题出在系统多语言支持上。。。,后来改成nb.arb,问题解决

 

### AppBarLayout 使用指南 AppBarLayout 是 Material Components 提供的一个布局,用来在屏幕顶部构建应用栏,通常与 CoordinatorLayout、Toolbar、NestedScrollView 等搭配使用,能够为应用提供滚动交互以及收缩和展开效果 [^3]。以下是使用步骤和示例代码: #### 1. 添加依赖 确保在项目的 `build.gradle` 文件中添加 Material Design 库的依赖: ```groovy implementation 'com.google.android.material:material:最新版本号' ``` #### 2. 布局文件中使用 AppBarLayout 通常将 AppBarLayout 放在 CoordinatorLayout 内部,示例如下: ```xml <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:theme="@style/Theme.MaterialComponents.NoActionBar"> <com.google.android.material.appbar.MaterialToolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:title="App Title" /> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <!-- 这里放置滚动内容 --> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` #### 3. 设置滚动标志 AppBarLayout 的子视图可以通过 `app:layout_scrollFlags` 属性设置滚动行为,常见的标志有: - `scroll`:子视图会随着滚动内容一起滚动。 - `enterAlways`:子视图在滚动内容向下滚动时立即显示。 - `enterAlwaysCollapsed`:子视图在滚动内容向下滚动时,先以最小高度显示,滚动到顶部时再展开。 - `exitUntilCollapsed`:子视图在滚动内容向上滚动时,先收缩到最小高度,直到滚动到顶部才完全隐藏。 示例: ```xml <com.google.android.material.appbar.MaterialToolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" /> ``` ### 常见问题解决方案 #### 1. 布局膨胀错误(Error inflating class com.google.android.material.appbar.AppBarLayout) 解决方案:确保布局文件中 AppBarLayout 的属性设置正确,例如: ```xml <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:theme="@style/Theme.MaterialComponents.NoActionBar"> <!-- 子视图 --> </com.google.android.material.appbar.AppBarLayout> ``` 同时,检查项目的依赖是否正确添加,并且版本兼容 [^1]。 #### 2. AppBarLayout 与滚动内容联系问题 要使 AppBarLayout 与滚动内容(如 NestedScrollView)进行关联,需要在滚动内容视图上设置 `app:layout_behavior="@string/appbar_scrolling_view_behavior"` 属性 [^3]。 #### 3. 子视图滚动标志不生效 确保子视图的 `app:layout_scrollFlags` 属性设置正确,并且 AppBarLayout 的父布局是 CoordinatorLayout。同时,检查滚动内容视图是否支持嵌套滚动(如使用 NestedScrollView)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值