main.dart 3.43 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

import 'dart:async';
6
import 'package:flutter/foundation.dart';
7 8 9 10
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 51 52 53 54 55 56
  static Widget get _buttonText {
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        return const Text('Continue in Android view');
      case TargetPlatform.iOS:
        return const Text('Continue in iOS view');
      case TargetPlatform.windows:
57 58 59
        return const Text('Continue in Windows view');
      case TargetPlatform.macOS:
        return const Text('Continue in macOS view');
60 61 62 63 64 65
      case TargetPlatform.fuchsia:
      case TargetPlatform.linux:
        throw UnimplementedError('Platform not yet implemented');
    }
  }

66
  Future<void> _launchPlatformCount() async {
67
    final int? platformCounter =
68
        await _methodChannel.invokeMethod('switchView', _counter);
69
    setState(() {
70
      _counter = platformCounter!;
71 72 73 74
    });
  }

  @override
75 76 77
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
78
        ),
79
        body: Column(
80 81
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
82 83 84
            Expanded(
              child: Center(
                child: Column(
85 86
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
87
                    Text(
88 89 90
                      'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
                      style: const TextStyle(fontSize: 17.0),
                    ),
91
                    Padding(
92
                      padding: const EdgeInsets.all(18.0),
93
                      child: ElevatedButton(
94
                        onPressed: _launchPlatformCount,
95
                        child: _buttonText,
96
                      ),
97 98 99 100 101
                    ),
                  ],
                ),
              ),
            ),
102
            Container(
103
              padding: const EdgeInsets.only(bottom: 15.0, left: 5.0),
104
              child: Row(
105
                children: <Widget>[
106
                  Image.asset('assets/flutter-mark-square-64.png', scale: 1.5),
107 108
                  const Text(
                    'Flutter',
109
                    style: TextStyle(fontSize: 30.0),
110 111 112 113 114 115
                  ),
                ],
              ),
            ),
          ],
        ),
116
        floatingActionButton: FloatingActionButton(
117 118 119 120 121 122
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      );
}