Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Examples Flutter code

sho5nn
April 18, 2018

Examples Flutter code

Flutter Documentation
https://flutter.io/docs/

Flutter Cookbook
https://flutter.io/cookbook/

sho5nn

April 18, 2018
Tweet

More Decks by sho5nn

Other Decks in Programming

Transcript

  1. Cookbook • UI • SnackBar, Tab & ViewPager • Images,

    Lists, Animation • Navigation, Forms, Handling Gestures • Networking • Persistence 8
  2. Cookbook • UI • SnackBar, Tab & ViewPager • Images,

    Lists, Animation • Navigation, Forms, Handling Gestures • Networking • Persistence 9
  3. class SnackBarPage extends StatelessWidget { @override Widget build(BuildContext context) {

    return new Center( child: new RaisedButton( onPressed: () { final snackBar = new SnackBar( content: new Text('Yay! A SnackBar!'), action: new SnackBarAction( label: 'Undo', onPressed: () { // Some code to undo the change! }, ), ); // Find the Scaffold in the Widget tree and use it to show a SnackBar! Scaffold.of(context).showSnackBar(snackBar); }, child: new Text('Show SnackBar'), 11
  4. class SnackBarPage extends StatelessWidget { @override Widget build(BuildContext context) {

    return new Center( child: new RaisedButton( onPressed: () { final snackBar = new SnackBar( content: new Text('Yay! A SnackBar!'), action: new SnackBarAction( label: 'Undo', onPressed: () { // Some code to undo the change! }, ), ); // Find the Scaffold in the Widget tree and use it to show a SnackBar! Scaffold.of(context).showSnackBar(snackBar); }, child: new Text('Show SnackBar'), 12
  5. class SnackBarPage extends StatelessWidget { @override Widget build(BuildContext context) {

    return new Center( child: new RaisedButton( onPressed: () { final snackBar = new SnackBar( content: new Text('Yay! A SnackBar!'), action: new SnackBarAction( label: 'Undo', onPressed: () { // Some code to undo the change! }, ), ); // Find the Scaffold in the Widget tree and use it to show a SnackBar! Scaffold.of(context).showSnackBar(snackBar); }, child: new Text('Show SnackBar'), 13
  6. Networking / Fetch data from the internet Add the http

    package to pubspec.yaml dependencies: http: <latest_version> 15
  7. appBar: new AppBar( title: new Text('Fetch Data Example'), ), body:

    new Center( child: new FutureBuilder<Post>( future: fetchPost(), builder: (context, snapshot) { if (snapshot.hasData) { return new Text(snapshot.data.title); } else if (snapshot.hasError) { return new Text("${snapshot.error}"); } // By default, show a loading spinner return new CircularProgressIndicator(); }, 16
  8. appBar: new AppBar( title: new Text('Fetch Data Example'), ), body:

    new Center( child: new FutureBuilder<Post>( future: fetchPost(), // original method, return Future<T> instance builder: (context, snapshot) { if (snapshot.hasData) { return new Text(snapshot.data.title); } else if (snapshot.hasError) { return new Text("${snapshot.error}"); } // By default, show a loading spinner return new CircularProgressIndicator(); }, 17
  9. appBar: new AppBar( title: new Text('Fetch Data Example'), ), body:

    new Center( child: new FutureBuilder<Post>( future: fetchPost(), builder: (context, snapshot) { // lambda if (snapshot.hasData) { return new Text(snapshot.data.title); } else if (snapshot.hasError) { return new Text("${snapshot.error}"); } // By default, show a loading spinner return new CircularProgressIndicator(); }, 18
  10. appBar: new AppBar( title: new Text('Fetch Data Example'), ), body:

    new Center( child: new FutureBuilder<Post>( // extends StatefulWidget future: fetchPost(), builder: (context, snapshot) { if (snapshot.hasData) { return new Text(snapshot.data.title); } else if (snapshot.hasError) { return new Text("${snapshot.error}"); } // By default, show a loading spinner return new CircularProgressIndicator(); }, 19
  11. appBar: new AppBar( title: new Text('Fetch Data Example'), ), body:

    new Center( child: new FutureBuilder<Post>( // extends StatefulWidget future: fetchPost(), builder: (context, snapshot) { if (snapshot.hasData) { return new Text(snapshot.data.title); } else if (snapshot.hasError) { return new Text("${snapshot.error}"); } // By default, show a loading spinner return new CircularProgressIndicator(); }, 20