Cheatography
https://cheatography.com
Flutter App - Mobile Application
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Explanation: Standard hello world app 1/2
void main() => runApp(MyApp()); |
Main method which starts the class MyApp |
class MyApp extends StatelessWidget {...} |
creates class MyApp, the extend StatelessWidget makes the entire app into a widget |
Widget build(BuildContext context) {...} |
builds the widgets in this method |
Add external packages
The pubspec.yaml manages the assets and dependencies of a flutter app |
Example of pubspec.yaml: |
dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.2 english_words: ^3.1.5
|
Get packages: |
At the top you should now be able to get the packages |
After you get the packages there are free to be imported in whatever class you need |
Create a List in flutter
Widget _buildSuggestions() {
return ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: /1/ (context, i) {
if (i.isOdd) return Divider(); /2/
==> The Divider adds visual seperation between two tiles of the list
final index = i ~/ 2; /3/
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10)); /4/
}
return _buildRow(_suggestions[index]);
});
}
|
|
|
Mobile Application: Standard Hello World App 2/2
return MaterialApp( returns an Material Design app
title: 'Welcome to Flutter',
home: Scaffold( creates a Scaffold Widget
appBar: AppBar( creates an AppBar which contains a title
title: Text('Welcome to Flutter'),
),
body: Center( creates a centered body with text in it
child: Text('Hello World'),
),
),
);
|
Add a stateful widget
class RandomWords extends StatefulWidget {
@override
RandomWordsState createState() => RandomWordsState();
}
===> This widget just creates it's state class
class RandomWordsState extends State<RandomWords> {
@override
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return Text(wordPair.asPascalCase);
}
}
==> This class saves and creates new random words
|
Build ListTile in Flutter with Interactivity
Widget _buildRow(WordPair pair) {
final alreadySaved = _saved.contains(pair);
return ListTile(
title: Text( ==> First Text is displayed in a tile
pair.asPascalCase,
style: _biggerFont,
),
trailing: Icon( ==> Adds an Icon to the tile
alreadySaved ? Icons.favorite : Icons.favorite_border,
color: alreadySaved ? Colors.red : null,
),
onTap: () { ==> Adds Interactivity to the tile
setState(() {
if (alreadySaved) {
_saved.remove(pair);
} else {
_saved.add(pair);
}
});
},
);
}
|
|