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

BuildContextについて調べてみた / Flutter Meetup Tokyo #2

BuildContextについて調べてみた / Flutter Meetup Tokyo #2

star_zero

May 31, 2018
Tweet

More Decks by star_zero

Other Decks in Programming

Transcript

  1. BuildContext class MyApp extends StatelessWidget { @override Widget build(BuildContext context)

    { return new MaterialApp( title: 'Flutter Demo', theme: new ThemeData( primarySwatch: Colors.blue, ), home: new MyHomePage(title: 'Flutter Demo'), ); } }
  2. BuildContext • WidgetツリーでのWidgetの位置 • StatelessWidget.buildやState.buildメソッド に渡される • WidgetごとにBuildContextを持っていて、 buildメソッドで返されるWidgetの親になる (?)

    • buildメソッドのBuildContextとbuildメソッド が返すBuildContextが同じではない(?) • (英語怪しいので間違ってるかも)
  3. うごく class SnackBarParent extends StatelessWidget { @override Widget build(BuildContext context)

    { return new MaterialApp( home: new Scaffold( appBar: new AppBar( title: new Text('SnackBar'), ), body: new SnackBarChild(), ), ); } }
  4. うごく class SnackBarChild extends StatelessWidget { @override Widget build(BuildContext context)

    { return new RaisedButton( onPressed: () { var snackbar = new SnackBar(content: new Text('Hello!!')); Scaffold.of(context).showSnackBar(snackbar); }, child: new Text('Show SnackBar'), ); } }
  5. うごかない class SnackBarChild extends StatelessWidget { @override Widget build(BuildContext context)

    { return new Scaffold( appBar: new AppBar( title: new Text('SnackBar'), ), body: new RaisedButton( onPressed: () { var snackbar = new SnackBar( content: new Text('Hello!!')); Scaffold.of(context).showSnackBar(snackbar); }, child: new Text('Show SnackBar'), ), ); } }
  6. Builder class SnackBarChild extends StatelessWidget { @override Widget build(BuildContext context)

    { return new Scaffold( body: new Builder(builder: (BuildContext context) { return new RaisedButton( onPressed: () { var snackbar = new SnackBar( content: new Text('Hello!!')); Scaffold.of(context).showSnackBar(snackbar); }, child: new Text('Show SnackBar'), ); }), ); } }
  7. GlobalKey class SnackBarChild extends StatelessWidget { final _scaffoldKey = GlobalKey<ScaffoldState>();

    @override Widget build(BuildContext context) { return new Scaffold( key: _scaffoldKey, body: new RaisedButton( onPressed: () { var snackbar = new SnackBar( content: new Text('Hello!!')); _scaffoldKey.currentState.showSnackBar(snackbar); }, child: new Text('Show SnackBar'), ), ); } }