Flutter开发iOS上TextField长按或反复点击报错No CupertinoLocalizations found.
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter开发iOS上TextField长按或反复点击报错No CupertinoLocalizations found.相关的知识,希望对你有一定的参考价值。
参考技术A 错误内容如下:======== Exception caught by widgets library =======================================================
The following assertion was thrown building _CupertinoTextSelectionControlsToolbar(dirty, dependencies: [MediaQuery, _LocalizationsScope-[GlobalKey#1fa5b]], state: _CupertinoTextSelectionControlsToolbarState#23a43):
No CupertinoLocalizations found.
_CupertinoTextSelectionControlsToolbar widgets require CupertinoLocalizations to be provided by a Localizations widget ancestor.
The cupertino library uses Localizations to generate messages, labels, and abbreviations.
To introduce a CupertinoLocalizations, either use a CupertinoApp at the root of your application to include them automatically, or add a Localization widget with a CupertinoLocalizations delegate.
The specific widget that could not find a CupertinoLocalizations ancestor was: _CupertinoTextSelectionControlsToolbar
dirty
dependencies: [MediaQuery, _LocalizationsScope-[GlobalKey#1fa5b]]
state: _CupertinoTextSelectionControlsToolbarState#23a43
The ancestors of this widget were:
: CupertinoApp
state: _CupertinoAppState#5e387
: MyApp
: ChangeNotifierProvider<KitUser>
value: <not yet loaded>
: MultiProvider
...
The relevant error-causing widget was:
CupertinoApp CupertinoApp:file:///Users/jarvis/Desktop/Flutter-workspace/flutter_oa/code/oa/lib/main.dart:70:14
When the exception was thrown, this was the stack:
#0 debugCheckHasCupertinoLocalizations.<anonymous closure> (package:flutter/src/cupertino/debug.dart:23:7)
#1 debugCheckHasCupertinoLocalizations (package:flutter/src/cupertino/debug.dart:43:4)
#2 CupertinoLocalizations.of (package:flutter/src/cupertino/localizations.dart:262:12)
#3 _CupertinoTextSelectionControlsToolbarState.build (package:flutter/src/cupertino/text_selection.dart:135:73)
#4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4782:27)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4665:15)
#6 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
#7 Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
#8 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4643:5)
#9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4831:11)
#10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4638:5)
... Normal element mounting (39 frames)
#49 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14)
#50 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6333:36)
#51 Element.updateChild (package:flutter/src/widgets/framework.dart:3425:18)
#52 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5758:32)
#53 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6356:17)
#54 Element.updateChild (package:flutter/src/widgets/framework.dart:3412:15)
#55 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4690:16)
#56 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4840:11)
#57 Element.rebuild (package:flutter/src/widgets/framework.dart:4355:5)
#58 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:33)
#59 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#60 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:319:5)
#61 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1143:15)
#62 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1080:9)
#63 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:996:5)
#67 _invoke (dart:ui/hooks.dart:166:10)
#68 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:270:5)
#69 _drawFrame (dart:ui/hooks.dart:129:31)
(elided 3 frames from dart:async)
====================================================================================================
从上面的错误信息中的一句:
To introduce a CupertinoLocalizations, either use a CupertinoApp at the root of your application to include them automatically, or add a Localization widget with a CupertinoLocalizations delegate.
大致的意思是需要引用‘CupertinoLocalizations’这么个东西,然后用’CupertinoApp‘这个东西包裹,那么‘CupertinoLocalizations’需要放在那里呢,哎,就是放在语言代理的‘localizationsDelegates’位置下,
当然,不能直接使用‘CupertinoLocalizations’,不然也是报错,这里需要使用的是‘GlobalCupertinoLocalizations.delegate,’,这个代理,我这里是区分了ios和android设备平台。
具体可参考下图:
flutter textfield 长按输入框出现剪切/复制/粘贴的菜单如何设置中文?
当 TextField 设置 enableInteractiveSelection 属性后长按会出现菜单,默认为英文,可通过设置 Flutter 国际化来处理;
- 在 pubspec.yaml 中集成 flutter_localizations;
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
- 在 MaterialApp 中设置本地化代理和支持的语言类型;
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale(‘zh‘, ‘CN‘),
const Locale(‘en‘, ‘US‘),
]
}
以上是关于Flutter开发iOS上TextField长按或反复点击报错No CupertinoLocalizations found.的主要内容,如果未能解决你的问题,请参考以下文章
flutter textfield 长按输入框出现剪切/复制/粘贴的菜单如何设置中文?
如何在 Flutter 测试中模拟 TextField 上的按 ENTER
长按键盘时 TextField 显示 -[UIWindow endDisablingInterfaceAutorotationAnimated:] 错误 (SwiftUI)