I just run the basic flutter code. I want to make stateful widget, only containing Text("hi") so I write my code like below
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Container(child: Text("hi");
}
}
but I got error No Directionality widget found. RichText widgets require a Directionality widget ancestor
I can fix my error to add textDirection attribute, but I wonder when I use Text I didn't get that error even if I don't use that attribute. I just want to use only Text, Container
CodePudding user response:
i think its because you dont have MaterialApp
try it like this will work
void main() {
runApp(const MyApp1());
}
class MyApp1 extends StatelessWidget {
const MyApp1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyApp(),
);
}
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Container(child: Text("hi"));
}
}
CodePudding user response:
Text
needs a Directionality
widget to provide information on whether the text is "left-to-right" or "right-to-left". Usually this is done (behind the scene) by MaterialApp
widget. If you don't want to use that, you could provide one yourself, and specify the text direction.
Minimal code for a hello world:
import 'package:flutter/material.dart';
void main() {
runApp(
Directionality(
textDirection: TextDirection.ltr,
child: Text("Hello world."),
),
);
}