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

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
11
  runApp(PlatformView());
12 13 14 15 16
}

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

class MyHomePage extends StatefulWidget {
28
  const MyHomePage({Key key, this.title}) : super(key: key);
29 30 31 32

  final String title;

  @override
33
  _MyHomePageState createState() => _MyHomePageState();
34 35 36 37
}

class _MyHomePageState extends State<MyHomePage> {
  static const MethodChannel _methodChannel =
38
      MethodChannel('samples.flutter.io/platform_view');
39 40 41 42 43 44 45 46 47

  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

48
  Future<void> _launchPlatformCount() async {
49
    final int platformCounter =
50
        await _methodChannel.invokeMethod('switchView', _counter);
51 52 53 54 55 56
    setState(() {
      _counter = platformCounter;
    });
  }

  @override
57 58 59
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
60
        ),
61
        body: Column(
62 63
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
64 65 66
            Expanded(
              child: Center(
                child: Column(
67 68
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
69
                    Text(
70 71 72
                      'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
                      style: const TextStyle(fontSize: 17.0),
                    ),
73
                    Padding(
74
                      padding: const EdgeInsets.all(18.0),
75
                      child: RaisedButton(
76 77 78 79 80 81 82 83 84
                          child: Platform.isIOS
                              ? const Text('Continue in iOS view')
                              : const Text('Continue in Android view'),
                          onPressed: _launchPlatformCount),
                    ),
                  ],
                ),
              ),
            ),
85
            Container(
86
              padding: const EdgeInsets.only(bottom: 15.0, left: 5.0),
87
              child: Row(
88
                children: <Widget>[
89
                  Image.asset('assets/flutter-mark-square-64.png',
90 91 92
                      scale: 1.5),
                  const Text(
                    'Flutter',
93
                    style: TextStyle(fontSize: 30.0),
94 95 96 97 98 99
                  ),
                ],
              ),
            ),
          ],
        ),
100
        floatingActionButton: FloatingActionButton(
101 102 103 104 105 106
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      );
}