[英]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.我正在使用 Android Studio。 Thanks:)
谢谢:)
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只需在两者之间进行评论并将 statefulwidget 更改为 statelesswidget
No there is no keyboard shortcut until now不,直到现在还没有键盘快捷键
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.
您可以使用 @Nirmal 的技巧来转换为无状态和重命名变量(必须从 widget.variable 转换为变量),您只需查找
widget.
and replace with empty value like this and hit replace all并用这样的空值替换并点击全部替换
There is no shortcut on Android studio. Android studio 上没有捷径。 Best option change 'StatefulWidget' to 'StatelessWidget' and delete the associated boiler plate code.
最佳选项将“StatefulWidget”更改为“StatelessWidget”并删除相关的样板代码。
class TaskTile extends StatefulWidget {
@override
_TaskTileState createState() => _TaskTileState();
}
Change to:改成:
class TaskTile extends StatelessWidget {
}
Simple three steps on android studio to convert stateful widget to stateless widget android studio上简单三步将有状态小部件转换为无状态小部件
Hover your cursor to StatelessWidget and use option + return, IDE will show you to convert this class to a Stateful Widget. Hover 将您的 cursor 转换为 StatelessWidget 并使用选项 + 返回,IDE 将告诉您将此 ZA2F2ED4F8EBC4ABCBBD4C 转换为Tap that option and following code is generated for you.
点击该选项并为您生成以下代码。
https://github.com/flutter/flutter-intellij/issues/3084 https://github.com/flutter/flutter-intellij/issues/3084
In most cases it wouldn't even make sense.在大多数情况下,它甚至没有意义。 Stateless widgets are a subset of stateful widgets.
无状态小部件是有状态小部件的子集。 That means that every stateless widget is also a stateful widget.
这意味着每个无状态小部件也是一个有状态小部件。
But not every stateful widget is stateless.但并不是每个有状态的小部件都是无状态的。 So there is a possiblity of breaking code if you would "convert" it.
因此,如果您“转换”它,就有可能破坏代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.