【问题标题】:Issues with Flutter's MaterialLocalizationsFlutter 的 MaterialLocalizations 问题
【发布时间】:2021-03-27 08:47:47
【问题描述】:

我的 Material 小部件(以 AppBar 小部件为例)有问题。我试图在其他 SO 问题中找到解决方案,但没有任何效果。在生产应用栏的后退按钮,以及其他一些材质文件也不起作用,甚至是材质剪贴板。

[编辑] 我发现问题出在某些非全球(中文、印度教)手机中不支持的语言(土库曼语/俄语 - 我的应用程序的两种主要语言)。其他 iOS / Android 设备没有问题。

于是有了我的 MaterialApp 文件:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:peydaly.market/blocs/markets_provider.dart';
import 'package:peydaly.market/generated/l10n.dart';
import 'package:peydaly.market/screens/loading_screen.dart';

import 'blocs/markets_provider.dart';

class App extends StatelessWidget {
  @override
  Widget build(context) {
    return MarketsProvider(
      child: MaterialApp(
        localizationsDelegates: [
          S.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        supportedLocales: S.delegate.supportedLocales,
        debugShowCheckedModeBanner: false,
        darkTheme: buildDarkMode(),
        theme: buildLightMode(),
        title: 'Peýdaly.Market',
        home: LoadingScreen(),
      ),
    );
  }

  ThemeData buildLightMode() {
    return ThemeData.light().copyWith(
      buttonTheme: ButtonThemeData(
        minWidth: 10,
      ),
      primaryColor: Colors.grey[100],
      backgroundColor: Colors.white,
      iconTheme: IconThemeData(
        color: Colors.grey[900],
      ),
      appBarTheme: AppBarTheme(
        centerTitle: true,
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: Colors.grey[900],
        ),
        actionsIconTheme: IconThemeData(
          color: Colors.grey[900],
        ),
        // brightness: Brightness.light,
        color: Colors.white,
        textTheme: TextTheme(
          headline6: TextStyle(
            color: Colors.grey[900],
            fontWeight: FontWeight.w600,
          ),
        ),
        elevation: 0,
      ),
    );
  }

  ThemeData buildDarkMode() {
    return ThemeData.dark().copyWith(
      buttonTheme: ButtonThemeData(
        minWidth: 10,
      ),
      backgroundColor: Colors.grey[900],
      primaryColor: Colors.grey[850],
      iconTheme: IconThemeData(
        color: Colors.white,
      ),
      appBarTheme: AppBarTheme(
        centerTitle: true,
        brightness: Brightness.dark,
        iconTheme: IconThemeData(
          color: Colors.white,
        ),
        actionsIconTheme: IconThemeData(
          color: Colors.white,
        ),
        // brightness: Brightness.dark,
        color: Colors.grey[900],
        textTheme: TextTheme(
          headline6: TextStyle(
            color: Colors.grey[100],
            fontWeight: FontWeight.w600,
          ),
        ),
        elevation: 0,
      ),
    );
  }
}

我的 pubspec.yaml 文件:

name: peydaly.market
description: Subscription Online Groceries delivery service app for mobile devices.
version: 0.2+23

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  animations: ^1.1.2
  cached_network_image: ^2.2.0+1
  carousel_slider: ^2.3.1
  crypto: ^2.1.5
  cupertino_icons: ^1.0.0
  cupertino_rounded_corners: ^1.0.3
  date_format: ^1.0.9
  flare_flutter: ^2.0.6
  flutter_polyline_points: ^0.2.4
  futuristic: ^0.1.3
  google_maps_flutter: ^1.0.6
  http: ^0.12.1
  intl: 0.16.1
  location: ^3.0.2
  modal_bottom_sheet: ^0.2.2
  path_provider: ^1.6.14
  rive: ^0.6.3
  rxdart: ^0.24.1
  shimmer: ^1.1.1
  splashscreen: ^1.3.4
  sqflite: ^1.3.1+1
  superellipse_shape: ^0.1.5

dev_dependencies:
  flutter_test:
    sdk: flutter
  pedantic: ^1.4.0

flutter:
  uses-material-design: true
  generate: true
  assets:
    - assets/social/
    - assets/playlists/
    - assets/avatar/
    - assets/sql/
flutter_localizations:
    sdk: flutter
flutter_intl:
  enabled: true
  class_name: S
  main_locale: tm

flutter_icons:
  android: "launcher_icon"
  ios: true
  image_path: "assets/icon/icon.png"

还有我的调试控制台错误。

═══════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building AppBar(dirty, dependencies: [_LocalizationsScope-[GlobalKey#2fd79]], state: _AppBarState#848b3):
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.

The specific widget that could not find a MaterialLocalizations ancestor was: AppBar
    dirty
    dependencies: [_LocalizationsScope-[GlobalKey#2fd79]]
    state: _AppBarState#848b3
The ancestors of this widget were
    Scaffold
        dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2fd79], Directionality, MediaQuery, _EffectiveTickerMode]
        state: ScaffoldState#0f235(tickers: tracking 2 tickers)
    CitySelectionScreen
        dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2fd79], MarketsProvider]
        state: _CitySelectionScreenState#fb711
    MaterialApp
        state: _MaterialAppState#44c02
    MarketsProvider
    App
    ...
The relevant error-causing widget was
AppBar
lib/screens/city_selection_screen.dart:53
When the exception was thrown, this was the stack
#0      debugCheckHasMaterialLocalizations.<anonymous closure>
package:flutter/…/material/debug.dart:74
#1      debugCheckHasMaterialLocalizations
package:flutter/…/material/debug.dart:94
#2      _AppBarState.build
package:flutter/…/material/app_bar.dart:509
#3      StatefulElement.build
package:flutter/…/widgets/framework.dart:4744
#4      ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4627
...
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════
No MaterialLocalizations found.
The ancestors of this widget were
    Scaffold
        dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2fd79], Directionality, MediaQuery, _EffectiveTickerMode]
        state: ScaffoldState#0f235(tickers: tracking 2 tickers)
    CitySelectionScreen
        dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2fd79], MarketsProvider]
        state: _CitySelectionScreenState#fb711
    MaterialApp
        state: _MaterialAppState#44c02
    MarketsProvider
    App
    ...
The relevant error-causing widget was
AppBar
lib/screens/city_selection_screen.dart:53
════════════════════════════════════════════════════════════════════════════════

【问题讨论】:

  • Widget build(Build context) // 添加 Build
  • @JohnJoe 你的意思是 App 类中的 build 方法?
  • 是的,试试看。
  • @JohnJoe 不,我发现这是因为某些非全球本地电话中不支持的语言(土库曼语)。但是,其他解决方案将不胜感激。

标签: flutter dart flutter-intl


【解决方案1】:

所以问题在于不受支持的设备上 Material UI 元素的 App 本地化问题,因此为了添加对它的支持,我将这些行添加到我的 App 文件中:

supportedLocales: [
  const Locale('ru'),
  const Locale('tm'),
  ...S.delegate.supportedLocales,
],

【讨论】:

    猜你喜欢
    • 2019-05-30
    • 2021-03-15
    • 2021-02-25
    • 2018-09-27
    • 1970-01-01
    • 2020-02-20
    • 2021-06-06
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多