|
@@ -1,50 +1,87 @@
|
|
-import 'package:flutter/material.dart';
|
|
+import 'dart:async';
|
|
-import 'package:flutter_chinese_chees/game_board.dart';
|
|
+import 'dart:io';
|
|
-import 'package:flutter_chinese_chees/models/game_manager.dart';
|
|
+import 'package:flutter/foundation.dart';
|
|
-
|
|
+import 'package:shirne_dialog/shirne_dialog.dart';
|
|
-
|
|
+import 'package:flutter/material.dart';
|
|
-
|
|
+import 'package:window_manager/window_manager.dart';
|
|
-
|
|
+
|
|
-class HomePage extends StatefulWidget {
|
|
+import '../global.dart';
|
|
- const HomePage({super.key});
|
|
+import '../models/game_manager.dart';
|
|
-
|
|
+
|
|
- @override
|
|
+class HomePage extends StatefulWidget {
|
|
- State<HomePage> createState() => _HomePageState();
|
|
+ final Widget child;
|
|
-}
|
|
+ final bool isMain;
|
|
-
|
|
+
|
|
-class _HomePageState extends State<HomePage> {
|
|
+ const HomePage({Key? key, required this.child, this.isMain = false})
|
|
- final GameManager gamer = GameManager();
|
|
+ : super(key: key);
|
|
-
|
|
+
|
|
- @override
|
|
+ static HomePageState of(BuildContext context) {
|
|
- Widget build(BuildContext context) {
|
|
+ return context.findAncestorStateOfType<HomePageState>()!;
|
|
- return Scaffold(
|
|
+ }
|
|
- appBar: AppBar(
|
|
+
|
|
- title: Text("context.l10n.appTitle"),
|
|
+ @override
|
|
- leading: Builder(builder: (context) {
|
|
+ State<HomePage> createState() => HomePageState();
|
|
- return IconButton(
|
|
+}
|
|
- icon: Icon(Icons.menu),
|
|
+
|
|
- onPressed: () {
|
|
+class HomePageState extends State<HomePage> with WindowListener {
|
|
- Scaffold.of(context).openDrawer();
|
|
+ final GameManager gamer = GameManager();
|
|
- },
|
|
+
|
|
- );
|
|
+ @override
|
|
- }),
|
|
+ void initState() {
|
|
- actions: [
|
|
+ super.initState();
|
|
- IconButton(
|
|
+ if (widget.isMain) {
|
|
- icon: Icon(Icons.logout),
|
|
+ if (!kIsWeb &&
|
|
- onPressed: () {},
|
|
+ (Platform.isWindows || Platform.isMacOS || Platform.isLinux)) {
|
|
- )
|
|
+ windowManager.addListener(this);
|
|
- ]),
|
|
+ }
|
|
- drawer: Drawer(
|
|
+ }
|
|
- child: ListView(children: [
|
|
+ }
|
|
- UserAccountsDrawerHeader(
|
|
+
|
|
- accountName: Text("张三"), accountEmail: Text(""))
|
|
+ @override
|
|
- ])),
|
|
+ void dispose() {
|
|
- body: GameBoard(),
|
|
+ if (widget.isMain) {
|
|
- );
|
|
+ gamer.dispose();
|
|
- }
|
|
+ }
|
|
-
|
|
+ super.dispose();
|
|
- @override
|
|
+ }
|
|
- void dispose() {
|
|
+
|
|
- super.dispose();
|
|
+ @override
|
|
- }
|
|
+ void onWindowClose() {
|
|
-}
|
|
+ logger.info('gamer destroy');
|
|
|
|
+ windowManager.removeListener(this);
|
|
|
|
+ gamer.dispose();
|
|
|
|
+ GameManager.instance.engine?.dispose();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Future<bool> _willPop() async {
|
|
|
|
+ logger.info('onwillpop');
|
|
|
|
+ final sure = await MyDialog.confirm(
|
|
|
|
+ context.l10n.exitNow,
|
|
|
|
+ buttonText: context.l10n.yesExit,
|
|
|
|
+ cancelText: context.l10n.dontExit,
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ if (sure ?? false) {
|
|
|
|
+ logger.info('gamer destroy');
|
|
|
|
+ gamer.dispose();
|
|
|
|
+
|
|
|
|
+ await Future.delayed(const Duration(milliseconds: 200));
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @override
|
|
|
|
+ Widget build(BuildContext context) {
|
|
|
|
+ Size size = MediaQuery.of(context).size;
|
|
|
|
+ if (size.width < 541) {
|
|
|
|
+ gamer.scale = (size.width - 20) / 521;
|
|
|
|
+ } else {
|
|
|
|
+ gamer.scale = 1;
|
|
|
|
+ }
|
|
|
|
+ return WillPopScope(
|
|
|
|
+ onWillPop: widget.isMain ? _willPop : null,
|
|
|
|
+ child: widget.child,
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+}
|