Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 236
Jay Tillu Avatar asked Apr 23 '20 12:04

Jay Tillu


People also ask

How do I change stateful widget to stateless widget?

To do so, just select the stateless or stateful widget and press Alt+Enter (for both Mac and Windows). A pop-up menu will appear. Choose the Convert to option to convert the widget from stateful to stateless or vice versa.

Can stateless widget contain stateful widget flutter?

A widget is either stateful or stateless. If a widget can change—when a user interacts with it, for example—it's stateful. A stateless widget never changes. Icon , IconButton , and Text are examples of stateless widgets.

Can you have a stateful widget inside a stateless widget?

when we update the state of the Stateful widget it will re-run its build function, when a stateless widget is part of that build function, flutter will check if its input data has changed or not, if not it will return the same instance of the widget, if it's changed then it will create another instance from the ...


Video Answer


1 Answers

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

like image 176
Nirmal Avatar answered Jan 03 '23 14:01

Nirmal