main.dart 2.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
10
  runApp(const FlutterView());
11 12 13
}

class FlutterView extends StatelessWidget {
14
  const FlutterView({super.key});
15

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

class MyHomePage extends StatefulWidget {
29
  const MyHomePage({super.key});
30

31
  @override
32
  State<MyHomePage> createState() => _MyHomePageState();
33 34 35
}

class _MyHomePageState extends State<MyHomePage> {
36 37 38
  static const String _channel = 'increment';
  static const String _pong = 'pong';
  static const String _emptyMessage = '';
39 40
  static const BasicMessageChannel<String?> platform =
      BasicMessageChannel<String?>(_channel, StringCodec());
41 42 43

  int _counter = 0;

44 45 46 47
  @override
  void initState() {
    super.initState();
    platform.setMessageHandler(_handlePlatformIncrement);
48 49
  }

50
  Future<String> _handlePlatformIncrement(String? message) async {
51 52 53
    setState(() {
      _counter++;
    });
54
    return _emptyMessage;
55 56 57
  }

  void _sendFlutterIncrement() {
58
    platform.send(_pong);
59 60 61 62
  }

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