0

请查看此视频

在这里,我扩展 TextEditingController 以突出显示(也可点击)关键字“ali”。每当用户键入“ali”时,它都可以正常工作。但是在将文本初始化到控制器时,它显示了很多错误。有人请帮助我解决此错误。

注意:当我初始化文本并从“textspan”中删除“recognizer”时,它运行良好,没有显示任何错误。我猜这个问题是由于文本跨度中的“识别器”而发生的。

这是我的代码,请取消注释第 15 行以查看错误。

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:kyubook/utils/utility.dart';

class TexspanTap extends StatefulWidget {
  @override
  _TexspanTapState createState() => _TexspanTapState();
}

class _TexspanTapState extends State<TexspanTap> {
  final MyTextEditingcontroller _editingcontroller = MyTextEditingcontroller();

  @override
  void initState() {
    //_editingcontroller.text = "dghali";
    super.initState();
  }

  @override
  void dispose() {
    _editingcontroller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: SafeArea(
        child: Column(
          children: [
            TextField(
              controller: _editingcontroller,
            ),
          ],
        ),
      ),
    );
  }
}

class MyTextEditingcontroller extends TextEditingController {
  @override
  TextSpan buildTextSpan(
      {required BuildContext context,
      TextStyle? style,
      required bool withComposing}) {
    if (text.toLowerCase().contains("ali")) {
      final List<InlineSpan> spans = [];

      final int indexOfAli = text.indexOf("ali");

      spans.add(TextSpan(text: text.substring(0, indexOfAli)));

      spans.add(
        TextSpan(
            text: text.substring(indexOfAli, indexOfAli + 3),
            style: const TextStyle(color: Colors.red),
            recognizer: TapGestureRecognizer()
              ..onTapDown = (final details) {
                commonToast("Touched");
              }),
      );

      spans.add(TextSpan(text: text.substring(indexOfAli + 3, text.length)));

      return TextSpan(children: spans, style: style);
    }

    return TextSpan(text: text, style: style);
  }
}

日志

I/flutter (26328): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (26328): The following assertion was thrown building
I/flutter (26328): RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#87b14](state:
I/flutter (26328): RawGestureDetectorState#ede1a(gestures: <none>, excludeFromSemantics: true, behavior: opaque)):
I/flutter (26328): 'package:flutter/src/rendering/editable.dart': Failed assertion: line 2376 pos 14: 'readOnly &&
I/flutter (26328): !obscureText': is not true.
I/flutter (26328): 
I/flutter (26328): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (26328): more information in this error message to help you determine and fix the underlying cause.
I/flutter (26328): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (26328):   https://github.com/flutter/flutter/issues/new?template=2_bug.md
I/flutter (26328): 
I/flutter (26328): The relevant error-causing widget was:
I/flutter (26328):   TextField
I/flutter (26328):   file:///Users/ali/Documents/Flutter/Xcelpros/kb_main_clientside/lib/components/profanity_checker/screen/textspan_tap.dart:32:13
I/flutter (26328): 
I/flutter (26328): When the exception was thrown, this was the stack:
I/flutter (26328): #2      RenderEditable.describeSemanticsConfiguration (package:flutter/src/rendering/editable.dart:2376:14)
I/flutter (26328): #3      RenderObject._semanticsConfiguration (package:flutter/src/rendering/object.dart:2525:7)
I/flutter (26328): #4      RenderObject.attach (package:flutter/src/rendering/object.dart:1416:34)
I/flutter (26328): #5      RelayoutWhenSystemFontsChangeMixin.attach (package:flutter/src/rendering/object.dart:3401:11)
I/flutter (26328): #6      RenderEditable.attach (package:flutter/src/rendering/editable.dart:2612:11)
I/flutter (26328): #7      AbstractNode.adoptChild (package:flutter/src/foundation/node.dart:138:13)
I/flutter (26328): #8      RenderObject.adoptChild (package:flutter/src/rendering/object.dart:1274:11)
I/flutter (26328): #9      RenderObjectWithChildMixin.child= (package:flutter/src/rendering/object.dart:3022:7)
I/flutter (26328): #10     SingleChildRenderObjectElement.insertRenderObjectChild (package:flutter/src/widgets/framework.dart:6098:18)
I/flutter (26328): #11     RenderObjectElement.attachRenderObject (package:flutter/src/widgets/framework.dart:5758:35)
I/flutter (26328): #12     RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5440:5)
I/flutter (26328): ...     Normal element mounting (92 frames)
I/flutter (26328): #104    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
I/flutter (26328): #105    Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18)
I/flutter (26328): #106    _DecorationElement._mountChild (package:flutter/src/material/input_decorator.dart:1591:31)
I/flutter (26328): #107    _DecorationElement.mount (package:flutter/src/material/input_decorator.dart:1604:5)
I/flutter (26328): ...     Normal element mounting (69 frames)
I/flutter (26328): #176    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
I/flutter (26328): #177    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
I/flutter (26328): #178    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
I/flutter (26328): ...     Normal element mounting (34 frames)
I/flutter (26328): #212    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
I/flutter (26328): #213    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
I/flutter (26328): #214    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6232:32)
I/flutter (26328): ...     Normal element mounting (238 frames)
I/flutter (26328): #452    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3611:14)
I/flutter (26328): #453    MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6221:36)
I/flutter (26328): #454    Element.updateChild (package:flutter/src/widgets/framework.dart:3363:18)
I/flutter (26328): #455    RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5654:32)
I/flutter (26328): #456    MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6243:17)
I/flutter (26328): #457    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #458    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #459    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
I/flutter (26328): #460    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #461    StatefulElement.update (package:flutter/src/widgets/framework.dart:4778:5)
I/flutter (26328): #462    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #463    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #464    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #465    ProxyElement.update (package:flutter/src/widgets/framework.dart:4922:5)
I/flutter (26328): #466    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #467    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #468    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #469    ProxyElement.update (package:flutter/src/widgets/framework.dart:4922:5)
I/flutter (26328): #470    _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:181:11)
I/flutter (26328): #471    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #472    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6090:14)
I/flutter (26328): #473    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #474    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #475    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
I/flutter (26328): #476    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #477    StatefulElement.update (package:flutter/src/widgets/framework.dart:4778:5)
I/flutter (26328): #478    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #479    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6090:14)
I/flutter (26328): #480    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #481    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6090:14)
I/flutter (26328): #482    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #483    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #484    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #485    ProxyElement.update (package:flutter/src/widgets/framework.dart:4922:5)
I/flutter (26328): #486    Element.updateChild (package:flutter/src/widgets/framework.dart:3350:15)
I/flutter (26328): #487    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4599:16)
I/flutter (26328): #488    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
I/flutter (26328): #489    Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5)
I/flutter (26328): #490    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2582:33)
I/flutter (26328): #491    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:875:21)
I/flutter (26328): #492    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:328:5)
I/flutter (26328): #493    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
I/flutter (26328): #494    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)
I/flutter (26328): #495    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:998:5)
I/flutter (26328): #499    _invoke (dart:ui/hooks.dart:163:10)
I/flutter (26328): #500    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:259:5)
I/flutter (26328): #501    _drawFrame (dart:ui/hooks.dart:126:31)
I/flutter (26328): (elided 5 frames from class _AssertionError and dart:async)
I/flutter (26328): 
I/flutter (26328): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (26328): Another exception was thrown: LateInitializationError: Field '_tap@798245603' has not been initialized.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 2 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 2 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 1 line
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 3 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 2 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 1 line
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/foundation/node.dart': Failed assertion: line 112 pos 12: '_owner != null': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4269 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
I/flutter (26328): Another exception was thrown: A GlobalKey was used multiple times inside one widget's child list.
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
I/chatty  (26328): uid=10089(com.kyubook.kyubookapp) 1.ui identical 18 lines
I/flutter (26328): Another exception was thrown: 'package:flutter/src/rendering/object.dart': Failed assertion: line 1285 pos 12: 'child.parentData != null': is not true.
4

1 回答 1

0

带有识别器的 TextSpan 中的 TextSpan 仅适用于 RichText 小部件。 看这里

所以简而言之,您的 TextField 中需要 RichText。但目前 TextFields 不支持 RichText。你需要一些可以实现它的包。我找到了 rich_input

如果这可以帮助您,请尝试一下。

于 2022-01-28T14:50:28.670 回答