main.dart 2.98 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(const PlatformView());
12 13 14
}

class PlatformView extends StatelessWidget {
15
  const PlatformView({super.key});
16

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

class MyHomePage extends StatefulWidget {
30
  const MyHomePage({super.key, required this.title});
31 32 33 34

  final String title;

  @override
35
  State<MyHomePage> createState() => _MyHomePageState();
36 37 38 39
}

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

  int _counter = 0;

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

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

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