Skip to content

Instantly share code, notes, and snippets.

@Hixie
Created February 18, 2016 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hixie/6c2527c8fba72d422710 to your computer and use it in GitHub Desktop.
Save Hixie/6c2527c8fba72d422710 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(
new MaterialApp(
title: "Flutter Demo",
routes: <String, RouteBuilder>{
'/': (RouteArguments args) {
return new Scaffold(
toolBar: new ToolBar(
center: new Text('Benchmark')
),
body: new MaterialList(
type: MaterialListType.oneLineWithAvatar,
children: <Widget>[
new ListItem(
onTap: () {
Navigator.pushNamed(args.context, '/const');
},
left: new CircleAvatar(
label: 'C'
),
center: new Text('Using "const"')
),
new ListItem(
onTap: () {
Navigator.pushNamed(args.context, '/new');
},
left: new CircleAvatar(
label: 'N'
),
center: new Text('Using "new"')
)
]
)
);
},
'/const': (RouteArguments args) {
return new Scaffold(
toolBar: new ToolBar(
center: new Text('Using "const"')
),
body: const ConstBenchmark()
);
},
'/new': (RouteArguments args) {
return new Scaffold(
toolBar: new ToolBar(
center: new Text('Using "new"')
),
body: new NewBenchmark()
);
}
}
)
);
}
class ConstBenchmark extends StatefulComponent {
const ConstBenchmark();
State createState() => new ConstBenchmarkState();
}
class NewBenchmark extends StatefulComponent {
NewBenchmark();
State createState() => new NewBenchmarkState();
}
abstract class BenchmarkState extends State {
Widget build(BuildContext context) {
List<Widget> children = <Widget>[];
Stopwatch sw = new Stopwatch();
sw.start();
fillList(children);
sw.stop();
print('$this : ${sw.elapsedMicroseconds}µs');
return new ScrollableGrid(
children: children,
delegate: new MaxTileWidthGridDelegate(
maxTileWidth: 80.0
)
);
}
void fillList(List<Widget> list);
}
class ConstBenchmarkState extends BenchmarkState {
void fillList(List<Widget> list) {
for (int index = 0; index < 1000; index += 1) {
list.add(new Container(
decoration: const BoxDecoration(
backgroundColor: const Color(0xFFFF7F00),
borderRadius: 2.0
),
padding: const EdgeDims.all(1.0),
margin: const EdgeDims.all(1.0),
constraints: const BoxConstraints.expand()
));
}
}
}
class NewBenchmarkState extends BenchmarkState {
void fillList(List<Widget> list) {
for (int index = 0; index < 1000; index += 1) {
list.add(new Container(
decoration: new BoxDecoration(
backgroundColor: new Color(0xFFFF7F00),
borderRadius: 2.0
),
padding: new EdgeDims.all(1.0),
margin: new EdgeDims.all(1.0),
constraints: new BoxConstraints.loose(new Size(2.0, 2.0))
));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment