如何将CupertinoApp包装在“本地化”小部件中,以便可以在其中使用“材质”小部件? [英] How do i wrap a CupertinoApp in a Localizations widget so i can use Material widgets within it?

查看:125
本文介绍了如何将CupertinoApp包装在“本地化”小部件中,以便可以在其中使用“材质”小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CupertinoApp小部件来包装我的应用程序,同时尝试在其中使用Material TextFormField小部件。

I am using a CupertinoApp widget to wrap my app, while trying to use a Material TextFormField widget within it.

例如,当我触发文本选择模式时(双击文本字段),出现以下错误(因为它试图打开需要材料本地化的覆盖小部件):

When I for example trigger the text selection mode (Double tap the text field), I am getting the following error (because it tries to open an overlay widget that requires material localizations):

未找到任何MaterialLocalizations。
OverlayEntry小部件要求MaterialLocalizations由Localizations小部件的祖先提供。

No MaterialLocalizations found. OverlayEntry widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.

Localizations用于生成许多不同的消息,标签和缩写,材料库。

Localizations are used to generate many different messages, labels,and abbreviations which are used by the material library.

要引入MaterialLocalizations,可以在应用程序的根目录使用MaterialApp来:自动包括它们,或者使用MaterialLocalizations委托添加Localization小部件。

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.

问题是我无法使用MaterialApp,因为我需要使用Cupertino样式导航(CupertinoTabBar / CupertinoTabView内部的嵌套导航器),所以我需要使用本地化窗口小部件方法。

The problem is that I cannot use a MaterialApp as I need to use Cupertino style navigation (nested navigators inside CupertinoTabBar/CupertinoTabView), so i need to use the Localization widget approach.

但是以下代码不能解决问题:

However the following code does not solve the problem:

void main() {
  runApp(
    Localizations(
      child: CupertinoApp(
        debugShowCheckedModeBanner: false,
        home: App(), // <-- Main Screen, a Cupertino Tab Scaffold with 3 CupertinoTabViews
      ),
      locale: Locale('en', 'US'),
      delegates: <LocalizationsDelegate>[

        // I assumed these were sufficient, but the runtime exception still persist.
        DefaultMaterialLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
      ],
    ),
  );
}

我的问题是,这样做的正确方法是什么无法在CupertinoApp中使用材料小部件?

My question is then, what is the proper way to do this, is it not possible to use a Material widget within a CupertinoApp?

I/flutter ( 7631): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 7631): The following assertion was thrown building
I/flutter ( 7631): _OverlayEntry-[LabeledGlobalKey<_OverlayEntryState>#1e5c6](dirty, dependencies:
I/flutter ( 7631): [_LocalizationsScope-[GlobalKey#d1cfa]], state: _OverlayEntryState#ba2a3):
I/flutter ( 7631): No MaterialLocalizations found.
I/flutter ( 7631): _OverlayEntry widgets require MaterialLocalizations to be provided by a Localizations widget
I/flutter ( 7631): ancestor.
I/flutter ( 7631): Localizations are used to generate many different messages, labels,and abbreviations which are used
I/flutter ( 7631): by the material library. 
I/flutter ( 7631): To introduce a MaterialLocalizations, either use a  MaterialApp at the root of your application to
I/flutter ( 7631): include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
I/flutter ( 7631): The specific widget that could not find a MaterialLocalizations ancestor was:
I/flutter ( 7631):   _OverlayEntry-[LabeledGlobalKey<_OverlayEntryState>#1e5c6]
I/flutter ( 7631): The ancestors of this widget were:
I/flutter ( 7631):   Stack(alignment: AlignmentDirectional.topStart, fit: expand, overflow: clip)
I/flutter ( 7631):   _Theatre
I/flutter ( 7631):   Overlay-[LabeledGlobalKey<OverlayState>#362c7]
I/flutter ( 7631):   _FocusScopeMarker
I/flutter ( 7631):   Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null,
I/flutter ( 7631):   hintOverrides: null)
I/flutter ( 7631):   FocusScope
I/flutter ( 7631):   AbsorbPointer(absorbing: false)
I/flutter ( 7631):   Listener(listeners: [down, up, cancel], behavior: deferToChild)
I/flutter ( 7631):   Navigator-[GlobalObjectKey<NavigatorState> _WidgetsAppState#2397a]
I/flutter ( 7631):   DefaultTextStyle(inherit: false, color: Color(0xff000000), family: .SF Pro Text, size: 17.0,
I/flutter ( 7631):   letterSpacing: -0.4, decoration: TextDecoration.none, softWrap: wrapping at box width, overflow:
I/flutter ( 7631):   clip)
I/flutter ( 7631):   Title(color: Color(0xff007aff))
I/flutter ( 7631):   Directionality(textDirection: ltr)
I/flutter ( 7631):   _LocalizationsScope-[GlobalKey#d1cfa]
I/flutter ( 7631):   Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null,
I/flutter ( 7631):   textDirection: ltr, hintOverrides: null)
I/flutter ( 7631):   Localizations(locale: en_US, delegates: [DefaultCupertinoLocalizations.delegate(en_US),
I/flutter ( 7631):   DefaultWidgetsLocalizations.delegate(en_US)])
I/flutter ( 7631):   MediaQuery(MediaQueryData(size: Size(411.4, 683.4), devicePixelRatio: 2.6, textScaleFactor: 1.0,
I/flutter ( 7631):   platformBrightness: Brightness.light, padding: EdgeInsets(0.0, 24.0, 0.0, 0.0), viewInsets:
I/flutter ( 7631):   EdgeInsets.zero, alwaysUse24HourFormat: false, accessibleNavigation: falsedisableAnimations:
I/flutter ( 7631):   falseinvertColors: falseboldText: false))
I/flutter ( 7631):   WidgetsApp-[GlobalObjectKey _CupertinoAppState#5df27]
I/flutter ( 7631):   IconTheme(IconThemeData#33904(color: Color(0xff007aff)))
I/flutter ( 7631):   _InheritedCupertinoTheme
I/flutter ( 7631):   CupertinoTheme
I/flutter ( 7631):   ScrollConfiguration(behavior: _AlwaysCupertinoScrollBehavior)
I/flutter ( 7631):   CupertinoApp
I/flutter ( 7631):   Directionality(textDirection: ltr)
I/flutter ( 7631):   _LocalizationsScope-[GlobalKey#dfb7d]
I/flutter ( 7631):   Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null,
I/flutter ( 7631):   textDirection: ltr, hintOverrides: null)
I/flutter ( 7631):   Localizations(locale: en_US, delegates: [DefaultMaterialLocalizations.delegate(en_US),
I/flutter ( 7631):   DefaultCupertinoLocalizations.delegate(en_US), DefaultWidgetsLocalizations.delegate(en_US)])
I/flutter ( 7631):   _InheritedModel<AppModel>
I/flutter ( 7631):   AnimatedBuilder(animation: Instance of 'AppModel')
I/flutter ( 7631):   ScopedModel<AppModel>
I/flutter ( 7631):   [root]
I/flutter ( 7631): 
I/flutter ( 7631): When the exception was thrown, this was the stack:
I/flutter ( 7631): #0      debugCheckHasMaterialLocalizations.<anonymous closure> (package:flutter/src/material/debug.dart:124:7)
I/flutter ( 7631): #1      debugCheckHasMaterialLocalizations (package:flutter/src/material/debug.dart:127:4)
I/flutter ( 7631): #2      _MaterialTextSelectionControls.buildToolbar (package:flutter/src/material/text_selection.dart:135:12)
// ....


推荐答案

您不应将 CupertinoApp 包装到本地化

CupertinoApp 包括插入自己的 Localization 小部件,因此您在 CupertinoApp 上方添加的内容都会被覆盖。

CupertinoApp includes insert its own Localization widget, so whatever you added above CupertinoApp just gets overridden.

相反,将您的代表直接传递到 CupertinoApp localizationsDelegates

Instead, pass your delegates directly to localizationsDelegates of CupertinoApp:

CupertinoApp(
  localizationsDelegates: [
    DefaultMaterialLocalizations.delegate,
    DefaultCupertinoLocalizations.delegate,
    DefaultWidgetsLocalizations.delegate,
  ],
),

这篇关于如何将CupertinoApp包装在“本地化”小部件中,以便可以在其中使用“材质”小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆