main.dart.tmpl 9.06 KB
Newer Older
1
import 'package:flutter/material.dart';
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
{{#withEmptyMain}}

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}
{{/withEmptyMain}}
{{^withEmptyMain}}
Daco Harkes's avatar
Daco Harkes committed
24
{{#withPlatformChannelPluginHook}}
25 26
import 'dart:async';

27
import 'package:flutter/services.dart';
28
import 'package:{{pluginProjectName}}/{{pluginProjectName}}.dart';
Daco Harkes's avatar
Daco Harkes committed
29
{{/withPlatformChannelPluginHook}}
30
{{#withFfi}}
Daco Harkes's avatar
Daco Harkes committed
31 32 33
import 'dart:async';

import 'package:{{pluginProjectName}}/{{pluginProjectName}}.dart' as {{pluginProjectName}};
34
{{/withFfi}}
35

36
void main() {
37
  runApp(const MyApp());
38 39
}

40
{{^withPluginHook}}
41
class MyApp extends StatelessWidget {
42
  const MyApp({super.key});
43

44 45 46
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
47
    return MaterialApp(
48
      title: 'Flutter Demo',
49
      theme: ThemeData(
50 51
        // This is the theme of your application.
        //
52
        // TRY THIS: Try running your application with "flutter run". You'll see
53
        // the application has a purple toolbar. Then, without quitting the app,
54 55 56 57 58
        // try changing the seedColor in the colorScheme below to Colors.green
        // and then invoke "hot reload" (save your changes or press the "hot
        // reload" button in a Flutter-supported IDE, or press "r" if you used
        // the command line to start the app).
        //
59
        // Notice that the counter didn't reset back to zero; the application
60 61 62 63 64 65 66
        // state is not lost during the reload. To reset the state, use hot
        // restart instead.
        //
        // This works for code too, not just values: Most code changes can be
        // tested with just a hot reload.
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
67
      ),
68
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
69 70
    );
  }
71 72
}

73
class MyHomePage extends StatefulWidget {
74
  const MyHomePage({super.key, required this.title});
75

76 77 78
  // 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.
79

80 81 82 83
  // 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".
84 85 86

  final String title;

87
  @override
88
  State<MyHomePage> createState() => _MyHomePageState();
89 90
}

91
class _MyHomePageState extends State<MyHomePage> {
92 93 94 95
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
96 97 98 99 100
      // 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.
101 102 103 104
      _counter++;
    });
  }

105
  @override
106
  Widget build(BuildContext context) {
107 108 109 110 111 112
    // 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.
113 114
    return Scaffold(
      appBar: AppBar(
115 116 117 118
        // TRY THIS: Try changing the color here to a specific color (to
        // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
        // change color while the other colors stay the same.
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
119 120
        // 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.
121
        title: Text(widget.title),
122
      ),
123
      body: Center(
124 125
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
126
        child: Column(
127
          // Column is also a layout widget. It takes a list of children and
128 129
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
130
          //
131 132 133 134 135
          // 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).
136 137 138 139
          //
          // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
          // action in the IDE, or press "p" in the console), to see the
          // wireframe for each widget.
140 141
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
142
            const Text(
143 144
              'You have pushed the button this many times:',
            ),
145
            Text(
146
              '$_counter',
147
              style: Theme.of(context).textTheme.headlineMedium,
148 149
            ),
          ],
150
        ),
151
      ),
152
      floatingActionButton: FloatingActionButton(
Adam Barth's avatar
Adam Barth committed
153 154
        onPressed: _incrementCounter,
        tooltip: 'Increment',
155
        child: const Icon(Icons.add),
156
      ), // This trailing comma makes auto-formatting nicer for build methods.
157 158 159
    );
  }
}
160
{{/withPluginHook}}
Daco Harkes's avatar
Daco Harkes committed
161
{{#withPlatformChannelPluginHook}}
162
class MyApp extends StatefulWidget {
163
  const MyApp({super.key});
164

165
  @override
166
  State<MyApp> createState() => _MyAppState();
167 168 169
}

class _MyAppState extends State<MyApp> {
170
  String _platformVersion = 'Unknown';
171
  final _{{pluginClassLowerCamelCase}} = {{pluginDartClass}}();
172 173

  @override
174
  void initState() {
175 176 177 178 179
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
180
  Future<void> initPlatformState() async {
181 182
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
183
    // We also handle the message potentially returning null.
184
    try {
185
      platformVersion =
186
          await _{{pluginClassLowerCamelCase}}.getPlatformVersion() ?? 'Unknown platform version';
187
    } on PlatformException {
188
      platformVersion = 'Failed to get platform version.';
189 190 191 192 193
    }

    // 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.
194
    if (!mounted) return;
195 196 197 198 199 200 201 202

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

  @override
  Widget build(BuildContext context) {
203 204 205
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
206
          title: const Text('Plugin example app'),
207
        ),
208 209
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
210
        ),
211 212 213 214
      ),
    );
  }
}
Daco Harkes's avatar
Daco Harkes committed
215
{{/withPlatformChannelPluginHook}}
216
{{#withFfi}}
Daco Harkes's avatar
Daco Harkes committed
217
class MyApp extends StatefulWidget {
218
  const MyApp({super.key});
Daco Harkes's avatar
Daco Harkes committed
219 220

  @override
221
  State<MyApp> createState() => _MyAppState();
Daco Harkes's avatar
Daco Harkes committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

class _MyAppState extends State<MyApp> {
  late int sumResult;
  late Future<int> sumAsyncResult;

  @override
  void initState() {
    super.initState();
    sumResult = {{pluginProjectName}}.sum(1, 2);
    sumAsyncResult = {{pluginProjectName}}.sumAsync(3, 4);
  }

  @override
  Widget build(BuildContext context) {
    const textStyle = TextStyle(fontSize: 25);
    const spacerSmall = SizedBox(height: 10);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Native Packages'),
        ),
        body: SingleChildScrollView(
          child: Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              children: [
                const Text(
                  'This calls a native function through FFI that is shipped as source in the package. '
                  'The native code is built as part of the Flutter Runner build.',
                  style: textStyle,
                  textAlign: TextAlign.center,
                ),
                spacerSmall,
                Text(
                  'sum(1, 2) = $sumResult',
                  style: textStyle,
                  textAlign: TextAlign.center,
                ),
                spacerSmall,
                FutureBuilder<int>(
                  future: sumAsyncResult,
                  builder: (BuildContext context, AsyncSnapshot<int> value) {
                    final displayValue =
                        (value.hasData) ? value.data : 'loading';
                    return Text(
                      'await sumAsync(3, 4) = $displayValue',
                      style: textStyle,
                      textAlign: TextAlign.center,
                    );
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
282
{{/withFfi}}
283
{{/withEmptyMain}}