J'essaie de créer une mise en page simple dans flutter
mais continue à avoir cette erreur:
L'assertion suivante a été lancée en construisant Text ("Livrer les fonctionnalités plus rapidement", textAlign: center): Aucun widget de directionalité trouvé.
Je pense que le problème provient peut-être de la propriété de la classe TextDirection, mais je n’ai pas pu le trouver.
Voici mon code:
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(),
),
),
],
)
),
);
}
}
Voici la trace complète de la pile de l'erreur:
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.
flutter ne sait pas si le texte est LTR ou RTL, vous devez donc lui dire le textDirection explicitement
new Text("Hello", textDirection: TextDirection.ltr)
ou vous pouvez simplement envelopper le texte avec Directionality Widget
new Directionality(
textDirection: TextDirection.ltr,
child: new Text('Hello')
et le but de cela est:
Un widget qui détermine la directionnalité ambiante du texte et objets de rendu sensibles au sens du texte.
De plus, il n'est pas nécessaire d'indiquer un sens d'écriture explicite à un widget Texte dans le périmètre d'un widget MaterialApp, car la localisation par défaut dans les bibliothèques de widgets et de matériaux est LTR.
Si vous n'utilisez pas MaterialApp
, vous devez envelopper vous-même votre application dans des widgets.
runApp(
new MediaQuery(
data: new MediaQueryData.fromWindow(ui.window),
child: new Directionality(
textDirection: TextDirection.rtl,
child: new MyHome())))
Le problème n'est pas que vous n'avez pas emballé vos widgets dans MaterialApp. Comme le dit la documentation, cette erreur est due à l'imbrication du même type de Widgets, comme l'imbrication de Rangée sur Rangée, Colonne sur Colonne ou Colonne sur ListView, de sorte que le problème se pose lorsqu'il devient une contrainte sans limite pour le Widget, incapable d'identifier la direction.
Donc, pour éviter ce genre de problèmes, il est fait appel à Flexible ou Expanded qui identifie l’espace restant.
Dans votre cas, vous pouvez simplement donner à crossAxisAlignment la valeur start
au widget Row.
Pour plus d'informations, visitez cette documentation page