未找到方向性小部件。 InkWell 小部件需要一个方向性小部件祖先

问题描述 投票:0回答:3

我对 flutter 完全陌生,我的第一步已经导致了错误,我无法自己解决或搜索这个网站..

我想创建一个按钮,但据我了解,得到了缺少方向性的异常。唯一需要 textDirection 的小部件是 Text Widget,但它有 ist,那么这里有什么问题呢?全面启动也已经尝试过多次,包括。新模拟器..

这是我的代码:

import 'package:flutter/material.dart';

void main(){
  runApp(
    new MyStatefullWidget()
  ) ;
}


class MyStatefullWidget extends StatefulWidget {

  @override
  _MyStatefullWidgetState createState() => _MyStatefullWidgetState();
}

class _MyStatefullWidgetState extends State<MyStatefullWidget> {

  @override
  Widget build(BuildContext context) {
    return Container(
      color:Colors.red,
      child: Center(
        child: RaisedButton(
          child: Text("klick mich", textDirection: TextDirection.ltr),
          onPressed: (){
            print("Hi");
          },
        ),
      ),
    );
  }
}

这是错误消息:

I/flutter ( 4763): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 4763): The following assertion was thrown building InkWell(gestures: [tap], clipped to BoxShape.rectangle,
I/flutter ( 4763): dirty, state: _InkResponseState<InkResponse>#43345):
I/flutter ( 4763): No Directionality widget found.
I/flutter ( 4763): InkWell widgets require a Directionality widget ancestor.
I/flutter ( 4763): The specific widget that could not find a Directionality ancestor was:
I/flutter ( 4763):   InkWell(gestures: [tap], clipped to BoxShape.rectangle)
I/flutter ( 4763): The ownership chain for the affected widget is:
I/flutter ( 4763):   InkWell ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#64251 ink
I/flutter ( 4763):   renderer] ← NotificationListener<LayoutChangedNotification> ← CustomPaint ← _ShapeBorderPaint ←
I/flutter ( 4763):   PhysicalShape ← _MaterialInterior ← Material ← ⋯
I/flutter ( 4763): Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the
I/flutter ( 4763): top of your application widget tree. It determines the ambient reading direction and is used, for
I/flutter ( 4763): example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve
I/flutter ( 4763): EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.
I/flutter ( 4763): 
I/flutter ( 4763): When the exception was thrown, this was the stack:
I/flutter ( 4763): #0      debugCheckHasDirectionality.<anonymous closure> (package:flutter/src/widgets/debug.dart:241:7)
I/flutter ( 4763): #1      debugCheckHasDirectionality (package:flutter/src/widgets/debug.dart:257:4)
I/flutter ( 4763): #2      InkResponse.debugCheckContext (package:flutter/src/material/ink_well.dart:376:12)
I/flutter ( 4763): #3      _InkResponseState.build (package:flutter/src/material/ink_well.dart:554:19)
I/flutter ( 4763): #4      StatefulElement.build (package:flutter/src/widgets/framework.dart:3809:27)
I/flutter ( 4763): #5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3721:15)
I/flutter ( 4763): #6      Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #8      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #9      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #10     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #11     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #12     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #13     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #14     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #15     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #16     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #17     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #18     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #19     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #20     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #21     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #22     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #25     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #28     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #29     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #30     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #31     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #32     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #33     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #34     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #35     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #36     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #37     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #38     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #39     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #40     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #41     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #42     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #43     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #44     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #45     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #46     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #47     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #48     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #49     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #50     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #51     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #52     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #53     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #54     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #55     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #56     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #57     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #58     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #59     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #60     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #61     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #62     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #63     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #64     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #65     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #66     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #67     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #68     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #69     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #70     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #71     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #72     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #73     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #74     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #75     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #76     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #77     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #78     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #79     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #80     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #81     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #82     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #83     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #84     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #85     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
I/flutter ( 4763): #86     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #87     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #88     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #89     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #90     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #91     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #92     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #93     Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #94     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
I/flutter ( 4763): #95     Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
I/flutter ( 4763): #96     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
I/flutter ( 4763): #97     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
I/flutter ( 4763): #98     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
I/flutter ( 4763): #99     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
I/flutter ( 4763): #100    Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
I/flutter ( 4763): #101    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:909:16)
I/flutter ( 4763): #102    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:880:5)
I/flutter ( 4763): #103    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:826:17)
I/flutter ( 4763): #104    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2266:19)
I/flutter ( 4763): #105    RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:825:13)
I/flutter ( 4763): #106    _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:712:7)
I/flutter ( 4763): #107    runApp (package:flutter/src/widgets/binding.dart:756:7)
I/flutter ( 4763): #108    main (package:flutter_app/main.dart:4:3)
I/flutter ( 4763): #109    _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:289:19)
I/flutter ( 4763): #110    _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter ( 4763): ════════════════════════════════════════════════════════════════════════════════════════════════════
dart flutter
3个回答
16
投票

你必须在容器之前添加一个

MaterialApp
小部件,如下所示:

class _MyStatefullWidgetState extends State<MyStatefullWidget> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Container(
        color: Colors.red,
        child: Center(
          child: RaisedButton(
            child: Text("klick mich", textDirection: TextDirection.ltr),
            onPressed: () {
              print("Hi");
            },
          ),
        ),
      ),
    );
  }
}

0
投票

我有同样的错误并添加更多信息,能够通过查看错误消息来解决,但不足的是我们可以在小部件树的顶部使用

MaterialApp
WidgetsApp
(如上所示) ):

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════

构建 InkWell 时抛出以下断言(手势:[tap],剪裁为 BoxShape.rectangle, 脏,状态:_InkResponseState#1b61e):

未找到方向性小部件。

InkWell 小部件需要方向性小部件祖先。

找不到方向性祖先的特定小部件是: 墨水井

受影响的小部件的所有权链是:“InkWell ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#3bd76 墨迹渲染器] ← 通知监听器 ← 自定义绘制 ← _ShapeBorderPaint ← 物理形状 ← _MaterialInterior ← 材质 ← ⋯"

通常,方向性小部件是由 MaterialApp 或 WidgetsApp 小部件在 您的应用程序小部件树的顶部。它确定环境阅读方向并用于 例如,确定如何布局文本、如何解释“开始”和“结束”值,以及解决 EdgeInsetsDirectional、AlignmentDirectional 和其他 *Directional 对象。


0
投票

2024 年的答案

对于那些未在 Flutter 应用程序中使用

Material
设计并遇到“未找到方向性小部件”错误的人:

作为修复,您可以在小部件树的顶层使用

Directionality
小部件包装您的根小部件。

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.ltr, // Set to ltr or rtl based on your needs
      child: MyNonMaterialWidget(),
    );
  }
}

class MyNonMaterialWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const Center(
      child: Text("Foo bar"),
    );
  }
}

#NonMaterialApps #Flutter

© www.soinside.com 2019 - 2024. All rights reserved.