Im-Kevin / cool_ui

用flutter实现一些我认为好看的UI控件,有Popover,仿Weui的Toast,自定义键盘
Apache License 2.0
460 stars 74 forks source link

长按textField抛"No MaterialLocalizations found" #50

Closed peterlpt closed 4 years ago

peterlpt commented 4 years ago

按指引,Step2 注册键盘,并且添加了KeyboardRootWidget:

void main() {
  CxyCarNumberKeyboard.registerRegion();
  CxyCarNumberKeyboard.registerCharacter();
  CxyCarNumberKeyboard.registerNumber();

  runApp(KeyboardRootWidget(child: MyApp(),));
}

运行自定义键盘工作正常,但在TextField上长按就会抛出No MaterialLocalizations found异常,具体见下面详细异常信息:

dependencies:
  cool_ui: "^0.5.5"
error log detail ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞════════════════════════ The following assertion was thrown building _OverlayEntryWidget-[LabeledGlobalKey<_OverlayEntryWidgetState>#a3566](dirty, state: _OverlayEntryWidgetState#c8bf6): No MaterialLocalizations found. _OverlayEntryWidget widgets require MaterialLocalizations to be provided by a Localizations widget ancestor. Localizations are used to generate many different messages, labels, and abbreviations which are used by the material library. 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: _OverlayEntryWidget-[LabeledGlobalKey<_OverlayEntryWidgetState>#a3566] The ancestors of this widget were: _Theatre Overlay MediaQuery KeyboardMediaQuery Directionality KeyboardRootWidget [root] Widget creation tracking is currently disabled. Enabling it enables improved error messages. It can be enabled by passing `--track-widget-creation` to `flutter run` or `flutter test`.
flutter doctor -v [✓] Flutter (Channel beta, 1.18.0-11.1.pre, on Mac OS X 10.15.3 19D76, locale zh-Hans-CN) • Flutter version 1.18.0-11.1.pre at /Users/peter/Data/flutter • Framework revision 2738a1148b (13 days ago), 2020-05-13 15:24:36 -0700 • Engine revision ef9215ceb2 • Dart version 2.9.0 (build 2.9.0-8.2.beta) [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2) • Android SDK at /Users/peter/Data/Android/sdk • Platform android-29, build-tools 29.0.2 • ANDROID_HOME = /Users/peter/Data/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 11.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 11.3, Build version 11C29 • CocoaPods version 1.8.4 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 3.6) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin version 44.0.2 • Dart plugin version 192.7761 • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

查阅相关资料,说是main方法中runApp的顶级widget必须是返回继承自 StatelessWidgetMaterialApp,但我按这样调整为如下代码:

void main() {
  CxyCarNumberKeyboard.registerRegion();
  CxyCarNumberKeyboard.registerCharacter();
  CxyCarNumberKeyboard.registerNumber();

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: KeyboardRootWidget(child: MyAppContent(),),
    );
  }
}

结果长按不会出错了,但Android测试自定义键盘是正常的,iOS却经常在TextField获取到焦点时无法弹出自定义键盘。

Im-Kevin commented 4 years ago

升级版本0.6.0,该问题已经修复了

Im-Kevin commented 4 years ago

43

peterlpt commented 4 years ago

升级版本0.6.0,该问题已经修复了

@Im-Kevin 谢谢及时回复,留意到升级后 keyboard_manager.dart#init的参数增加了一个,这里相较于0.5.5版本的使用注意或指引,我可以在哪里可以看到相关的资料呢?

Im-Kevin commented 4 years ago

目前是不需要执行init方法了,用KeyboardRootWidget 包含MaterialApp即可

peterlpt commented 4 years ago

目前是不需要执行init方法了,用KeyboardRootWidget 包含MaterialApp即可

收到,已测试,目前看功能正常,谢谢。