Reputation: 1548
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
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'),
],
),
),
);
}
}
Step # 2: Place your mouse anywhere in the Widget's class name line of code
Step # 3: Press on your keyboard the keys: Option + Enter.
Step # 4: Select the option "Convert to StatelessWidget".
Step # 5: Now we have a StatelessWidget.
Upvotes: 0
Reputation: 115
Simple three steps on android studio to convert stateful widget to stateless widget
Upvotes: 0
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
Upvotes: 3
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
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
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