main.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import 'dart:io';
  2. import 'package:device_info_plus/device_info_plus.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:image_puzzle/app_lifecycle/app_lifecycle.dart';
  6. import 'package:image_puzzle/play/board_play.dart';
  7. import 'package:logging/logging.dart';
  8. import 'dart:developer' as dev;
  9. import 'package:path_provider/path_provider.dart';
  10. import 'package:provider/provider.dart';
  11. import 'config/config.dart';
  12. import 'config/device.dart';
  13. Logger _log = Logger('main.dart');
  14. final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
  15. void main() async {
  16. // Subscribe to log messages.
  17. Logger.root.onRecord.listen((record) {
  18. dev.log(
  19. record.message,
  20. time: record.time,
  21. level: record.level.value,
  22. name: record.loggerName,
  23. zone: record.zone,
  24. error: record.error,
  25. stackTrace: record.stackTrace,
  26. );
  27. });
  28. WidgetsFlutterBinding.ensureInitialized();
  29. // 强制竖屏
  30. SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  31. // 进入全屏沉浸式, 隐藏底部导航以及状态栏
  32. if (Platform.isAndroid) {
  33. SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
  34. }
  35. SystemChrome.setSystemUIOverlayStyle(
  36. const SystemUiOverlayStyle(
  37. statusBarColor: Colors.transparent, // <-- SEE HERE
  38. statusBarIconBrightness: Brightness.dark, //<-- For Android SEE HERE (dark icons)
  39. statusBarBrightness: Brightness.light, //<-- For iOS SEE HERE (dark icons)
  40. ),
  41. );
  42. Directory baseDir = await getApplicationDocumentsDirectory();
  43. runApp(MyApp(baseDir: baseDir));
  44. }
  45. class MyApp extends StatelessWidget {
  46. final Directory baseDir;
  47. const MyApp({super.key, required this.baseDir});
  48. // This widget is the root of your application.
  49. @override
  50. Widget build(BuildContext context) {
  51. Config config = Config(context, baseDir);
  52. return AppLifecycleObserver(
  53. child: MultiProvider(
  54. providers: [
  55. Provider<Config>(lazy: false, create: (context) => config),
  56. Provider<Device>(lazy: false, create: (context) => config.device),
  57. ],
  58. child: Prepare(
  59. child: MaterialApp(
  60. key: GlobalKey(),
  61. title: 'Image Puzzle',
  62. initialRoute: '/play',
  63. navigatorObservers: [routeObserver],
  64. routes: {
  65. '/': (context) => const MyHomePage(title: "Image Puzzle"),
  66. '/play': (context) => BoardPlay(cols: 4, rows: 4),
  67. },
  68. theme: ThemeData(brightness: Brightness.light, primaryColor: Colors.green, primarySwatch: Colors.blue),
  69. ),
  70. ),
  71. ),
  72. );
  73. }
  74. }
  75. class MyHomePage extends StatefulWidget {
  76. const MyHomePage({super.key, required this.title});
  77. // This widget is the home page of your application. It is stateful, meaning
  78. // that it has a State object (defined below) that contains fields that affect
  79. // how it looks.
  80. // This class is the configuration for the state. It holds the values (in this
  81. // case the title) provided by the parent (in this case the App widget) and
  82. // used by the build method of the State. Fields in a Widget subclass are
  83. // always marked "final".
  84. final String title;
  85. @override
  86. State<MyHomePage> createState() => _MyHomePageState();
  87. }
  88. class _MyHomePageState extends State<MyHomePage> {
  89. int _counter = 0;
  90. void _incrementCounter() {
  91. setState(() {
  92. // This call to setState tells the Flutter framework that something has
  93. // changed in this State, which causes it to rerun the build method below
  94. // so that the display can reflect the updated values. If we changed
  95. // _counter without calling setState(), then the build method would not be
  96. // called again, and so nothing would appear to happen.
  97. _counter++;
  98. });
  99. }
  100. @override
  101. Widget build(BuildContext context) {
  102. // This method is rerun every time setState is called, for instance as done
  103. // by the _incrementCounter method above.
  104. //
  105. // The Flutter framework has been optimized to make rerunning build methods
  106. // fast, so that you can just rebuild anything that needs updating rather
  107. // than having to individually change instances of widgets.
  108. return Scaffold(
  109. appBar: AppBar(
  110. // TRY THIS: Try changing the color here to a specific color (to
  111. // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
  112. // change color while the other colors stay the same.
  113. backgroundColor: Theme.of(context).colorScheme.inversePrimary,
  114. // Here we take the value from the MyHomePage object that was created by
  115. // the App.build method, and use it to set our appbar title.
  116. title: Text(widget.title),
  117. ),
  118. body: Center(
  119. // Center is a layout widget. It takes a single child and positions it
  120. // in the middle of the parent.
  121. child: Column(
  122. // Column is also a layout widget. It takes a list of children and
  123. // arranges them vertically. By default, it sizes itself to fit its
  124. // children horizontally, and tries to be as tall as its parent.
  125. //
  126. // Column has various properties to control how it sizes itself and
  127. // how it positions its children. Here we use mainAxisAlignment to
  128. // center the children vertically; the main axis here is the vertical
  129. // axis because Columns are vertical (the cross axis would be
  130. // horizontal).
  131. //
  132. // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
  133. // action in the IDE, or press "p" in the console), to see the
  134. // wireframe for each widget.
  135. mainAxisAlignment: MainAxisAlignment.center,
  136. children: <Widget>[
  137. const Text('You have pushed the button this many times:'),
  138. Text('$_counter', style: Theme.of(context).textTheme.headlineMedium),
  139. ],
  140. ),
  141. ),
  142. floatingActionButton: FloatingActionButton(
  143. onPressed: _incrementCounter,
  144. tooltip: 'Increment',
  145. child: const Icon(Icons.add),
  146. ), // This trailing comma makes auto-formatting nicer for build methods.
  147. );
  148. }
  149. }
  150. class Prepare extends StatefulWidget {
  151. final Widget child;
  152. const Prepare({super.key, required this.child});
  153. @override
  154. State<Prepare> createState() => _PrepareState();
  155. }
  156. class _PrepareState extends State<Prepare> {
  157. @override
  158. void initState() {
  159. super.initState();
  160. loadDeviceInfo();
  161. }
  162. /// 获取android平台信息,用户判断是否低端机
  163. loadDeviceInfo() async {
  164. DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  165. if (Platform.isAndroid) {
  166. try {
  167. context.read<Device>().androidDeviceInfo = await deviceInfoPlugin.androidInfo;
  168. } catch (e) {}
  169. }
  170. }
  171. @override
  172. Widget build(BuildContext context) {
  173. //Update ad banner size
  174. // AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
  175. // MediaQuery.of(context).size.width.truncate(),
  176. // ).then((value) {
  177. // if (value != null) {
  178. // context.read<Device>().bannerHeight = value.height.toDouble();
  179. // }
  180. // }).catchError((err) {
  181. // //todo
  182. // });
  183. context.read<Device>().bannerHeight = 50;
  184. return widget.child;
  185. }
  186. }