Rablidad
Rablidad

Reputation: 55

What is this widget property from the StatefulWidget flutter

I sometimes see something like:
widget.title or widget.(anything) in flutter; like this example in the Text widget child of an AppBar Widget:

class MyApp extends StatefulWidget{
    // some declarations here
    @override
    _MyApp createState() => _MyApp();
}

class _MyApp extends State<MyApp>{
   // some declaration here
    @override
    Widget build(BuildContext context){

        return MaterialApp(
            home: Scaffold(
               appBar: AppBar(child: Text(widget.title),),
            ),
        );
    }
}

what is this actually?

widget.title
I mean, what is the widget referencing? what is it?

Upvotes: 0

Views: 2785

Answers (3)

Zukhrofy Abbar
Zukhrofy Abbar

Reputation: 1

The widget property is a part of the State class, which inherits it in the _YourWidgetState class. This _YourWidgetState class is a subclass of the State class.

When you examine the State class reference, you will find:

abstract class State<T extends StatefulWidget> with Diagnosticable {
    get widget => _widget!;
    T? _widget;
}

In this context, T represents the type parameter of the State class, which, in this case, is your custom widget class. Consequently, the widget property within the State class refers to an instance of your custom widget class.

Let's consider the example of the BiggerText widget:

class BiggerText extends StatefulWidget {
   final String text;

   const BiggerText({Key? key, required this.text}) : super(key: key);

   @override
    State<BiggerText> createState() => _BiggerTextState();
}

class _BiggerTextState extends State<BiggerText> {
   double _textSize = 16.0;

   @override
   Widget build(BuildContext context) {
     return Column(
       mainAxisAlignment: MainAxisAlignment.center,
       children: <Widget>[
         Text(**widget.text**, style: TextStyle(fontSize: _textSize)),
       ],
     );
   }
}

based on the example above

class _BiggerTextState extends State<BiggerText> 

The code signifies that _BiggerTextState inherits the widget property from the State class, thanks to being a subclass of the State class.

Within the State class, the widget property represents an instance of the generic type T, which, in this case, corresponds to the BiggerText class. As a result, the widget property in this context is an instance of the BiggerText class itself.

So, when you use, for instance, widget.text, you're accessing a property of the BiggerText class.

Upvotes: 0

Mangaldeep Pannu
Mangaldeep Pannu

Reputation: 3987

Long answer short

You have extended the State class.
The State class has a readonly property called widget. Which is what you are referencing to.

Upvotes: 1

anmol.majhail
anmol.majhail

Reputation: 51206

The MyApp class extends StatefulWidget, which means this widget stores mutable state. When the MyApp widget is first inserted into the tree, the framework calls the createState() function to create a fresh instance of _MyAppState to associate with that location in the tree. (Notice that subclasses of State are typically named with leading underscores to indicate that they are private implementation details.) When this widget’s parent rebuilds, the parent creates a new instance of MyApp, but the framework reuses the _MyAppState instance that is already in the tree rather than calling createState again.

To access the properties of the current MyApp, the _MyAppState can use its widget property. If the parent rebuilds and creates a new MyApp, the _MyAppState rebuilds with the new widget value. If you wish to be notified when the widget property changes, override the didUpdateWidget() function, which is passed as oldWidget to let you compare the old widget with the current widget.

Now as Per Docs: widget property

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.

reference link

Upvotes: 1

Related Questions