Skip to content

Instantly share code, notes, and snippets.

@fmcarvalho
Last active September 16, 2021 12:57
Show Gist options
  • Save fmcarvalho/5f57a071ba5261b2e922df96de4c924f to your computer and use it in GitHub Desktop.
Save fmcarvalho/5f57a071ba5261b2e922df96de4c924f to your computer and use it in GitHub Desktop.
Flutter cheatsheet

After installing Flutter you will have on Windows:

  • C:\src\flutter ---> Git repository ---> ~964 Mb
  • C:\src\flutter\bin ---> Tools
  • C:\src\flutter\bin\cache\dart-sdk ----> DART ---> ~418 Mb

Maybe you will need the following paths on PATH environment variable:

  • C:\src\flutter\bin ---> contains dart.bat and flutter.bat
  • C:\Users\miguel\AppData\Local\Android\Sdk\build-tools\29.0.2
  • C:\Users\miguel\AppData\Local\Android\Sdk\emulator

=====> Flutter Depends on Android SKD which in turn "may" depend of the Android Studio

  • C:\Users\migue\AppData\Local\Android\Sdk ===> 8.1 Gb
  • C:\Program Files\Android\Android Studio ===> 1.5 Gb

====> Flutter also requires Android SDK Command-line Tools

====> Android Studio then go to Tools -> SDK Manager -> SDK Tools -> Android SDK Command-line Tools

##################################

dart migrate --apply-changes // migrate code for null safety

flutter doctor                     # Check installation
flutter create <sample app name>
flutter devices                    # listing available devices
flutter run 
flutter run -d chrome

>>>>> Type r on console for hot reload of the application

emulator -list-avds  # To list and then choose your emulator to run

##################################

  • main.dart file
  • void main() - entry point
  • runApp(<Widget instance>) - inflate given widget and attach it to the screen
  • Custom Widget extends Widget e.g. class MyApp extends StatelessWidget...
  • An App is a Widget (e.g. StatelessWidget)
  • StatelessWidget
    • does not require mutable state
    • are immutable, meaning that their properties can't change (all values are final).
  • In Flutter almost everything is a Widget

MyApp --- build() ----> MaterialApp -----> Scaffold

  • build:
    • method that describes how to display the widget in terms of other, lower-level widgets.
    • runs each time the App requires rendering.
  • class MyApp extends StatelessWidget { @override Widget build(BuildContext context) ----> MaterialApp
  • Scaffold from package:flutter/material.dart has appBar and body properties

  • Type keyword stful and TAB to generate StatefulWidget boilerplate code on your visual code editor.

  • StatefulWidget:

    • is immutable and can be Thrown away and Regenerated.
    • its State object persists over the lifetime of the widget.
    • the State object class as the name of its Widget beginning with _ and finishing with State.
    • E.g. RandomWords widget has a corresponding State class _RandomWordsState.
    • The State class relates with its Widget through the type parameter of the base class, i.e. ... extends State<RandomWords>

SatefulWidget ---- createState() ----> State<SatefulWidget> --- build() ---> another Widget

NOTICE !!!!! The build() on Widget may be called Many types and thus generate (new words on RandomWords app on every reload)

!!!!! On the other hand, the createState() is called once and the resulting State persists (the same word persists).

  • Handlers may call setState() to notify the framework that state has changed.
  • setState() ----- triggers-- --> State::build() ---> Update UI

ListView:

  • Static -- ListView(children: ...list of widgets ...)
  • Dynamic -- ListView.builder(itemBuilder: (context, index) {..return widget...})

Navigation:

  • A page is called a route in Flutter terminology.
  • Navigator manages a stack of routes.
  • Pushing a route onto Navigator's stack updates the display to that route.
  • Popping a route from Navigator's stack returns the display to the previous route.
Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => Widget()),
);

DART:

  • final - imutable at runtime
  • const - compile time evaluation (static)
  • _ - use prefix underscore on naming to enforce privacy.
  • <ElementType>[] - Strongly typed List
  • {ElementType}[] - Strongly typed Set (no repetitions)

##################################

  • https://pub.dev/ --> Repository for Dart packages
  • pubspec.yaml ------> project dependencies file
  • flutter packages get ---> update dependencies
  • dart pub get --- is implicit in the previous command and updates pubspeck.lock with further dependencies of dependencies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment