main.dart.tmpl 6.11 KB
Newer Older
1
import 'package:flutter/material.dart';
2
{{#withDriverTest}}
3
import 'package:flutter_driver/driver_extension.dart';
4
{{/withDriverTest}}
5
{{#withPluginHook}}
6 7
import 'dart:async';

8
import 'package:flutter/services.dart';
9 10
import 'package:{{pluginProjectName}}/{{pluginProjectName}}.dart';
{{/withPluginHook}}
11

12
{{^withDriverTest}}
13
void main() => runApp(MyApp());
14
{{/withDriverTest}}
15
{{#withDriverTest}}
16
void main() {
17
  // Enable integration testing with the Flutter Driver extension.
18
  // See https://flutter.dev/testing/ for more info.
19
  enableFlutterDriverExtension();
20
  runApp(MyApp());
21
}
22
{{/withDriverTest}}
23

24
{{^withPluginHook}}
25 26 27 28
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
29
    return MaterialApp(
30
      title: 'Flutter Demo',
31
      theme: ThemeData(
32 33
        // This is the theme of your application.
        //
34 35 36 37
        // 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",
38 39 40
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
41
        primarySwatch: Colors.blue,
42
      ),
43
      home: MyHomePage(title: 'Flutter Demo Home Page'),
44 45
    );
  }
46 47
}

48
class MyHomePage extends StatefulWidget {
49
  MyHomePage({Key key, this.title}) : super(key: key);
50

51 52 53
  // 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.
54

55 56 57 58
  // 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".
59 60 61

  final String title;

62
  @override
63
  _MyHomePageState createState() => _MyHomePageState();
64 65
}

66
class _MyHomePageState extends State<MyHomePage> {
67 68 69 70
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
71 72 73 74 75
      // 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.
76 77 78 79
      _counter++;
    });
  }

80
  @override
81
  Widget build(BuildContext context) {
82 83 84 85 86 87
    // 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.
88 89
    return Scaffold(
      appBar: AppBar(
90 91
        // 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.
92
        title: Text(widget.title),
93
      ),
94
      body: Center(
95 96
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
97
        child: Column(
98
          // Column is also a layout widget. It takes a list of children and
99 100
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
101
          //
102 103 104
          // 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)
105
          // to see the wireframe for each widget.
106
          //
107 108 109 110 111
          // 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).
112 113
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
114
            Text(
115 116
              'You have pushed the button this many times:',
            ),
117
            Text(
118
              '$_counter',
119 120 121
              style: Theme.of(context).textTheme.display1,
            ),
          ],
122
        ),
123
      ),
124
      floatingActionButton: FloatingActionButton(
Adam Barth's avatar
Adam Barth committed
125 126
        onPressed: _incrementCounter,
        tooltip: 'Increment',
127
        child: Icon(Icons.add),
128
      ), // This trailing comma makes auto-formatting nicer for build methods.
129 130 131
    );
  }
}
132 133
{{/withPluginHook}}
{{#withPluginHook}}
134 135
class MyApp extends StatefulWidget {
  @override
136
  _MyAppState createState() => _MyAppState();
137 138 139
}

class _MyAppState extends State<MyApp> {
140 141 142
  String _platformVersion = 'Unknown';

  @override
143
  void initState() {
144 145 146 147 148
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
149
  Future<void> initPlatformState() async {
150 151 152 153 154
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await {{pluginDartClass}}.platformVersion;
    } on PlatformException {
155
      platformVersion = 'Failed to get platform version.';
156 157 158 159 160
    }

    // 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.
161
    if (!mounted) return;
162 163 164 165 166 167 168 169

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

  @override
  Widget build(BuildContext context) {
170 171 172
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
173
          title: const Text('Plugin example app'),
174
        ),
175 176
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
177
        ),
178 179 180 181 182
      ),
    );
  }
}
{{/withPluginHook}}