main.dart 2.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(new FlutterView());
}

class FlutterView extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter View',
      theme: new ThemeData(
        primarySwatch: Colors.grey,
      ),
18
      home: new MyHomePage(),
19 20 21 22 23 24 25 26 27 28 29
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const String _channel = "increment";
30
  static const String _pong = "pong";
31
  static const String _emptyMessage = "";
32 33
  static const BasicMessageChannel<String> platform =
      const BasicMessageChannel<String>(_channel, const StringCodec());
34 35 36

  int _counter = 0;

37 38 39 40
  @override
  void initState() {
    super.initState();
    platform.setMessageHandler(_handlePlatformIncrement);
41 42
  }

43
  Future<String> _handlePlatformIncrement(String message) async {
44 45 46
    setState(() {
      _counter++;
    });
47
    return _emptyMessage;
48 49 50
  }

  void _sendFlutterIncrement() {
51
    platform.send(_pong);
52 53 54 55 56 57 58 59 60 61 62 63
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          new Expanded(
            child: new Center(
              child: new Text(
                'Platform button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
64
                style: const TextStyle(fontSize: 17.0))
65 66 67 68 69 70 71
            ),
          ),
          new Container(
            padding: const EdgeInsets.only(bottom: 15.0, left: 5.0),
            child: new Row(
              children: <Widget>[
                new Image.asset('assets/flutter-mark-square-64.png', scale: 1.5),
72
                const Text('Flutter', style: const TextStyle(fontSize: 30.0)),
73 74 75 76 77 78 79
              ],
            ),
          ),
        ],
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _sendFlutterIncrement,
80
        child: const Icon(Icons.add),
81 82 83 84
      ),
    );
  }
}