Flutter IDE Wish: Define a Stateful/Stateless widget class
See original GitHub issueThere are several basic bits of widget class boilerplate that Flutter app developers are likely to write repeatedly: define a stateless widget class, define a stateful widget class. It’s also common to start with a stateless widget and then convert it to a stateful widget later on. Finally, when a Stateful widget class is defined, you may want to have it create an AnimationController.
The IDE could generate the boilerplate automatically. Perhaps, in some cases, it could also automatically convert a stateless widget to a stateful one (and back).
The boilerplate for a stateless widget Foo
class Foo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Container(); // YOUR CODE HERE
}
}
The boilerplate for a stateful widget Foo
class Foo extends StatefulWidget {
@override
_FooState createState() => new _FooState();
}
class _FooState extends State<Foo> {
@override
Widget build(BuildContext context) {
return new Container(); // YOUR CODE HERE
}
}
The boilerplate for a stateful widget Foo, with an AnimationController:
class Foo extends StatefulWidget {
@override
_FooState createState() => new _FooState();
}
class _FooState extends State<Foo> with SingleTickerProviderStateMixin {
AnimationController _controller;
@override
void initState() {
super.initState();
_controller = new AnimationController(vsync: this);
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
@override
Widget build(BuildContext context) {
return new Container(); // YOUR CODE HERE
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (8 by maintainers)
Top Results From Across the Web
Adding interactivity to your Flutter app - Flutter documentation
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...
Read more >Flutter — IDE Shortcuts for Faster Development - Medium
The IDE can do it for you! Just type stless to create a Stateless Widget like this: Or stful to create Stateful widget:...
Read more >Intro to Flutter - Stateful and Stateless Widgets, Widget Tree
tensorprogramming # flutter #introtoflutterIn this tutorial, we take a look at Flutter basics. We look at the Widget tree and the Stateless ......
Read more >Widgets: Stateless and Stateful - DataDrivenInvestor
Meaning, in terms of Android development, flutter does not have two ... This widget inherits from the class StatelessWidget which is a ...
Read more >What is the difference between stateless and stateful widgets ...
Implementing a stateful widget requires at least two classes: 1) a StatefulWidget class that creates an instance of 2) a State class. The ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We implemented a Quick Assist (activated using Alt+Enter) to convert
StatelessWidget
toStatefulWidget
.Here’s a possible solution that I’d like feedback regarding usability. If this is usable we can build this, and others, into the Flutter plugin.
The basic idea is to create a custom live template that expands into the definition of a new StatelessWidget subclass. Open Preferences/Settings and search for the Live Templates page. Here’s what mine looks like when finished.
Getting there requires a few steps.
+
(under “Reset” on the right side) and select option 2. Call it “Flutter”.+
again.<abbreviation>
field. I chose “stless” (more about that later).Define
button next to “No applicable contexts yet.”Dart
and select theOther
beneath it. (This is a bit flakey, I think. I had to deselectDart
and reselectOther
to eliminate statement applicability.)OK
to save the new template.You can use the template in two ways: select the template from a menu or type “stless” and hit Tab. Your cursor needs to be outside a class definition for the template to be available.
If you type “stless<tab>” the template will be expanded and your cursor will be left at the position of the new class name. If you select the template from a menu then you can get an additional shortcut by typing the name of the new class, selecting it, then selecting the template from the menu (Cmd-Opt-J on Mac or Cmd-Opt-T on Mac). Cmd-Opt-J has the advantage of putting
stless
at the top of the list. Once the template is expanded your new class is defined and the cursor is left in the arguments ofnew Container()
.I know it is asking a lot to have someone experiment with this. But if this is actually useful it would be simple to add templates for all the suggestions in this issue to the Flutter plugin.
Also, $SELECTION$ can appear multiple times. Here’s the output of a Stateful widget template (after reformatting):
/cc @sethladd @mit-mit