• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6import 'dart:io';
7import 'dart:typed_data';
8import 'package:flutter/material.dart';
9import 'package:flutter/services.dart' show rootBundle;
10import 'package:flutter/services.dart';
11
12/// An example that sets up local http server for serving single
13/// image, creates single flutter widget with five copies of requested
14/// image and prints how long the loading took.
15///
16/// This is used in [$FH/flutter/devicelab/bin/tasks/image_list_reported_duration.dart] test.
17Future<void> main() async {
18  final HttpServer httpServer =
19      await HttpServer.bind(InternetAddress.anyIPv6, 0);
20  final int port = httpServer.port;
21  print('Listening on port $port.');
22
23  final ByteData byteData = await rootBundle.load('images/coast.jpg');
24  final Uint8List bytes = byteData.buffer
25      .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes);
26  httpServer.listen((HttpRequest request) async {
27    request.response.add(bytes);
28    request.response.close();
29  });
30
31  runApp(MyApp(port));
32}
33
34const int IMAGES = 50;
35
36@immutable
37class MyApp extends StatelessWidget {
38  const MyApp(this.port);
39
40  final int port;
41
42  @override
43  Widget build(BuildContext context) {
44    return MaterialApp(
45      title: 'Flutter Demo',
46      theme: ThemeData(
47        primarySwatch: Colors.blue,
48      ),
49      home: MyHomePage(title: 'Flutter Demo Home Page', port: port),
50    );
51  }
52}
53
54class MyHomePage extends StatefulWidget {
55  const MyHomePage({Key key, this.title, this.port}) : super(key: key);
56  final String title;
57  final int port;
58
59  @override
60  _MyHomePageState createState() => _MyHomePageState();
61}
62
63class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
64  int _counter = 0;
65
66  void _incrementCounter() {
67    setState(() {
68      _counter++;
69    });
70  }
71
72  Widget createImage(final int index, final Completer<bool> completer) {
73    return Image.network(
74        'http://localhost:${widget.port}/${_counter * IMAGES + index}',
75        frameBuilder: (
76          BuildContext context,
77          Widget child,
78          int frame,
79          bool wasSynchronouslyLoaded,
80        ) {
81          if (frame == 0 && !completer.isCompleted) {
82            completer.complete(true);
83          }
84          return child;
85        },
86    );
87  }
88
89  @override
90  Widget build(BuildContext context) {
91    final List<AnimationController> controllers = List<AnimationController>(IMAGES);
92    for (int i = 0; i < IMAGES; i++) {
93      controllers[i] = AnimationController(
94        duration: const Duration(milliseconds: 3600),
95        vsync: this,
96      )..repeat();
97    }
98    final List<Completer<bool>> completers = List<Completer<bool>>(IMAGES);
99    for (int i = 0; i < IMAGES; i++) {
100      completers[i] = Completer<bool>();
101    }
102    final List<Future<bool>> futures = completers.map(
103        (Completer<bool> completer) => completer.future).toList();
104    final DateTime started = DateTime.now();
105    Future.wait(futures).then((_) {
106      print(
107          '===image_list=== all loaded in ${DateTime.now().difference(started).inMilliseconds}ms.');
108    });
109    return Scaffold(
110      appBar: AppBar(
111        title: Text(widget.title),
112      ),
113      body: Center(
114        child: Column(
115          mainAxisAlignment: MainAxisAlignment.center,
116          children: <Widget>[
117            Row(children: createImageList(IMAGES, completers, controllers)),
118            const Text(
119              'You have pushed the button this many times:',
120            ),
121            Text(
122              '$_counter',
123              style: Theme.of(context).textTheme.display1,
124            ),
125          ],
126        ),
127      ),
128      floatingActionButton: FloatingActionButton(
129        onPressed: _incrementCounter,
130        tooltip: 'Increment',
131        child: Icon(Icons.add),
132      ),
133    );
134  }
135
136  List<Widget> createImageList(int count, List<Completer<bool>> completers,
137      List<AnimationController> controllers) {
138    final List<Widget> list = <Widget>[];
139    for (int i = 0; i < count; i++) {
140      list.add(Flexible(
141          fit: FlexFit.tight,
142          flex: i + 1,
143          child: RotationTransition(
144              turns: controllers[i],
145              child: createImage(i + 1, completers[i]))));
146    }
147    return list;
148  }
149}
150