Flutter版本1.7.0,长按textField时,触发文本复制或者选择功能时报错,具体报错信息大概如下

flutter: The following NoSuchMethodError was thrown building
flutter: _OverlayEntry-[LabeledGlobalKey<_OverlayEntryState>#fad24](dirty, dependencies: [MediaQuery,
flutter: _LocalizationsScope-[GlobalKey#587a3]], state: _OverlayEntryState#b41c8):
flutter: The getter 'cutButtonLabel' was called on null.
flutter: Receiver: null
flutter: Tried calling: cutButtonLabel
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
flutter: #1      _CupertinoTextSelectionControls.buildToolbar (package:flutter/src/cupertino/text_selection.dart:358:44)
flutter: #2      TextSelectionOverlay._buildToolbar (package:flutter/src/widgets/text_selection.dart:545:34)
flutter: #3      _OverlayEntryState.build (package:flutter/src/widgets/overlay.dart:170:25)
flutter: #4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4012:27)
flutter: #5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3924:15)
flutter: #6      Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
flutter: #7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
flutter: #8      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11)

看样子和本地化有关,相关解决办法如下,自定义一个本地化代理并添加

class FallbackCupertinoLocalisationsDelegate
    extends LocalizationsDelegate<CupertinoLocalizations> {
  const FallbackCupertinoLocalisationsDelegate();

  @override
  bool isSupported(Locale locale) => true;

  @override
  Future<CupertinoLocalizations> load(Locale locale) =>
      DefaultCupertinoLocalizations.load(locale);

  @override
  bool shouldReload(FallbackCupertinoLocalisationsDelegate old) => false;
}

最终app入口大概如下

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,//配置debug图标
      initialRoute: '/',     //初始化的时候加载的路由
      onGenerateRoute: onGenerateRoute,
      localizationsDelegates: [
        // ... app-specific localization delegate[s] here
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        const FallbackCupertinoLocalisationsDelegate(),
      ],
      supportedLocales: [                                   //此处
        const Locale('zh','CH'),
        const Locale('en','US'),
      ],
    );
  }


}

标签: flutter, textField, cutButtonLabel, error

添加新评论