home_page.dart 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/foundation.dart';
  4. import 'package:shirne_dialog/shirne_dialog.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:window_manager/window_manager.dart';
  7. import '../global.dart';
  8. import '../models/game_manager.dart';
  9. class HomePage extends StatefulWidget {
  10. final Widget child;
  11. final bool isMain;
  12. const HomePage({Key? key, required this.child, this.isMain = false})
  13. : super(key: key);
  14. static HomePageState of(BuildContext context) {
  15. return context.findAncestorStateOfType<HomePageState>()!;
  16. }
  17. @override
  18. State<HomePage> createState() => HomePageState();
  19. }
  20. class HomePageState extends State<HomePage> with WindowListener {
  21. final GameManager gamer = GameManager();
  22. @override
  23. void initState() {
  24. super.initState();
  25. if (widget.isMain) {
  26. if (!kIsWeb &&
  27. (Platform.isWindows || Platform.isMacOS || Platform.isLinux)) {
  28. windowManager.addListener(this);
  29. }
  30. }
  31. }
  32. @override
  33. void dispose() {
  34. if (widget.isMain) {
  35. gamer.dispose();
  36. }
  37. super.dispose();
  38. }
  39. @override
  40. void onWindowClose() {
  41. logger.info('gamer destroy');
  42. windowManager.removeListener(this);
  43. gamer.dispose();
  44. GameManager.instance.engine?.dispose();
  45. }
  46. Future<bool> _willPop() async {
  47. logger.info('onwillpop');
  48. final sure = await MyDialog.confirm(
  49. context.l10n.exitNow,
  50. buttonText: context.l10n.yesExit,
  51. cancelText: context.l10n.dontExit,
  52. );
  53. if (sure ?? false) {
  54. logger.info('gamer destroy');
  55. gamer.dispose();
  56. //gamer = null;
  57. await Future.delayed(const Duration(milliseconds: 200));
  58. return true;
  59. }
  60. return false;
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. Size size = MediaQuery.of(context).size;
  65. if (size.width < 541) {
  66. gamer.scale = (size.width - 20) / 521;
  67. } else {
  68. gamer.scale = 1;
  69. }
  70. return WillPopScope(
  71. onWillPop: widget.isMain ? _willPop : null,
  72. child: widget.child,
  73. );
  74. }
  75. }