Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter: What is good practice for making the custom widget?

I am confused about good practice between creating the custom widget with the Widget function and the class (stf,stl).

For the example about creating the custom widget with the Widget function:

class FieldCustomWidget {
  static Widget textField(...) {
    return ...;
  }

  static Widget idCardNumberField(...) {
    return ...;
  }

  static Widget phoneField(...) {
    return ...;
  }
}

For the example about creating the custom widget with the class (stf,stl):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class TextFieldCustomWidget  extends StatelessWidget {
  ...
  const TextFieldCustomWidget ({Key? key, ...}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ...;
  }
}
class IdCardNumberFieldCustomWidget  extends StatelessWidget {
  ...
  const IdCardNumberFieldCustomWidget ({Key? key, ...}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ...;
  }
}
class PhoneFieldCustomWidget  extends StatelessWidget {
  ...
  const PhoneFieldCustomWidget ({Key? key, ...}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ...;
  }
}

All customer widgets can use the Widget function or stl because I am using state management.
What is good practice?
If you have another way, please tell me.

>Solution :

As a general convention don’t use functions to create widgets. Here is a discussion if you want to know more details about this.

https://github.com/flutter/flutter/issues/19269

What is the difference between functions and classes to create reusable widgets?

If you are following a functional programming paradigm you can use the functional_widget package.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading