Do's and Don'ts for Flutter development

Overview

Best practices in Flutter development

Do's and Don'ts for Flutter development, heavily inspired from the android-best-practices

Summary

Add a linting rules firstly when starting app from scratch

Use const wherever possible

Create separate class to define the colors

Define theme for your app

Don't use functional widgets


lint-rules

Whenever you start a project from scratch firstly add Lint package which can help you to statically analyzed your flutter/dart code and can help you to improve your code quality which evantually reduces the bugs and errors.

use-const

Try using a const keyword wherever possible, to improve the performance of the app. For example: When you use it for some widgets and when setState gets called it does not change the widget with the const keyword.

const Text(
  "Flutter Best Practices",
  style: const TextStyle(
    fontSize: 24,
    fontWeight: FontWeight.bold,
  ),
),

separate-color-class

Try to have all the colors in a single class for your application, do it with the Strings as well if you are not using the localization so whenever you want to add localization you can find all the strings in one place.

Note: You might get the linting error avoid_classes_with_only_static_members but it is okay to ignore for this kind of usage.

Quoting the official documentation:

In idiomatic Dart, classes define kinds of objects. A type that is never instantiated is a code smell. However, this isn’t a hard rule. With constants and enum-like types, it may be natural to group them in a class.

Color Example:

class AppColor {
  static const Color red = Color(0xFFFF0000);
  static const Color green = Color(0xFF4CAF50);
  static const Color errorRed = Color(0xFFFF6E6E);
}

define-theme

Define Theme of the app as well as a first priority to avoid the headache of changing the theme in future updates, Setting up Theme is surely confusring but one time task. Ask your designer to share all the Theme related data like, Colors, font sizes and weightage.

Example:

MaterialApp(
  title: appName,
  theme: ThemeData(
    // Define the default brightness and colors.
    brightness: Brightness.dark,
    
    // You can add the color from the seprate class here as well to maintain it well.
    primaryColor: Colors.lightBlue[800],

    // Define the default font family.
    fontFamily: 'Georgia',

    // Define the default `TextTheme`. Use this to specify the default
    // text styling for headlines, titles, bodies of text, and more.
    textTheme: const TextTheme(
      headline1: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
      headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
      bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
    ),
  ),
  home: const MyHomePage(
    title: appName,
  ),
);

You can do much more with the Theme, which is defining your custom TextField, Card, Bottom Navigation Bar themes for once and use it straight away in the app. Checkout an example of using Theme over here

In Addition, In a large app/project you might end up having a multipl Themes not just light and dark mode themes but Custom themes for some specific widgets/components as well so you can always create a separate class/file and maintain all the themes over there.

avoid-functional-widgets

We usually have a situation where we need to separate out UI code from the widget, But we avoid creating a separate widget and use function which returns Widget. This practice have some benefits, like you don't need to pass all parameters in your new widget, You have less code and less files. But this approach may cause issue when you want to inspect your widget. Let's see this in depth.

When you use functional widget code looks like this.

Widget functionWidget({ Widget child}) {
  return Container(child: child);
}

You can now use it as

functionWidget(
  child: functionWidget(),
);

In this case Widget tree will look something like this

Container
  Container

Instead if we use Widget, Our widget looks like

class ClassWidget extends StatelessWidget {
  final Widget child;

  const ClassWidget({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: child,
    );
  }
}

You can use it as

new ClassWidget(
  child: new ClassWidget(),
);

And in this case Widget tree looks like this

ClassWidget
  Container
    ClassWidget
      Container

As we can see here, if we use Widgets, framework understands it in better way and UI becomes easy to inspect. For more info follow this answer from stackoverflow

You might also like...

This is an app created by me while undertaking an android app development in flutter course on Udemy.

Expense Planner App A Flutter project created by Mithilesh Ghadge in a Fluttter Android app development course on Udemy. Create a new Flutter project

Oct 14, 2021

Flutter development projects

Flutter development projects

quizapp A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if this

Oct 26, 2021

Utility classes/functions to help with UI development using the Flutter framework.

Aqua Utility classes/functions to help with UI development using the Flutter framework. It is recommended to use the as keyword with the import statem

Jul 17, 2022

Grocery-App (Widle Studio - A Creative Flutter App Development Company) OR Get an Estimate

 Grocery-App (Widle Studio - A Creative Flutter App Development Company) OR Get an Estimate

Grocery-App (Widle Studio - A Creative Flutter App Development Company) OR Get an Estimate Flutter Grocery Shopping App Fully Working Template with Wo

Jul 21, 2022

SideHustle Mobile Application Development Track (Group 1)

SideHustle Mobile Application Development Track (Group 1)

login_signup A new Flutter project. Getting Started Flutter Sign and Sign Up Page. Group 1 Mobile Application Track SideHustle Internship 4.0 GroupLis

Dec 21, 2021

Any contributor is free to add his Project on Mobile development.

Any contributor is free to add his Project on Mobile development.

Any contributor is free to add his Project on Mobile development. This repository's sole aim is to increase the knowledge about Open Source Contribution and for beginners to understand Git and Github

Jan 30, 2022

FileManager is a wonderful widget that allows you to manage files and folders, pick files and folders, and do a lot more. Designed to feel like part of the Flutter framework.

FileManager is a wonderful widget that allows you to manage files and folders, pick files and folders, and do a lot more. Designed to feel like part of the Flutter framework.

File Manager FileManager is a wonderful widget that allows you to manage files and folders, pick files and folders, and do a lot more. Designed to fee

Dec 30, 2022

Flutter ShopApp, you can see products and their prices, categories and their products, search for a product, add to favorite, add to cart, sign in and sign up.

shop_app_2 A new Flutter application. Getting Started This project is a starting point for a Flutter application. A few resources to get you started i

Aug 7, 2022

A Flutter plugin for handling Connectivity and REAL Connection state in the mobile, web and desktop platforms. Supports iOS, Android, Web, Windows, Linux and macOS.

A Flutter plugin for handling Connectivity and REAL Connection state in the mobile, web and desktop platforms. Supports iOS, Android, Web, Windows, Linux and macOS.

cross_connectivity A Flutter plugin for handling Connectivity and REAL Connection state in the mobile, web and desktop platforms. Supports iOS, Androi

Nov 15, 2022
Comments
  • fix: solved a navigating issue

    fix: solved a navigating issue

    In the Summary section, on clicking or tapping at Don't use functional widgets, it didn't navigate to dont-use-functional-widgets.

    The problem was the way md file was written.

    If a line is written in this way

    #### [This is a line](#this-is-a-line)
    

    Then, there arises a problem in the navigation when we try clicking or tapping on the text as both the text inside of square brackets and parenthesis are the same.

    My commit resolves that issue.

    opened by Biplab-Dutta 1
  • Add a directory structure

    Add a directory structure

    Below is the directory structure I follow to separate the code/files.

    assets/
      png/
        1x/
        2x/
        3x/
      svg/
      
    lib/
      network/
      models/
      utils/
      screens/
      constants/
      widgets/
    
    opened by ibhavikmakwana 0
Owner
Bhavik Makwana
Google Developer Expert for Flutter and Dart | Speaker | Blogger | Google Certified Associate Android Developer
Bhavik Makwana
Exemplo em Flutter, para trabalhar com as câmeras dos devices

Fotografe e compartilhe! Como rodar: Clone the project to your Machine. Import the project to Android Studio. Run directly on Android or iOS device. A

null 3 Aug 23, 2021
Consumo de dos API de musica last.fm y musixmatch (Lyrics)

practica5 A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if thi

null 0 Nov 6, 2021
Blog App Development Front-End and Back-End using Flutter, ExpressJs, NodeJS, and MongoDB

Blog App Development Front-End and Back-End using Flutter, ExpressJs, NodeJS, and MongoDB

null 2 Dec 14, 2022
A study about clean architecture and TDD(Test Driven Development) in Flutter.

coin_checker A study about clean architecture and TDD(Test Driven Development) in Flutter. Getting Started This project is a starting point for a Flut

null 2 Jan 25, 2022
Do's and Don'ts for Flutter development

Best practices in Flutter development Do's and Don'ts for Flutter development, heavily inspired from the android-best-practices Summary Add a linting

Bhavik Makwana 91 Dec 30, 2022
Development of a simple mobile application to perform mathematical operations, using DART and FLUTTER

Desenvolvimento de uma aplicação mobile simples para realizar operações matemáticas, usando DART e FLUTTER.

Bruno Silva 2 Jan 20, 2022
A research and development project EMPLOYEE DASHBOARD

employee_dashboard A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you start

k Sai Kiran 1 Nov 5, 2021
Web development for smartgreen's RYO-OHKI Project, should contain: a login page, import, export and visualization of cards (almost a dashboard) it is under construction!

Web development for smartgreen's RYO-OHKI Project, should contain: a login page, import, export and visualization of cards (almost a dashboard) it is under construction!

Beatriz gonçalves 6 Nov 13, 2021
Flutter App Developer Roadmap - A complete roadmap to learn Flutter App Development

Flutter App Developer Roadmap - A complete roadmap to learn Flutter App Development. I tried to learn flutter using this roadmap. If you want to add something please contribute to the project. Happy Learning

Md Tarikul Islam 708 Jan 3, 2023
Implementing Firebase Authentication with Riverpod following Flutter Domain Driven Development pattern

firebase_auth_flutter_ddd Firebase authentication example with Hooks Riverpod and Freezed following Flutter DDD architecture Getting Started This proj

Python Hub 42 Jan 8, 2023