421 lines
13 KiB
Dart
421 lines
13 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:aiplot/common/func.dart';
|
|
import 'package:aiplot/dialog/login_dialog.dart';
|
|
import 'package:aiplot/dialog/phone_login_dialog.dart';
|
|
import 'package:aiplot/network/NetworkConfig.dart';
|
|
import 'package:aiplot/tools/me/me_page.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:ironsource_mediation/ironsource_mediation.dart';
|
|
import 'package:sensors_analytics_flutter_plugin/sensors_analytics_flutter_plugin.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
import '../../bean/levitating_ball_pop_bean.dart';
|
|
import '../../bean/popout_bean.dart';
|
|
import '../../common/EventBusUtil.dart';
|
|
import '../../common/Global.dart';
|
|
import '../../common/dialog_bean.dart';
|
|
import '../../common/dialog_manager.dart';
|
|
import '../../common/dialog_util.dart';
|
|
import '../../dialog/newDialog.dart';
|
|
import '../../dialog/new_ball_dialog.dart';
|
|
import '../../generated/l10n.dart';
|
|
import '../create/create_page.dart';
|
|
import '../gallery/gallery_content_page.dart';
|
|
import '../upload/upload_page.dart';
|
|
import 'home_model.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> with IronSourceRewardedVideoListener {
|
|
StreamSubscription? subscription;
|
|
final HomeModel _viewModel = HomeModel();
|
|
|
|
//创建StreamController
|
|
StreamController? streamController;
|
|
|
|
// 获取StreamSink用于发射事件
|
|
StreamSink? get streamSink => streamController?.sink;
|
|
|
|
// 获取Stream用于监听
|
|
Stream? get streamData => streamController?.stream;
|
|
|
|
int currentIndex = 0;
|
|
late List<BottomNavigationBarItem> items;
|
|
late List<StatefulWidget> bodyList;
|
|
late PageController pageController; //
|
|
|
|
GlobalKey<LoginDialogState> loginDialogKey = GlobalKey(); //登录弹框key
|
|
|
|
AssetImage galleryImg = const AssetImage('assets/images/gallery.png');
|
|
AssetImage galleryImgS = const AssetImage('assets/images/gallery_r.png');
|
|
AssetImage createImg = const AssetImage('assets/images/create.png');
|
|
AssetImage createImgS = const AssetImage('assets/images/create_r.png');
|
|
AssetImage meImg = const AssetImage('assets/images/me.png');
|
|
AssetImage meImgS = const AssetImage('assets/images/me_r.png');
|
|
|
|
AssetImage createImg1 = const AssetImage('assets/images/jia.png');
|
|
AssetImage createImgS1 = const AssetImage('assets/images/jia_w.png');
|
|
|
|
late StreamSubscription<PhoneLoginBackEvent> _phoneLoginBackEvent;
|
|
late StreamSubscription<TabBarJumpEvent> _tabBarJumpEvent;
|
|
late StreamSubscription<LoginSuccess> _loginSuccess;
|
|
|
|
late LevitatingBallPopBean levitatingBallPopBean;
|
|
List<PopoutBean> popList = [];
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
IronSource.setRVListener(this);
|
|
streamController = StreamController<int>();
|
|
pageController = PageController(initialPage: NetworkConfig.appConfigBean != null && !NetworkConfig.appConfigBean!.IsCheckRemark! ? 1 : 0);
|
|
NetworkConfig.appConfigBean != null && !NetworkConfig.appConfigBean!.IsCheckRemark! ? streamSink!.add(1) : streamSink!.add(0);
|
|
|
|
///eventBus
|
|
//手机登录返回
|
|
_phoneLoginBackEvent = EventBusUtil.listen((event) {
|
|
showLoginDialog();
|
|
});
|
|
|
|
//导航跳转
|
|
_tabBarJumpEvent = EventBusUtil.listen((event) {
|
|
if (currentIndex != event.index) {
|
|
pageController.animateToPage(
|
|
event.index,
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
}
|
|
});
|
|
|
|
//登录完成
|
|
_loginSuccess = EventBusUtil.listen((event) {
|
|
//新用户弹框
|
|
_viewModel.getNewUserPackage();
|
|
});
|
|
|
|
//网络请求回调
|
|
subscription = _viewModel.streamController.stream.listen((newData) {
|
|
String code = newData['code'];
|
|
if (code.isNotEmpty) {
|
|
EasyLoading.dismiss();
|
|
switch (code) {
|
|
case "getNewUserPackage": //新用户弹框
|
|
levitatingBallPopBean = newData['data'];
|
|
if (levitatingBallPopBean.BgPicInfo != null) {
|
|
DialogManager()
|
|
..add(DialogBean(
|
|
createDialogWidget: () => DialogUtil.createTipWidget(
|
|
context,
|
|
NewBallDialog(
|
|
onTap: () {
|
|
EventBusUtil.fire(RefreshUserdata());
|
|
},
|
|
popoutBean: levitatingBallPopBean,
|
|
),
|
|
"NewBallDialog",
|
|
canceled: false)))
|
|
..show(context);
|
|
}
|
|
break;
|
|
case "lookAdReward": //看完广告
|
|
Fluttertoast.showToast(
|
|
msg: newData['data'],
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.CENTER,
|
|
timeInSecForIosWeb: 1,
|
|
backgroundColor: Colors.black,
|
|
textColor: Colors.white,
|
|
fontSize: 16.0);
|
|
EventBusUtil.fire(RefreshUserdata());
|
|
break;
|
|
case "getPopoutList":
|
|
popList = newData['data'];
|
|
if (popList.length > 0) {
|
|
for (int i = 0; i < popList.length; i++) {
|
|
DialogManager()
|
|
..add(DialogBean(
|
|
createDialogWidget: () => DialogUtil.createTipWidget(
|
|
context,
|
|
newDialog(
|
|
onTap: (PopoutId, PopoutType, text) {
|
|
// ObtainPopoutAwards(PopoutId, PopoutType, text);
|
|
_xy(text);
|
|
},
|
|
popoutBean: popList[i],
|
|
),
|
|
"Popout" + i.toString(),
|
|
canceled: false)))
|
|
..show(context);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
setState(() {});
|
|
});
|
|
_viewModel.getNewUserPackage();
|
|
_viewModel.getPopoutList();
|
|
_firstFiring();
|
|
}
|
|
|
|
//判断首次启动
|
|
_firstFiring() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
bool boolValue = prefs.getBool('firstFiring') ?? false;
|
|
if (!boolValue) {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
prefs.setBool('firstFiring', true);
|
|
SensorsAnalyticsFlutterPlugin.track('Device_activation', {'IsDefaultTourist': 0, 'WUID': NetworkConfig.deviceID});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
bottomItem();
|
|
return WillPopScope(
|
|
onWillPop: doubleClickBack,
|
|
child: Scaffold(
|
|
backgroundColor: Colors.white,
|
|
bottomNavigationBar: StreamBuilder(
|
|
stream: streamData,
|
|
builder: (BuildContext context, AsyncSnapshot snapshot) {
|
|
if (snapshot.data != null) {
|
|
currentIndex = snapshot.data;
|
|
return BottomNavigationBar(
|
|
items: items,
|
|
elevation: 0,
|
|
backgroundColor: Colors.white,
|
|
currentIndex: snapshot.data,
|
|
onTap: onTap,
|
|
selectedItemColor: Color(0xFFAF78E3),
|
|
unselectedItemColor: Color(0xFF818181),
|
|
selectedFontSize: 12,
|
|
unselectedFontSize: 12,
|
|
type: BottomNavigationBarType.fixed,
|
|
);
|
|
} else {
|
|
return Container();
|
|
}
|
|
},
|
|
),
|
|
body: Stack(
|
|
children: [
|
|
PageView(
|
|
controller: pageController,
|
|
onPageChanged: onPageChanged,
|
|
children: bodyList,
|
|
physics: NeverScrollableScrollPhysics(),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
///底部导航
|
|
bottomItem() {
|
|
items = [
|
|
BottomNavigationBarItem(
|
|
icon: Image(
|
|
width: 34,
|
|
height: 34,
|
|
image: galleryImg,
|
|
),
|
|
activeIcon: Image(
|
|
width: 34,
|
|
height: 34,
|
|
image: galleryImgS,
|
|
),
|
|
label: S.of(context).Gallery),
|
|
BottomNavigationBarItem(
|
|
icon: Image(
|
|
width: 34,
|
|
height: 34,
|
|
image: !NetworkConfig.appConfigBean!.IsCheckRemark! ? createImg : createImg1,
|
|
),
|
|
activeIcon: Image(
|
|
width: 34,
|
|
height: 34,
|
|
image: !NetworkConfig.appConfigBean!.IsCheckRemark! ? createImgS : createImgS1,
|
|
),
|
|
label: !NetworkConfig.appConfigBean!.IsCheckRemark! ? S.of(context).Creation : S.of(context).Upload),
|
|
BottomNavigationBarItem(
|
|
icon: Image(
|
|
width: 34,
|
|
height: 34,
|
|
image: meImg,
|
|
),
|
|
activeIcon: Image(
|
|
width: 34,
|
|
height: 34,
|
|
image: meImgS,
|
|
),
|
|
label: S.of(context).Me),
|
|
];
|
|
bodyList = [
|
|
GalleryContentPage(),
|
|
!NetworkConfig.appConfigBean!.IsCheckRemark! ? const CreatePage() : const UploadPage(),
|
|
const MePage(),
|
|
];
|
|
}
|
|
|
|
void onTap(int index) {
|
|
if (index > 1 && NetworkConfig.userId == "") {
|
|
showLoginDialog();
|
|
} else {
|
|
EventBusUtil.fire(TabBarEvent(index));
|
|
pageController.animateToPage(
|
|
index,
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
}
|
|
}
|
|
|
|
void onPageChanged(int index) {
|
|
streamSink!.add(index);
|
|
}
|
|
|
|
//弹出登录
|
|
showLoginDialog() {
|
|
DialogManager()
|
|
..add(DialogBean(
|
|
dialogPriority: DialogPriority.highClear,
|
|
createDialogWidget: () => DialogUtil.createTipWidget(
|
|
context,
|
|
LoginDialog(
|
|
loginDialogKey,
|
|
onTap: (index) {
|
|
switch (index) {
|
|
case 0:
|
|
if (loginDialogKey.currentState != null) {
|
|
loginDialogKey.currentState!.Pop();
|
|
}
|
|
FunctionUtil.bottomSheetDialog(context, PhoneLoginDialog());
|
|
break;
|
|
case 1: //谷歌登录
|
|
Map<String, String> map = {"googleLogin": "googleLogin"};
|
|
invokeNativeMethod("googleLogin", map);
|
|
break;
|
|
}
|
|
},
|
|
),
|
|
"LoginDialog",
|
|
canceled: true)))
|
|
..show(context);
|
|
}
|
|
|
|
// 获取原生的值
|
|
invokeNativeMethod(String method, Map<String, dynamic> map) async {
|
|
dynamic args;
|
|
try {
|
|
args = await Global.method.invokeMethod(method, map);
|
|
} on PlatformException catch (e) {}
|
|
}
|
|
|
|
int last = 0;
|
|
|
|
//双击退出
|
|
Future<bool> doubleClickBack() async {
|
|
int now = DateTime.now().millisecondsSinceEpoch;
|
|
// if (currentIndex != 0) {
|
|
// EventBusUtil.fire(TabBarEvent(0));
|
|
// } else {
|
|
if (now - last > 2500) {
|
|
last = DateTime.now().millisecondsSinceEpoch;
|
|
EasyLoading.showToast("${S.of(context).Press_again}HissAi");
|
|
return Future.value(false);
|
|
} else {
|
|
exit(0);
|
|
}
|
|
// }
|
|
}
|
|
|
|
Future<void> _xy(String AppUrl) async {
|
|
String url = AppUrl;
|
|
if (await canLaunch(url)) {
|
|
await launch(url);
|
|
} else {
|
|
throw 'Could not launch $url';
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// TODO: implement dispose
|
|
streamController?.close();
|
|
subscription?.cancel();
|
|
_phoneLoginBackEvent?.cancel();
|
|
_tabBarJumpEvent?.cancel();
|
|
_loginSuccess?.cancel();
|
|
pageController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
///单击视频广告时调用。
|
|
///-点击广告的奖励数据作为【放置】进行传递。
|
|
@override
|
|
void onRewardedVideoAdClicked(IronSourceRVPlacement placement) {
|
|
// TODO: implement onRewardedVideoAdClicked
|
|
}
|
|
|
|
///当RewardedVideo广告视图即将关闭时调用。
|
|
@override
|
|
void onRewardedVideoAdClosed() {
|
|
// TODO: implement onRewardedVideoAdClosed
|
|
_viewModel.lookAdReward();
|
|
}
|
|
|
|
///当视频广告播放完毕时调用。
|
|
@override
|
|
void onRewardedVideoAdEnded() {
|
|
// TODO: implement onRewardedVideoAdEnded
|
|
}
|
|
|
|
///打开
|
|
@override
|
|
void onRewardedVideoAdOpened() {}
|
|
|
|
///当用户完成视频并应获得奖励时调用。
|
|
///-【放置】包含奖励数据。
|
|
///-如果您正在使用服务器对服务器的奖励回调,
|
|
///您可以忽略此事件并等待ironSource服务器的回调。
|
|
@override
|
|
void onRewardedVideoAdRewarded(IronSourceRVPlacement placement) {
|
|
debugPrint("完成");
|
|
}
|
|
|
|
///当“奖励视频”无法显示时调用。
|
|
///-您可以通过检查[error]来了解原因
|
|
@override
|
|
void onRewardedVideoAdShowFailed(IronSourceError error) {
|
|
// TODO: implement onRewardedVideoAdShowFailed
|
|
debugPrint("$error");
|
|
}
|
|
|
|
///当视频广告开始播放时调用。
|
|
@override
|
|
void onRewardedVideoAdStarted() {
|
|
// TODO: implement onRewardedVideoAdStarted
|
|
}
|
|
|
|
@override
|
|
void onRewardedVideoAvailabilityChanged(bool isAvailable) {
|
|
///广告是否可用
|
|
NetworkConfig.isAdAvailable = isAvailable;
|
|
}
|
|
}
|