loader_app.dart 3.54 KB
Newer Older
1 2
import 'dart:async';

3 4
import 'package:flutter/material.dart';

5 6 7 8 9 10 11
String message = 'Flutter Debug Loader';
String explanation = 'Please stand by...';
double progress = 0.0;
double progressMax = 0.0;
StateSetter setState = (VoidCallback fn) => fn();
Timer connectionTimeout;

12
void main() {
13 14 15 16 17
  new LoaderBinding();
  runApp(
    new MaterialApp(
      title: 'Flutter Debug Loader',
      debugShowCheckedModeBanner: false,
18
      home: new Scaffold(
19 20 21 22 23
        body: new StatefulBuilder(
          builder: (BuildContext context, StateSetter setStateRef) {
            setState = setStateRef;
            return new Column(
              children: <Widget>[
24
                new Expanded(
25
                  child: new Center(
26
                    child: const FlutterLogo(size: 100.0),
27
                  ),
28
                ),
29
                new Expanded(
30 31 32 33 34
                  child: new Builder(
                    builder: (BuildContext context) {
                      List<Widget> children = <Widget>[];
                      children.add(new Text(
                        message,
35
                        style: const TextStyle(fontSize: 24.0),
36 37 38
                        textAlign: TextAlign.center
                      ));
                      if (progressMax >= 0.0) {
39
                        children.add(const SizedBox(height: 18.0));
40 41 42
                        children.add(new Center(child: new CircularProgressIndicator(value: progressMax > 0 ? progress / progressMax : null)));
                      }
                      return new Block(children: children);
43 44
                    },
                  ),
45
                ),
46
                new Expanded(
47
                  child: new Block(
48
                    padding: const EdgeInsets.symmetric(horizontal: 24.0),
49
                    children: <Widget>[ new Text(explanation, textAlign: TextAlign.center) ]
50
                  ),
51
                ),
52
              ],
53
            );
54 55 56 57
          },
        ),
      ),
    ),
58
  );
59 60 61 62 63 64 65 66 67 68 69
  connectionTimeout = new Timer(const Duration(seconds: 8), () {
    setState(() {
      explanation =
        'This is a hot-reload-enabled debug-mode Flutter application. '
        'To launch this application, please use the "flutter run" command. '
        'To be able to launch a Flutter application in debug mode from the '
        'device, please use "flutter run --no-hot". To install a release '
        'mode build of this application on your device, use "flutter install".';
      progressMax = -1.0;
    });
  });
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
class LoaderBinding extends WidgetsFlutterBinding {
  @override
  void initServiceExtensions() {
    super.initServiceExtensions();
    registerStringServiceExtension(
      name: 'loaderShowMessage',
      getter: () => message,
      setter: (String value) {
        connectionTimeout?.cancel();
        connectionTimeout = null;
        setState(() {
          message = value;
        });
      }
    );
87 88 89 90 91 92 93 94 95 96 97
    registerStringServiceExtension(
      name: 'loaderShowExplanation',
      getter: () => explanation,
      setter: (String value) {
        connectionTimeout?.cancel();
        connectionTimeout = null;
        setState(() {
          explanation = value;
        });
      }
    );
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    registerNumericServiceExtension(
      name: 'loaderSetProgress',
      getter: () => progress,
      setter: (double value) {
        setState(() {
          progress = value;
        });
      }
    );
    registerNumericServiceExtension(
      name: 'loaderSetProgressMax',
      getter: () => progressMax,
      setter: (double value) {
        setState(() {
          progressMax = value;
        });
      }
    );
  }
}