Reputation: 31
I have learned Dart and when I started learning Flutter, I got confused by some parts of Flutter code.
Can someone tell me what's the meaning of the colon (:) after child, in the following code:
const Center(
child: Text(
'Hello, world!',
textDirection: TextDirection.ltr,
),
Thanks
Upvotes: 0
Views: 172
Reputation: 63809
This is used on named constructor. The textDirection
is an optional named parameter on Text
widget.
class Foo {
final int data;
Foo({
required this.data,
});
}
Here Foo
class has single integer data
field, and using named required constructor to create instance.
final foo = Foo(data: 1);
Now If it was optional as textDirection
, we aren't forced to pass it when creating a Foo instance like
class Foo {
final int? data;
Foo({
this.data,
});
}
void main(List<String> args) {
final foo = Foo();
}
Now explore the Text
, It contains one String
position filed and others are optional positional argument.
Here we are forced to pass a String, but data
is optional
class Foo {
final String text;
final int? data;
Foo(this.text, {this.data});
}
void main(List<String> args) {
final foo = Foo("test");
}
The Text
widget has been defined like
const Text(
String this.data, {
super.key,
this.style,
this.strutStyle,
this.textAlign,
this.textDirection,
this.locale,
.......
Find more about using constructors and Text
Upvotes: 3