Flutter-intellij: "Create Constructor" for Widgets

Created on 28 Mar 2018  路  9Comments  路  Source: flutter/flutter-intellij

Hey hey :) I find myself creating a lot of Widget constructors, and I end up repeating the same manual process over and over. I thought it might be cool if this could be part of the Flutter IntelliJ plugin!

tl;dr -- "Create Constructor" should produce an idiomatic constructor for Widgets.

Full Steps:

  1. type stless to create a Stateless Widget
  2. Add a final field to the Widget
  3. Put cursor over the final field
  4. Alt-Enter
  5. Select "Create Constructor for Final Field", produces: MyWidget(this.title);
  6. Change this to conform with the normal Flutter rules: MyWidget({Key key, this.title}) : super(key: key);

    • All fields optional

    • Includes a Key and proper call to super.

Not a huge deal, but I've done it so much recently I thought it'd be nice if it could be generated for me :) Thanks again for all the hard work -- the plugin is getting sooooo good.

enhancement

Most helpful comment

All 9 comments

Well, @scheglov what do you think? We already treat Widget subclasses specially in other ways for some assists.

@brianegan Could you please point me out on the "normal Flutter rules", so that I could read more about it?

Ah, I found it.
https://docs.flutter.io/flutter/widgets/StatelessWidget-class.html

By convention, widget constructors only use named arguments. Named arguments can be marked as required using @required. Also by convention, the first argument is key, and the last argument is child, children, or the equivalent.

Yep, that's it! Sorry about the slow response, appreciate ya taking a look at it :)

Omg, amazing. Thanks so much!

How can I view the light bulb? It does not appear on Android Studio on highlight.

@leslieharland Alt + Enter

My newly installed Android Studio / Flutter / Dart does not generate Widget constructors in accordance with Flutter rules, how can I make it do that?
https://stackoverflow.com/questions/62524041/can-i-make-android-studio-generate-dart-constructors-code-with-named-parameters

Was this page helpful?
0 / 5 - 0 ratings