admin
2022-03-31 36754ba47da7a3277d5be183a523c912a1dc4cef
src/main/resources/code/flutter_module/lib/main.dart
@@ -1,112 +1,371 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_boost/flutter_boost.dart';
void main() => runApp(const MyApp());
import '../../home.dart';
import '../../model/video/video_model.dart';
import '../../ui/common/browser.dart';
import '../../ui/demo_page.dart';
import '../../ui/mine/email_login.dart';
import '../../ui/mine/login.dart';
import '../../ui/mine/person_info.dart';
import '../../ui/mine/settings.dart';
import '../../ui/video/video_collected_list.dart';
import '../../ui/video/video_detail.dart';
import '../../ui/video/video_list.dart';
import '../../ui/video/video_scan_record_list.dart';
import 'mine.dart';
import 'ui/mine/about_us.dart';
import 'ui/mine/advice.dart';
import 'ui/search/search.dart';
import 'ui/video/video_attention_list.dart';
import 'ui/video/video_download_list.dart';
import 'ui/video/video_player_browser.dart';
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
// void main() {
//   runApp(getBasePage(
//     AboutUsPage(title: ""),
//   ));
// }
  // This widget is the root of your application.
void main() {
  ///添加全局生命周期监听类
  PageVisibilityBinding.instance.addGlobalObserver(AppLifecycleObserver());
  CustomFlutterBinding();
  //window.defaultRouteName
  runApp(MyApp());
}
class CustomFlutterBinding extends WidgetsFlutterBinding
    with BoostFlutterBinding {}
class MyApp extends StatefulWidget {
  @override
  Widget build(BuildContext context) {
  _MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  /// 由于很多同学说没有跳转动画,这里是因为之前exmaple里面用的是 [PageRouteBuilder],
  /// 其实这里是可以自定义的,和Boost没太多关系,比如我想用类似iOS平台的动画,
  /// 那么只需要像下面这样写成 [CupertinoPageRoute] 即可
  /// (这里全写成[MaterialPageRoute]也行,这里只不过用[CupertinoPageRoute]举例子)
  ///
  /// 注意,如果需要push的时候,两个页面都需要动的话,
  /// (就是像iOS native那样,在push的时候,前面一个页面也会向左推一段距离)
  /// 那么前后两个页面都必须是遵循CupertinoRouteTransitionMixin的路由
  /// 简单来说,就两个页面都是CupertinoPageRoute就好
  /// 如果用MaterialPageRoute的话同理
  static Route<dynamic>? _getSimpleRoute(
      RouteSettings settings, String? uniqueId, Widget page) {
    return CupertinoPageRoute(
        settings: settings,
        builder: (_) {
          return page;
        });
  }
  Map<String, FlutterBoostRouteFactory> routerMap = {
    '/': (RouteSettings settings, String? uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            // Map<String, Object> map = settings.arguments as Map<String, Object> ;
            // String data = map['data'] as String;
            return HomePage(
              title: "",
            );
          });
    },
    'home': (RouteSettings settings, String? uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            // Map<String, Object> map = settings.arguments as Map<String, Object> ;
            // String data = map['data'] as String;
            return HomePage(
              title: "",
            );
          });
    },
    'mine': (RouteSettings settings, String? uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            return MinePage(
              title: "",
            );
          });
    },
    'VideoDetailPage': (RouteSettings settings, String? uniqueId) {
      if (settings.arguments == null) {
        return null;
      }
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            Map<String, dynamic> params =
                settings.arguments as Map<String, dynamic>;
            int position = params["position"] ?? 0;
            return VideoDetailPage(
              videoInfo: VideoInfoModel.fromJson(params["video"]),
              position: position,
            );
          });
    },
    'VideoCollectedPage': (RouteSettings settings, String? uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            return VideoCollectedPage(
              title: "",
            );
          });
    },
    'VideoAttentionPage': (RouteSettings settings, String? uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            return VideoAttentionPage(
              title: "",
            );
          });
    },
    'VideoScanRecordPage': (RouteSettings settings, String? uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            return VideoScanRecordPage(
              title: "",
            );
          });
    },
    'VideoDownloadPage': (RouteSettings settings, String? uniqueId) {
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            return VideoDownloadPage(
              title: "",
            );
          });
    },
    "PersonInfoPage": (RouteSettings settings, String? uniqueId) {
      return _getSimpleRoute(settings, uniqueId, PersonInfoPage(title: ""));
    },
    "SettingPage": (RouteSettings settings, String? uniqueId) {
      return _getSimpleRoute(settings, uniqueId, SettingPage(title: ""));
    },
    "EmailLoginPage": (RouteSettings settings, String? uniqueId) {
      return _getSimpleRoute(settings, uniqueId, EmailLoginPage(title: ""));
    },
    "LoginPage": (RouteSettings settings, String? uniqueId) {
      return _getSimpleRoute(settings, uniqueId, LoginPage(title: ""));
    },
    "AdvicePage": (RouteSettings settings, String? uniqueId) {
      return _getSimpleRoute(settings, uniqueId, AdvicePage(title: ""));
    },
    "AboutUsPage": (RouteSettings settings, String? uniqueId) {
      return _getSimpleRoute(settings, uniqueId, AboutUsPage(title: ""));
    },
    'SearchPage': (RouteSettings settings, String? uniqueId) {
      if (settings.arguments == null) {
        return null;
      }
      Map<String, dynamic> params = settings.arguments as Map<String, dynamic>;
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            return SearchPage(
              title: params["title"] ?? "",
            );
          });
    },
    'VideoListPage': (RouteSettings settings, String? uniqueId) {
      if (settings.arguments == null) {
        return null;
      }
      Map<String, dynamic> params = settings.arguments as Map<String, dynamic>;
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            String kw = params["kw"]!;
            String title = params["title"] ?? "";
            return VideoListPage(
              title: title,
              kw: kw,
            );
          });
    },
    'BrowserPage': (RouteSettings settings, String? uniqueId) {
      if (settings.arguments == null) {
        return null;
      }
      Map<String, dynamic> params = settings.arguments as Map<String, dynamic>;
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            String title = params["title"] ?? "";
            return BrowserPage(title: title, url: params["url"]);
          });
    },
    'VideoPlayerWebPage': (RouteSettings settings, String? uniqueId) {
      if (settings.arguments == null) {
        return null;
      }
      Map<String, dynamic> params = settings.arguments as Map<String, dynamic>;
      return CupertinoPageRoute(
          settings: settings,
          builder: (_) {
            String title = params["title"] ?? "";
            return VideoPlayerWebPage(title: title, url: params["url"]);
          });
    },
  };
  Route<dynamic>? routeFactory(RouteSettings settings, String? uniqueId) {
    print("路由构造:${settings}");
    FlutterBoostRouteFactory? func = routerMap[settings.name!];
    if (func == null) {
      return null;
    }
    return func(settings, uniqueId);
  }
  Widget appBuilder(Widget home) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or press Run > Flutter Hot Reload in a Flutter IDE). Notice that the
        // counter didn't reset back to zero; the application is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
      home: home,
      debugShowCheckedModeBanner: true,
      ///必须加上builder参数,否则showDialog等会出问题
      builder: (_, __) {
        return home;
      },
    );
  }
}
class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  // 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.
  // 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".
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  void _incrementCounter() {
    setState(() {
      // 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.
      _counter++;
    });
  void initState() {
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    // 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.
    return Scaffold(
      appBar: AppBar(
        // 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.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // 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).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    return FlutterBoostApp(
      routeFactory,
      appBuilder: appBuilder,
    );
  }
}
Widget widgetForRoute(String route) {
  print("flutter page: $route");
  Map<String, dynamic> json = jsonDecode(route);
  String page = json["page"];
  dynamic params = json["params"];
  switch (page) {
    case "DemoPage":
      return DemoPage(
        title: '',
      );
    case "home":
      return HomePage(
        title: '',
      );
    case "mine":
      return MinePage(title: "");
    case "VideoCollectedPage":
      return VideoCollectedPage(title: "");
    case "VideoAttentionPage":
      return VideoAttentionPage(title: "");
    case "VideoScanRecordPage":
      return VideoScanRecordPage(title: "");
    case "VideoDownloadPage":
      return VideoDownloadPage(title: "");
    case "PersonInfoPage":
      return PersonInfoPage(title: "");
    case "SettingPage":
      return SettingPage(title: "");
    case "EmailLoginPage":
      return EmailLoginPage(title: "");
    case "LoginPage":
      return LoginPage(title: "");
    case "AdvicePage":
      return AdvicePage(title: "");
    case "SearchPage":
      return SearchPage(title: params!["title"]);
    case "AboutUsPage":
      return AboutUsPage(title: "");
    case "VideoDetailPage":
      {
        int position = params!["position"] ?? 0;
        return VideoDetailPage(
          videoInfo: VideoInfoModel.fromJson(params!["video"]),
          position: position,
        );
      }
    case "VideoListPage":
      {
        String kw = params!["kw"]!;
        String title = params!["title"] ?? "";
        return VideoListPage(
          title: title,
          kw: kw,
        );
      }
    case "BrowserPage":
      {
        String title = params!["title"] ?? "";
        return BrowserPage(title: title, url: params!["url"]);
      }
    case "VideoPlayerWebPage":
      {
        String title = params!["title"] ?? "";
        return VideoPlayerWebPage(title: title, url: params!["url"]);
      }
  }
  return Container();
}
///全局生命周期监听示例
class AppLifecycleObserver with GlobalPageVisibilityObserver {
  @override
  void onBackground(Route route) {
    super.onBackground(route);
    print("AppLifecycleObserver - ${route.settings.name} - onBackground");
  }
  @override
  void onForeground(Route route) {
    super.onForeground(route);
    print("AppLifecycleObserver ${route.settings.name} - onForground");
  }
  @override
  void onPagePush(Route route) {
    super.onPagePush(route);
    print("AppLifecycleObserver - ${route.settings.name}- onPagePush");
  }
  @override
  void onPagePop(Route route) {
    super.onPagePop(route);
    print("AppLifecycleObserver - ${route.settings.name}- onPagePop");
  }
  @override
  void onPageHide(Route route) {
    super.onPageHide(route);
    print("AppLifecycleObserver - ${route.settings.name}- onPageHide");
  }
  @override
  void onPageShow(Route route) {
    super.onPageShow(route);
    print("AppLifecycleObserver - ${route.settings.name}- onPageShow");
  }
}