loader_app.dart 3.23 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        body: new StatefulBuilder(
          builder: (BuildContext context, StateSetter setStateRef) {
            setState = setStateRef;
            return new Column(
              children: <Widget>[
                new Flexible(
                  child: new Container() // TODO(ianh): replace this with our logo in a Center box
                ),
                new Flexible(
                  child: new Builder(
                    builder: (BuildContext context) {
                      List<Widget> children = <Widget>[];
                      children.add(new Text(
                        message,
                        style: new TextStyle(fontSize: 24.0),
                        textAlign: TextAlign.center
                      ));
                      if (progressMax >= 0.0) {
                        children.add(new SizedBox(height: 18.0));
                        children.add(new Center(child: new CircularProgressIndicator(value: progressMax > 0 ? progress / progressMax : null)));
                      }
                      return new Block(children: children);
                    }
                  )
                ),
                new Flexible(
                  child: new Block(
                    padding: new EdgeInsets.symmetric(horizontal: 16.0),
                    children: <Widget>[ new Text(explanation, textAlign: TextAlign.center) ]
                  )
                ),
              ]
            );
          }
53 54 55 56
        )
      )
    )
  );
57 58 59 60 61 62 63 64 65 66 67
  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;
    });
  });
68 69
}

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