Flutter Cheat Sheet
Flutter Cheat Sheet
https://howtodothisinflutter.com/#table-of-contents 1/18
10/26/22, 7:34 AM Flutter cheat sheet
Table of contents
⏫ ToC
Init
Healthcheck
Hello World
Stateless Widget
Required and default props
Stateful Widget
Combining props and state
Lifecycle hooks
Android Ink effect
Detecting Gestures
Loading indicator
Platform specific code
Hide status bar
Lock orientation
Show alert
Check if dev
Navigation
Arrays
Make http request
Async Await
JSON
Singleton
Debounce
Init
https://howtodothisinflutter.com/#table-of-contents 2/18
10/26/22, 7:34 AM Flutter cheat sheet
Healthcheck
⏫ ToC
flutter doctor
Hello World
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
return MaterialApp(
home: Scaffold(
body: Center(
),
),
);
Stateless Widget
import 'package:flutter/material.dart';
https://howtodothisinflutter.com/#table-of-contents 3/18
10/26/22, 7:34 AM Flutter cheat sheet
⏫ ToC
@override
return Container(
);
import 'package:flutter/material.dart';
SomeComponent({
@required this.foo,
});
@override
return Container(
);
https://howtodothisinflutter.com/#table-of-contents 4/18
10/26/22, 7:34 AM Flutter cheat sheet
Stateful Widget
⏫ ToC
import 'package:flutter/material.dart';
@override
int counter = 0;
increment() {
setState(() {
counter++;
});
decrement() {
setState(() {
counter--;
});
@override
return Row(
children: <Widget>[
],
);
https://howtodothisinflutter.com/#table-of-contents 5/18
10/26/22, 7:34 AM Flutter cheat sheet
import 'package:flutter/material.dart';
SomeWidget({@required this.fruit});
@override
int count = 0;
@override
return Container(
);
@override
return Container(
);
https://howtodothisinflutter.com/#table-of-contents 6/18
10/26/22, 7:34 AM Flutter cheat sheet
Lifecycle hooks
⏫ ToC
class _MyComponentState extends State<MyComponent> {
@override
void initState() {
super.initState();
@override
super.didUpdateWidget(oldWidget);
@override didChangeDependencies() {
@override
void dispose() {
super.dispose();
InkWell(
child: Text('Button'),
onTap: _onTap,
https://howtodothisinflutter.com/#table-of-contents 7/18
10/26/22, 7:34 AM Flutter cheat sheet
onLongPress: _onLongPress,
onDoubleTap: _onDoubleTap,
onTapCancel: _onTapCancel,
⏫ ToC
);
Detecting Gestures
GestureDetector(
onTap: _onTap,
onLongPress: _onLongPress,
child: Text('Button'),
);
Loading indicator
@override
Future future;
@override
void initState() {
super.initState();
@override
return FutureBuilder(
future: future,
https://howtodothisinflutter.com/#table-of-contents 8/18
10/26/22, 7:34 AM Flutter cheat sheet
? Text('Loaded')
⏫ ToC
: CircularProgressIndicator();
},
);
if (Platform.isIOS) {
doSmthIOSSpecific();
if (Platform.isAndroid) {
doSmthAndroidSpecific();
import 'package:flutter/services.dart';
void main() {
SystemChrome.setEnabledSystemUIOverlays([]);
Lock orientation
https://howtodothisinflutter.com/#table-of-contents 9/18
10/26/22, 7:34 AM Flutter cheat sheet
import 'package:flutter/services.dart';
]);
runApp(App());
Show alert
showDialog<void>(
context: context,
barrierDismissible: false,
return AlertDialog(
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text('Cancel'),
onPressed: () {
print('Cancel pressed');
Navigator.of(context).pop();
},
),
FlatButton(
https://howtodothisinflutter.com/#table-of-contents 10/18
10/26/22, 7:34 AM Flutter cheat sheet
child: Text('OK'),
onPressed: () {
print('OK pressed');
⏫ ToC
Navigator.of(context).pop();
},
),
],
);
},
);
Check if dev
assert(isDev == true);
if (isDev) {
doSmth();
Navigation
import 'package:flutter/material.dart';
@override
return Center(
child: RaisedButton(
),
);
https://howtodothisinflutter.com/#table-of-contents 11/18
10/26/22, 7:34 AM Flutter cheat sheet
⏫ ToC
class SecondScreen extends StatelessWidget {
void _pushSecondScreen(context) {
@override
return Column(
children: <Widget>[
RaisedButton(
),
RaisedButton(
),
],
);
void main() {
runApp(MaterialApp(
initialRoute: '/',
routes: {
},
));
https://howtodothisinflutter.com/#table-of-contents 12/18
10/26/22, 7:34 AM Flutter cheat sheet
Arrays
⏫ ToC
final length = items.length;
items.add(42);
https://howtodothisinflutter.com/#table-of-contents 13/18
10/26/22, 7:34 AM Flutter cheat sheet
acc[item.id] = item;
return acc;
⏫ ToC
});
items.removeAt(0);
items.insert(0, 42);
dependencies:
http: ^0.12.0
http.get(API_URL).then((http.Response res) {
});
https://howtodothisinflutter.com/#table-of-contents 14/18
10/26/22, 7:34 AM Flutter cheat sheet
Async Await
⏫ ToC
Future<int> doSmthAsync() async {
return result;
class SomeClass {
method() async {
return result;
JSON
json.decode(someString);
json.encode(encodableObject);
You should describe each entity as a Dart class with fromJson and toJson
methods
class User {
String displayName;
String photoUrl;
User({this.displayName, this.photoUrl});
: displayName = json['displayName'],
https://howtodothisinflutter.com/#table-of-contents 15/18
10/26/22, 7:34 AM Flutter cheat sheet
photoUrl = json['photoUrl'];
⏫ ToC
return {
'displayName': displayName,
'photoUrl': photoUrl,
};
json.encode(user.toJson());
However this approach is error-prone (e.g. you can forget to update map key
after class field was renamed), so you can use json_serializable as an
alternative
dependencies:
json_annotation: ^2.0.0
dev_dependencies:
build_runner: ^1.0.0
json_serializable: ^2.0.0
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable()
class User {
String displayName;
String photoUrl;
https://howtodothisinflutter.com/#table-of-contents 16/18
10/26/22, 7:34 AM Flutter cheat sheet
User({this.displayName this.photoUrl});
⏫ ToC
// _$UserFromJson is generated and available in user.g.dart
return _$UserFromJson(json);
To watch for changes run flutter packages pub run build_runner watch
Singleton
class Singleton {
Singleton._internal()
: prop = 42;
https://howtodothisinflutter.com/#table-of-contents 17/18
10/26/22, 7:34 AM Flutter cheat sheet
Debounce
⏫ ToC
Timer _debounce;
someFN();
});
MIT © Lesnitsky
https://howtodothisinflutter.com/#table-of-contents 18/18