Tapioca
Tapioca

Reputation: 339

What are StatelessWidget?

When I create a simple, basic app, like the one below, in flutter, Im creating a non explicit StatelessWidget?

import 'package:flutter/material.dart';

void main() {
  return runApp(
    MaterialApp(
      home: Scaffold(
         body: Text('HI),
      ),
    ),
  );
}

Upvotes: 2

Views: 612

Answers (4)

shalu sampla
shalu sampla

Reputation: 11

Stateless widgets There are cases where you’ll create widgets that don’t need to manage any form of internal state, this is where you’ll want to make use of the StatelessWidget. This widget doesn’t require any mutable state and will be used at times where other than the data that is initially passed into the object.

https://api.flutter.dev/flutter/widgets/StatelessWidget-class.html

Upvotes: 0

Shagun Jain
Shagun Jain

Reputation: 171

As stateless widget is the one which cannot change its state or instances defined in it is final. And in your code your app state cannot be changed. So you can assume it to be a stateless widget because in flutter everything is a widget.

Upvotes: 1

George Rappel
George Rappel

Reputation: 7198

It's not creating a non explicit widget. So it would be neither, not Stateless nor Stateful.

The runApp function will just take your MaterialApp widget and "attach it to the screen", so you're not creating a widget along the way, but just using widgets that already exist. Your widget tree will begin at the MaterialApp itself.


Conceptually, also, I'd say that since you don't have a space to work with the state and/or variables of the widget, it could be called a StatelessWidget, since you can't change the state of what you built.

Widgets describe what their view should look like given their current configuration and state. When a widget’s state changes, the widget rebuilds its description [...]. - from the Flutter Widgets Intro

In your example, even though you do have StatefulWidgets in your app tree, you don't have a way of exposing the state of that tree, no variables, no control, no management. You're just passing other widgets to your app directly. You're actually referring one widget tree (the MaterialApp with its children) to the runApp function.

Upvotes: 3

ejabu
ejabu

Reputation: 3147

although apparently we cannot interact with screen, actually inside the widget tree, we have Stateful Widget by rendering Scaffold.

Scaffold extends StatefulWidget

Upvotes: 1

Related Questions