Skip to content

Creating New Widget

Follow these steps to create new widgets in your Flutter project:

  1. [Optional but good to have] Make sure you have a widgets folder in your lib folder

  2. Create a new file in the widgets folder

  3. Name the file something ending with _widget.dart, for example form_widget.dart (feel free to rename it)

  4. Open the newly created Dart file

  5. Import material.dart:

    import 'package:flutter/material.dart';
    

  6. Create the class in PascalCase and extend either StatelessWidget or StatefulWidget

    TIP

    It is better to create it as a StatelessWidget first and then convert it into a StatefulWidget if needed.

    class FormWidget extends StatelessWidget {
    } // end of class
    
  7. In the class, add the build() method and do not forget to add @override:

    REMEMBER

    routeName is not needed for a widget!

    class LoginScreen extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
            return 
        }
    }
    
  8. In the build() method, return the type of widget you want to use:

    class LoginScreen extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
            return Placeholder(); // Placeholder Widget is just an example, so please change it!
        }
    }
    
  9. Save your changes

    REMEMBER

    Since there is no routeName in a widget, you do not need to "register" routes.