Created
July 4, 2018 06:26
-
-
Save mravn-google/6cef37896ce913826e9801d5f445458b to your computer and use it in GitHub Desktop.
Comparison of inside-out vs outside-in widget tree building
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
Column buildButtonColumn(IconData icon, String label) { | |
final Color color = Theme.of(context).primaryColor; | |
final Widget text = Text( | |
label, | |
style: TextStyle( | |
fontSize: 12.0, | |
fontWeight: FontWeight.w400, | |
color: color, | |
), | |
); | |
return Column( | |
mainAxisSize: MainAxisSize.min, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Icon(icon, color: color), | |
Container(margin: const EdgeInsets.only(top: 8.0), child: text), | |
], | |
); | |
} | |
final Widget image = Image.asset( | |
'images/lake.jpg', | |
width: 600.0, | |
height: 240.0, | |
fit: BoxFit.cover, | |
); | |
final Widget title = Container( | |
padding: const EdgeInsets.only(bottom: 8.0), | |
child: Text( | |
'Oeschinen Lake Campground', | |
style: TextStyle(fontWeight: FontWeight.bold), | |
), | |
); | |
final Widget subtitle = Text( | |
'Kandersteg, Switzerland', | |
style: TextStyle(color: Colors.grey[500]), | |
); | |
final Widget titles = Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
title, | |
subtitle, | |
], | |
); | |
final Widget titleSection = Container( | |
padding: const EdgeInsets.all(32.0), | |
child: Row(children: <Widget>[ | |
Expanded(child: titles), | |
Icon(Icons.star, color: Colors.red[500]), | |
Text('41'), | |
]), | |
); | |
final Widget buttonSection = Container( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
buildButtonColumn(Icons.call, 'CALL'), | |
buildButtonColumn(Icons.near_me, 'ROUTE'), | |
buildButtonColumn(Icons.share, 'SHARE'), | |
], | |
), | |
); | |
final Widget textSection = Container( | |
padding: const EdgeInsets.all(32.0), | |
child: Text( | |
'Lake Oeschinen lies at the foot of the Blüemlisalp...', | |
softWrap: true, | |
), | |
); | |
final Widget body = ListView(children: <Widget>[ | |
image, | |
titleSection, | |
buttonSection, | |
textSection, | |
]); | |
final Widget scaffold = Scaffold( | |
appBar: AppBar(title: Text('Top Lakes')), | |
body: body, | |
); | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: scaffold, | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
Column buildButtonColumn(IconData icon, String label) { | |
Color color = Theme.of(context).primaryColor; | |
return Column( | |
mainAxisSize: MainAxisSize.min, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Icon(icon, color: color), | |
Container( | |
margin: const EdgeInsets.only(top: 8.0), | |
child: Text( | |
label, | |
style: TextStyle( | |
fontSize: 12.0, | |
fontWeight: FontWeight.w400, | |
color: color, | |
), | |
), | |
), | |
], | |
); | |
} | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold( | |
appBar: AppBar(title: Text('Top Lakes')), | |
body: ListView( | |
children: <Widget>[ | |
Image.asset( | |
'images/lake.jpg', | |
width: 600.0, | |
height: 240.0, | |
fit: BoxFit.cover, | |
), | |
Container( | |
padding: const EdgeInsets.all(32.0), | |
child: Row( | |
children: <Widget>[ | |
Expanded( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Container( | |
padding: const EdgeInsets.only(bottom: 8.0), | |
child: Text( | |
'Oeschinen Lake Campground', | |
style: TextStyle(fontWeight: FontWeight.bold), | |
), | |
), | |
Text( | |
'Kandersteg, Switzerland', | |
style: TextStyle(color: Colors.grey[500]), | |
), | |
], | |
), | |
), | |
Icon(Icons.star, color: Colors.red[500]), | |
Text('41'), | |
], | |
), | |
), | |
Container( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
buildButtonColumn(Icons.call, 'CALL'), | |
buildButtonColumn(Icons.near_me, 'ROUTE'), | |
buildButtonColumn(Icons.share, 'SHARE'), | |
], | |
), | |
), | |
Container( | |
padding: const EdgeInsets.all(32.0), | |
child: Text( | |
'Lake Oeschinen lies at the foot of the Blüemlisalp...', | |
softWrap: true, | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment