// 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.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:my_plugin/my_plugin.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _lifecyclePlugin = MyPlugin();
  var _lifecycleEvents = '';

  void _refreshLifecycleEvents() async {
    String updatedEvents;
    try {
      updatedEvents = await _lifecyclePlugin.getLifecycleEvents() ?? '';
    } on PlatformException {
      updatedEvents = '';
    }
    setState(() {
      _lifecycleEvents = updatedEvents;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: _refreshLifecycleEvents,
              child: const Text('Get Lifecycle Events'),
            ),
            const Text('Lifecycle Events:'),
            Text(_lifecycleEvents),
          ],
        ),
      ),
    );
  }
}
