Show Menu
Cheatography

MobileApplication2 Cheat Sheet (DRAFT) by [deleted]

new phone who dis nvs

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Stateful Widget

A stateful widget is dynamic:it can change its appearance in response to events triggered by user intera­ctions. Checkbox, Form, and TextField are examples of stateful widgets. Stateful widgets subclass Statef­ulW­idget.

Stateless

A stateless widget never changes. Icon, IconBu­tton, and Text are examples of stateless widgets. Stateless widgets subclass Statel­ess­Widget.

Managing State

class TapboxA extends StatefulWidget {
  TapboxA({Key key}) : super(key: key);

  @override
  _TapboxAState createState() => _TapboxAState();
}

class _TapboxAState extends State<TapboxA> {
  bool _active = false;

  void _handleTap() {
    setState(() {
      _active = !_active;
    });
  }
Widget manages own state
 

Example Stateful Implem­ent­ation

class _FavoriteWidgetState extends State<FavoriteWidget> {
  // ยทยทยท
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          padding: EdgeInsets.all(0),
          child: IconButton(
            icon: (_isFavorited ? Icon(Icons.star) : Icon(Icons.star_border)),
            color: Colors.red[500],
            onPressed: _toggleFavorite,
          ),
        ),
        SizedBox(
          width: 18,
          child: Container(
            child: Text('$_favoriteCount'),
          ),
        ),

Managing State

lass ParentWidget extends StatefulWidget {
  @override
  _ParentWidgetState createState() => _ParentWidgetState();
}

class _ParentWidgetState extends State<ParentWidget> {
  bool _active = false;

  void _handleTapboxChanged(bool newValue) {
    setState(() {
      _active = newValue;
    });
  }
Parent widget manages state
 

Navigation

Push new route
Naviga­tor.push(     context,     Materi­alP­age­Rou­te(­bui­lder: (context) => Second­Rou­te()),   ); }
Return to old route
onPressed: () {   Naviga­tor.po­p(c­ont­ext); }
Define routes
  routes: {     '/': (context) => FirstS­cre­en(),     '/second': (context) => Second­Scr­een(),   },
Push data to new screen
        Naviga­tor.push(           context,           Materi­alP­age­Route(             builder: (context) => Detail­Scr­een­(todo: todos[­ind­ex]),           ),
Return data from screen
Raised­Button(   onPressed: () {     // The Yep button returns "­Yep­!" as the result.     Naviga­tor.po­p(c­ontext, 'Yep!');   },   child: Text('­Yep!'), );