-
creating an inital flutter app
-
use an external package
-
add a stateful widget
-
create an infinite scrolling ListView
-
add icons to list
-
add interactivity
-
navigate to new screen
-
use UI themes
-
create a new flutter app using
flutter create <app_name>
-
core of app code resides at
./lib/main.dart
creates a material app
main method uses arrow (=>) notation, used for one-line functions
app extends StatelessWidget, making app itself a widget
in flutter, almost everything is a widget, including alignment, padding, and layout
Scaffold widget, provides a default app bar, a title, and a body property that holds the widget tree for the home screen
-
flutter format <filename>
-
can run it using
flutter run
-
can find opensource flutter packages at pub.dev
-
use external package english_words providing few thousands of most used english language words with some utility functions
-
to add an external packages add something like
pubspec.yaml
file
dependencies:
english_words: ^3.1.5
-
to fetch
flutter packages get
-
following changes to MaterialApp for random word as body
+ final wordPair = WordPair.random();
return MaterialApp(
- title: const Text('Fluttr-ing'),
+ title: Text('Fluttr-ing'),
- body: const Center(
- child: const Text('Hey Flocks'),
+ body: Center(
+ child: Text(wordPair.asPascalCase),
-
stateless widgets are immutable
-
stateful widgets require 2 classes;
StatefulWidget
class to create an instance ofState
class -
here we'll add a stateful widget
RandomWords
with state class asRandomWordsState
State<RandomWords>
indicates use of generic State class specialized forRandomWords
state.
RandomWordsState
needs a build method to generate the data
-
expand
RandomWordsState
to provide a list of word-pairs, growing list inifinitely as user scrolls -
ListView
widgetbuilder
factory constructor allows you to build a lazy list -
prefix
underscore
to identifier in Dart enforces privacy -
_buildSuggestion
usesListView
provideditemBuilder
, a factory builder and callback function specified as an anon function
itemBuilder called once per suggested word-pairing
add one-pixel-high divider widget before each row, every odd row
as items are half the list rows,
i ~/ 2
divides by 2 returning int in Dart; adding 10 more items everytime current list is over
- we'll add more interactivity, route navigation and modify theme
-
add
_favorite
Set to RandomWordsState to store favorited word-pairs -
add already favorited check in
_buildRow
by usingcontains
on Set -
add a trailing favorite icon by modifying
ListTile
returned by_buildRow
- make favorite action working by calling
setState()
from_buildRow
atonTap
event
-
in Flutter, navigator manages stack containing app's routes and routing gets managed via push/pop
-
add a list icon to AppBar widget for
onPressed
action calling_pushFavorited
-
_pushFavorited
will push the route to Navigator stack, it provides scaffold to builder via_favoriteScaffold
-
can use device dependent theme or a brand custom theme by configuring
ThemeData
class -
customize app's primary color to red (also chang favorite color to orange to match)