main.dart.tmpl 5.96 KB
Newer Older
1
import 'package:flutter/material.dart';
2
{{#withPluginHook}}
3 4
import 'dart:async';

5
import 'package:flutter/services.dart';
6 7
import 'package:{{pluginProjectName}}/{{pluginProjectName}}.dart';
{{/withPluginHook}}
8

9
void main() => runApp(const MyApp());
10

11
{{^withPluginHook}}
12
class MyApp extends StatelessWidget {
13 14
  const MyApp({Key? key}) : super(key: key);

15 16 17
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
18
    return MaterialApp(
19
      title: 'Flutter Demo',
20
      theme: ThemeData(
21 22
        // This is the theme of your application.
        //
23 24 25 26
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
27
        // or press Run > Flutter Hot Reload in a Flutter IDE). Notice that the
28
        // counter didn't reset back to zero; the application is not restarted.
29
        primarySwatch: Colors.blue,
30
      ),
31
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
32 33
    );
  }
34 35
}

36
class MyHomePage extends StatefulWidget {
37
  const MyHomePage({Key? key, required this.title}) : super(key: key);
38

39 40 41
  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.
42

43 44 45 46
  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".
47 48 49

  final String title;

50
  @override
51
  State<MyHomePage> createState() => _MyHomePageState();
52 53
}

54
class _MyHomePageState extends State<MyHomePage> {
55 56 57 58
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
59 60 61 62 63
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
64 65 66 67
      _counter++;
    });
  }

68
  @override
69
  Widget build(BuildContext context) {
70 71 72 73 74 75
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
76 77
    return Scaffold(
      appBar: AppBar(
78 79
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
80
        title: Text(widget.title),
81
      ),
82
      body: Center(
83 84
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
85
        child: Column(
86
          // Column is also a layout widget. It takes a list of children and
87 88
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
89
          //
90 91 92 93
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
94
          //
95 96 97 98 99
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
100 101
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
102
            const Text(
103 104
              'You have pushed the button this many times:',
            ),
105
            Text(
106
              '$_counter',
107
              style: Theme.of(context).textTheme.headline4,
108 109
            ),
          ],
110
        ),
111
      ),
112
      floatingActionButton: FloatingActionButton(
Adam Barth's avatar
Adam Barth committed
113 114
        onPressed: _incrementCounter,
        tooltip: 'Increment',
115
        child: const Icon(Icons.add),
116
      ), // This trailing comma makes auto-formatting nicer for build methods.
117 118 119
    );
  }
}
120 121
{{/withPluginHook}}
{{#withPluginHook}}
122
class MyApp extends StatefulWidget {
123 124
  const MyApp({Key? key}) : super(key: key);

125
  @override
126
  State<MyApp> createState() => _MyAppState();
127 128 129
}

class _MyAppState extends State<MyApp> {
130 131 132
  String _platformVersion = 'Unknown';

  @override
133
  void initState() {
134 135 136 137 138
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
139
  Future<void> initPlatformState() async {
140 141
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
142
    // We also handle the message potentially returning null.
143
    try {
144 145
      platformVersion =
          await {{pluginDartClass}}.platformVersion ?? 'Unknown platform version';
146
    } on PlatformException {
147
      platformVersion = 'Failed to get platform version.';
148 149 150 151 152
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
153
    if (!mounted) return;
154 155 156 157 158 159 160 161

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
162 163 164
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
165
          title: const Text('Plugin example app'),
166
        ),
167 168
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
169
        ),
170 171 172 173 174
      ),
    );
  }
}
{{/withPluginHook}}