|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- import 'dart:convert' as convert;
- import 'dart:io';
-
- import 'package:cached_network_image/cached_network_image.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:moblink/moblink.dart';
- import 'package:provider/provider.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/activity_dialog/activity_dialog.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_create.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/notification_setting_dialog/notification_setting_dialog.dart';
- import 'package:zhiying_base_widget/dialog/global_dialog/policy_dialog/policy_dialog.dart';
- import 'package:zhiying_base_widget/utils/contants.dart';
- import 'package:zhiying_comm/models/base/base_tab_model.dart';
- import 'package:zhiying_comm/util/image_util.dart';
- import 'package:zhiying_comm/util/mob_util/mob_util.dart';
- import 'package:sharesdk_plugin/sharesdk_plugin.dart';
- import 'package:zhiying_comm/util/shared_prefe_util.dart';
- import 'package:zhiying_comm/util/update/app_update_util.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
-
- class HomePage extends StatefulWidget {
- HomePage({Key key}) : super(key: key);
-
- @override
- _HomePageState createState() => _HomePageState();
- }
-
- class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
- int _currentIndex = 0;
- List<Map<String, dynamic>> _data = List();
- static const EventChannel _eventChannel =
- const EventChannel('JAVA_TO_FLUTTER');
-
- @override
- void initState() {
- WidgetsBinding.instance.addObserver(this);
- String data = BaseSettingModel.setting.tab['data'];
- try {
- List list = convert.jsonDecode(data);
- _data = list.map((item) {
- return Map<String, dynamic>.from(item);
- }).toList();
- Logger.debug(_data);
- } catch (error) {
- Logger.error(error);
- }
- Constants.isShowIntellectDialog = false;
- AppUpdateUtil.updateApp(context);
- TaobaoAuth.initAuth(context);
-
- // 弹窗
- _showPolicy();
-
- Moblink.uploadPrivacyPermissionStatus(1, (bool success) {});
- SharesdkPlugin.uploadPrivacyPermissionStatus( 1 , (bool success) {});
- // 是安卓系统,Android场景还原的实现
- /*if (defaultTargetPlatform == TargetPlatform.android) {
-
- }*/
-
- //app后台杀死时候的还原
- Moblink.restoreScene(_restore);
- // 监听开始(传递监听到原生端,用户监听场景还原的数据回传回来)
- _eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
-
- super.initState();
- }
-
- @override
- void dispose() {
- WidgetsBinding.instance.removeObserver(this);
- super.dispose();
- }
-
- @override
- void didChangeAppLifecycleState(AppLifecycleState state) async {
- ///智能粘贴板
-
- IntellectCreate.checkAndCreate(state, context);
-
- super.didChangeAppLifecycleState(state);
- }
-
- @override
- Widget build(BuildContext context) {
- ScreenUtil.init(context, width: 750, height: 1334);
- Logger.debug('home_page build');
- List<Map<String, dynamic>> tabs = _data;
- if (tabs == null || tabs.length == 0) {
- return Scaffold();
- }
-
- List<Widget> contentWidgets = tabs.map((item) {
- BaseTabModel model = BaseTabModel.fromJson(item);
- return PageFactory.create(model.skipIdentifier, item);
- }).toList();
- if (_currentIndex >= contentWidgets.length) {
- _currentIndex = 0;
- }
-
- return Scaffold(
- body: IndexedStack(
- index: _currentIndex,
- children: contentWidgets,
- ),
- //底部导航栏
- bottomNavigationBar: createBottomNavigationBar(tabs),
- );
- }
-
- Widget createBottomNavigationBar(List<Map<String, dynamic>> tabs) {
- List<BottomNavigationBarItem> items = List<BottomNavigationBarItem>();
-
- for (int i = 0; i < tabs.length; i++) {
- BaseTabModel model = BaseTabModel.fromJson(tabs[i]);
- String icon = ImageUtil.getUrl(model.icon);
- String selectedIcon = ImageUtil.getUrl(model.chooseIcon ?? model.icon);
- String textColor = model.fontColor;
- String chooseColor = model.chooseColor ?? textColor;
-
- items.add(BottomNavigationBarItem(
- icon: Container(
- width: 24,
- height: 24,
- margin: EdgeInsets.only(bottom: 4),
- child: CachedNetworkImage(
- imageUrl: _currentIndex == i ? selectedIcon : icon,
- fit: BoxFit.fitWidth,
- ),
- ),
- title: Text(
- model.name,
- style: TextStyle(
- fontSize: 11,
- color: HexColor.fromHex(
- _currentIndex == i ? chooseColor : textColor)),
- )));
- }
-
- if (items.length < 2) {
- return Container();
- }
- String bgColor = '#ffffff';
- if (tabs.first != null) {
- BaseTabModel model = BaseTabModel.fromJson(tabs.first);
- bgColor = model.bgColor ?? bgColor;
- }
- return BottomNavigationBar(
- backgroundColor: HexColor.fromHex(bgColor),
- type: BottomNavigationBarType.fixed,
- selectedFontSize: 11,
- unselectedFontSize: 11,
- currentIndex: _currentIndex,
- elevation: 0,
- onTap: ((index) async {
- BaseTabModel model = BaseTabModel.fromJson(tabs[index]);
- if (await _checkLimit(model)) {
- setState(() {
- _currentIndex = index;
- });
- }
- }),
- //底部导航栏
- items: items);
- }
-
- Future<bool> _checkLimit(BaseTabModel model) async {
- if (model.requiredLogin == '1') {
- UserInfoModel user =
- await Provider.of<UserInfoNotifier>(context, listen: false)
- .getUserInfoModel();
- print(user.toString());
- if (user?.token == null || user.token == '') {
- print('need login...');
- RouterUtil.goLogin(context);
- return false;
- }
- }
- return true;
- }
-
- ///
- /// 各种弹窗
- /// 1、用户协议弹窗
- /// 2、通知栏开启弹窗
- /// 3、活动弹窗
- ///
- Future _showPolicy() async {
- // 协议弹窗
- String isShowPolicy = await SharedPreferencesUtil.getStringValue(Constants.isShowPolicy);
- if (isShowPolicy == null || isShowPolicy != '1') {
- bool isAccept = await PolicyDialog.show(context);
- if (!isAccept) {
- exit(0);
- } else {
- await SharedPreferencesUtil.setStringValue(Constants.isShowPolicy, "1");
- }
- }
-
- // 通知弹窗
- String isShowNotiPermission = await SharedPreferencesUtil.getStringValue(
- Constants.isShowNotiPermission);
- if (isShowNotiPermission == null || isShowNotiPermission != '1') {
- await NotificationSettingDialog.show(context);
- await SharedPreferencesUtil.setStringValue(Constants.isShowNotiPermission, "1");
- }
-
- // 活动弹窗
- await ActivityDialog.show(context);
-
- IntellectCreate.checkAndCreateFirst(context);
- }
-
- // 场景还原,记录邀请码
- void _restore(MLSDKScene scene) {
- // const bool inProduction = const bool.fromEnvironment("dart.vm.product");
- // if (!inProduction) {
- // // 非release环境,弹窗测试
- // showAlert('要还原的路径为:' + scene.className + '\n' + scene.params.toString(),
- // context);
- // }
- Logger.debug('要还原的路径为:' + scene.className);
- try {
- Map<String, dynamic> params = Map<String, dynamic>.from(scene.params);
- if (params.containsKey('tgid')) {
- String tgid = params['tgid'].toString();
- // 记录邀请码到本地
- MobUtil.storeInvitedCode(tgid);
- }
- } catch (e) {
- Logger.error(e);
- }
- }
-
- //app存在后台时候的还原
- void _onEvent(Object event) {
- Logger.debug('返回的内容: $event');
- try {
- if (null != event) {
- Map<String, dynamic> params = Map<String, dynamic>.from(event);
- // const bool inProduction = const bool.fromEnvironment("dart.vm.product");
- // if (!inProduction) {
- // // 非release环境,弹窗测试
- // showAlert('要还原的路径为[活着]:$event', context);
- // }
- if (params['params'].containsKey('tgid')) {
- String tgid = params['params']['tgid'].toString();
- // 记录邀请码到本地
- MobUtil.storeInvitedCode(tgid);
- }
- }
- } catch (e) {
- Logger.error(e);
- }
- }
-
- void _onError(Object error) {
- Logger.error('返回的错误: $error');
- }
-
- void showAlert(String text, BuildContext context) {
- showDialog(
- context: context,
- builder: (BuildContext context) => CupertinoAlertDialog(
- title: new Text("提示"),
- content: new Text(text),
- actions: <Widget>[
- new FlatButton(
- child: new Text("OK"),
- onPressed: () {
- Navigator.of(context).pop();
- },
- )
- ]));
- }
- }
|