Reputation: 1345
I was learning a bit of flutter, and I came across part where we define states, a class was given something like this:
class _sButtonState extends State<sButton>{
@override
Widget build(BuildContext context){
return Container(
RaisedButton(
color: Colors.red,
child: widget.child,
onPressed: widget.onPressed,
)
);
}
}
widget wasn't an instance
nor class
variable defined anywhere. So, I could only assume it was something implicitly provided by the dart framework. So, my question is:
widget
dart
called?Upvotes: 0
Views: 92
Reputation: 80914
widget
is a property in the State
class, since _sButtonState
extends State
then your class inherits those properties and methods.
The current configuration.
A State object's configuration is the corresponding StatefulWidget instance. This property is initialized by the framework before calling initState. If the parent updates this location in the tree to a new widget with the same runtimeType and Widget.key as the current configuration, the framework will update this property to refer to the new widget and then call didUpdateWidget, passing the old configuration as an argument.
For example:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
title
property is an instance variable in the class MyHomePage
which extends StatefulWidget
and since State<T extends StatefulWidget>
, then you can use the custom class MyHomePage
and call widget.title
Upvotes: 1