Jay Tillu
Jay Tillu

Reputation: 1548

How to convert a Stateful widget into a Stateless widget in flutter?

I have a stateful widget and I want to convert it into a stateless widget. I know how to convert a Stateless widget into a Stateful widget. Just like a Stateless widget is there any shortcut available to convert a Stateful widget.

I am using an Android Studio. Thanks :)

Upvotes: 11

Views: 21130

Answers (7)

Reinier Garcia
Reinier Garcia

Reputation: 1690

There is a very simple shortcut in Android Studio.

Step 1: We initially have a StatefulWidget.

import 'package:flutter/material.dart';

class CategoriesScreen extends StatefulWidget {
  final String title;

  const CategoriesScreen({super.key, required this.title});

  @override
  State<CategoriesScreen> createState() => _CategoriesScreenState();
}

class _CategoriesScreenState extends State<CategoriesScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Hello World'),
          ],
        ),
      ),
    );
  }
}

enter image description here

Step # 2: Place your mouse anywhere in the Widget's class name line of code

enter image description here

Step # 3: Press on your keyboard the keys: Option + Enter.

enter image description here

Step # 4: Select the option "Convert to StatelessWidget".

Step # 5: Now we have a StatelessWidget.

enter image description here

Upvotes: 0

Komolafe Ezekiel dare
Komolafe Ezekiel dare

Reputation: 115

Simple three steps on android studio to convert stateful widget to stateless widget

  1. comment state class of the stateful widget and delete the commented area.
  2. change the stateful widget name that the class extends to statelessWidget
  3. press control R, and it will open two text fields for replace function above your code, press widget. into the upper text field, and leave the lower one empty, then click on replace all button. This process will help you convert all the final variables that were convert by the stateful Widget back to what they were before.

Upvotes: 0

Suj
Suj

Reputation: 97

You can use @Nirmal 's Trick for converting to stateless and for Renaming variables(gotta convert from widget.variable to just variable) you can just Find widget. and replace with empty value like this and hit replace all

enter image description here

Upvotes: 3

tchindebe235
tchindebe235

Reputation: 25

Hover your cursor to StatelessWidget and use option + return, IDE will show you to convert this class to a Stateful Widget. Tap that option and following code is generated for you.

https://github.com/flutter/flutter-intellij/issues/3084

Upvotes: -1

Nirmal
Nirmal

Reputation: 626

One Simple Trick

    class Counter extends StatelessWidget {
//   @override
//   _CounterState createState() => _CounterState();
// }

// class _CounterState extends State<Counter> {
  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}

Just comment in between and change the statefulwidget to statelesswidget

Upvotes: 51

Matt Forbes
Matt Forbes

Reputation: 21

There is no shortcut on Android studio. Best option change 'StatefulWidget' to 'StatelessWidget' and delete the associated boiler plate code.

class TaskTile extends StatefulWidget {
  @override
  _TaskTileState createState() => _TaskTileState();
}

Change to:

 class TaskTile extends StatelessWidget {

    }

Upvotes: 2

HII
HII

Reputation: 4109

No there is no keyboard shortcut until now

Upvotes: 3

Related Questions