我正在尝试在flutter
中创建一个简单的布局,但不断收到此错误:
以下断言被抛出构建文本("更快地发送功能",textAlign:center): 找不到方向性小部件。
我认为问题可能出在TextDirection类属性中,但我一直无法找到它。
这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
void main(){
runApp( new MyHome());
}
class MyHome extends StatelessWidget{
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Material(
child: new Container(
color: Colors.red,
margin: const EdgeInsets.all(5.0),
child: new Row(
children: <Widget>[
new Expanded(
child: new Text('Deliver features faster', textAlign: TextAlign.center),
),
new Expanded(
child: new Text('Craft beautiful UIs', textAlign: TextAlign.center),
),
new Expanded(
child: new FittedBox(
fit: BoxFit.contain, // otherwise the logo will be tiny
child: const FlutterLogo(),
),
),
],
)
),
);
}
}
以下是错误的完整堆栈跟踪:
The following assertion was thrown building Text("Deliver features faster", textAlign: center):
No Directionality widget found.
RichText widgets require a Directionality widget ancestor.
The specific widget that could not find a Directionality ancestor was:
RichText(textAlign: center, softWrap: wrapping at box width, maxLines: unlimited, text: "Deliver
features faster")
The ownership chain for the affected widget is:
RichText ← Text ← Expanded ← Row ← DecoratedBox ← Padding ← Container ← DefaultTextStyle ←
AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#978b7 ink renderer] ← ⋯
Typically, the Directionality widget is introduced by the MaterialApp or WidgetsApp widget at the
top of your application widget tree. It determines the ambient reading direction and is used, for
example, to determine how to lay out text, how to interpret "start" and "end" values, and to resolve
EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.
When the exception was thrown, this was the stack:
#0 debugCheckHasDirectionality.<anonymous closure> (package:flutter/src/widgets/debug.dart:223:7)
#1 debugCheckHasDirectionality (package:flutter/src/widgets/debug.dart:239:4)
#2 RichText.createRenderObject (package:flutter/src/widgets/basic.dart:4245:37)
#3 RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4259:28)
#4 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#5 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#7 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#8 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#9 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#11 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#12 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#13 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#14 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#15 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#16 ParentDataElement.mount (package:flutter/src/widgets/framework.dart:3938:16)
#17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#18 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4738:32)
#19 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#20 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#21 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4633:14)
#22 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#23 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#24 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4633:14)
#25 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#26 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#27 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#28 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#29 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#30 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#31 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#32 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#33 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#34 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#35 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#36 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#37 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#38 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#39 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#40 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#41 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#42 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3752:22)
#43 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#44 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#45 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#46 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4633:14)
#47 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#48 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#49 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#50 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#51 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#52 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#53 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#54 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#55 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4633:14)
#56 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#57 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#58 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#59 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#60 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#61 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3752:22)
#62 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#63 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#64 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#65 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#66 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#67 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#68 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3752:22)
#69 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#70 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#71 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#72 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3636:16)
#73 Element.rebuild (package:flutter/src/widgets/framework.dart:3478:5)
#74 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3605:5)
#75 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#76 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#77 Element.updateChild (package:flutter/src/widgets/framework.dart:2693:12)
#78 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:852:16)
#79 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:823:5)
#80 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:769:17)
#81 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2205:19)
#82 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:768:13)
#83 BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&RendererBinding&WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:657:7)
#84 runApp (package:flutter/src/widgets/binding.dart:699:7)
#85 main (/data/user/0/com.yourcompany.flutterproject/cache/flutter_projectENWZDI/flutter_project/lib/main.dart:6:3)
#86 _startIsolate.<anonymous closure> (dart:isolate-patch/dart:isolate/isolate_patch.dart:279)
#87 _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)
════════════════════════════════════════════════════════════════════════════════════════════════════
Another exception was thrown: No Directionality widget found.
Another exception was thrown: Horizontal RenderFlex with multiple children has a null textDirection, so the layout order is undefined.
Another exception was thrown: 'package:flutter/src/rendering/box.dart': Failed assertion: line 1446 pos 12: 'hasSize': is not true.
uid=10079(com.yourcompany.flutterproject) Thread-5 identical 2 lines
Another exception was thrown: 'package:flutter/src/rendering/box.dart': Failed assertion: line 1446 pos 12: 'hasSize': is not true.
Another exception was thrown: 'package:flutter/src/rendering/proxy_box.dart': Failed assertion: line 1564 pos 12: 'hasSize': is not true.
答案 0 :(得分:20)
flutter不知道文本是LTR还是RTL,所以你需要明确告诉他textDirection
new Text("Hello", textDirection: TextDirection.ltr)
或者您可以使用Directionality小部件
包装文本new Directionality(
textDirection: TextDirection.ltr,
child: new Text('Hello')
,其中的耻辱是:
确定文本和环境方向性的小部件 文本方向敏感的渲染对象。
在MaterialApp小部件范围内的Text小部件不需要给出明确的书写方向,因为小部件和材料库中的默认本地化是LTR
答案 1 :(得分:12)
如果您不使用MaterialApp
,则需要自己将应用程序包装到某些小部件中
import 'dart:ui' as ui;
...
runApp(
new MediaQuery(
data: new MediaQueryData.fromWindow(ui.window),
child: new Directionality(
textDirection: TextDirection.rtl,
child: new MyHome())))
答案 2 :(得分:5)
如果未在树层次结构的根级别使用MaterialApp
或WidgetsApp
,则会出现此错误。
解决方案:
void main() {
runApp(
MaterialApp(home: MyHome()), // use MaterialApp
);
}
class MyHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold( // use Scaffold also in order to provide material app widgets
body: Center(child: Text("Something")),
);
}
}
答案 3 :(得分:2)
在 Dart 2 中,使用 new 是可选的。此外,有效 Dart 指南不鼓励使用新。
您需要做的就是将 MyHome() 类包装到 MaterialApp() 中。
void main() {
runApp(MaterialApp(home: MyHome()));
}
答案 4 :(得分:0)
问题不在于您没有将小部件包装到MaterialApp中。正如文档所说,由于嵌套了相同类型的小部件,例如将行嵌入行,列到列或列到ListView中,所以出现了这个错误,因此问题就出现了,因为它成为Widget的无限制约束,因此无法识别方向。
为了避免这种问题,会发生使用Flexible或Expanded的问题,以识别剩余的空间。
在您的情况下,您只需向行小部件提供值为start
的crossAxisAlignment。
有关详细信息,请访问此documentation页面