| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- import 'dart:convert';
- import 'dart:io';
- import 'package:amap_location/amap_location.dart';
- import 'package:amap_location/amap_location_option.dart';
- import 'package:bbyyy/beans/coupon_bean_entity.dart';
- import 'package:bbyyy/beans/user_bean_entity.dart';
- import 'package:bbyyy/https/MyDio.dart';
- import 'package:bbyyy/https/my_request.dart';
- import 'package:bbyyy/my_tools/dims.dart';
- import 'package:bbyyy/my_tools/easy_loading/easy_loading.dart';
- import 'package:bbyyy/my_tools/event_bus.dart';
- import 'package:bbyyy/my_tools/my_cookie.dart';
- import 'package:bbyyy/my_tools/my_datas.dart';
- import 'package:bbyyy/my_tools/my_tools.dart';
- import 'package:bbyyy/my_tools/pop_up_queue.dart';
- import 'package:bbyyy/nsq/nsq.dart' as Nsq;
- import 'package:bbyyy/paegs/gang_page/gang_page.dart';
- import 'package:bbyyy/paegs/home_page/home_page.dart';
- import 'package:bbyyy/paegs/mine_page/mine_page.dart';
- import 'package:bbyyy/paegs/msg_page/msg_page.dart';
- import 'package:bbyyy/paegs/root_page/root_page_view.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:permission_handler/permission_handler.dart';
- class RootPage extends StatefulWidget {
- @override
- _RootPageState createState() => _RootPageState();
- }
- class _RootPageState extends State<RootPage> {
- PageController pageController = PageController(initialPage: 0);
- DateTime lastPopTime;
- @override
- void initState() {
- super.initState();
- MyCookie().initP();
- getLocation();
- Nsq.ConnectServer(
- Nsq.handlerMessage, // 接收消息处理逻辑
- // serverIp: '172.16.104.14',
- serverIp: MyCookie().server,
- // 服务端返回
- serverPort: 4150,
- // 服务端返回
- topic: '${MyCookie().getUID()}',
- // 主题订阅,服务端返回
- channel: '${MyCookie().getUID()}',
- // 服务端返回uid
- clientId: '客户uid',
- // 服务端返回uid
- hostname: '主机名', // 服务端返回
- );
- EventBus().emit('检查粘贴板');
- EventBus().on('ChangePage', (arg) {
- if (RootPageView().bNIndex == 3) {
- queryPersonalInformation();
- }
- pageController.jumpToPage(RootPageView().bNIndex);
- setState(() {});
- });
- MyData().init(context);
- queryPersonalInformation();
- checkForUpdates(context);
- Future.delayed(Duration(seconds: 1), () {
- checkCouponEvent();
- });
- }
- @override
- void dispose() {
- // TODO: implement dispose
- EventBus().off('ChangePage');
- AMapLocationClient.stopLocation();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return WillPopScope(
- onWillPop: () async {
- if (lastPopTime == null ||
- DateTime.now().difference(lastPopTime) > Duration(seconds: 2)) {
- lastPopTime = DateTime.now();
- EasyLoading.showToast('再按一次退出');
- } else {
- lastPopTime = DateTime.now();
- await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
- }
- return false;
- },
- child: Scaffold(
- resizeToAvoidBottomInset: false,
- backgroundColor: Colors.transparent,
- body: Column(
- children: [
- Expanded(
- child: PageView(
- controller: pageController,
- children: [HomePage(), GangPage(), MsgPage(), MinePage()],
- physics: NeverScrollableScrollPhysics(),
- ),
- ),
- RootPageView().bottomNavigationBar(),
- ],
- ),
- ),
- );
- }
- void queryPersonalInformation() {
- MyDio().query({
- "key": "user",
- "filters": {
- "conditions": ["uid == ${MyCookie().getUID()}"]
- },
- "dims": userDims,
- "paging": [1, 20]
- }, (response, hasError) {
- if (!hasError) {
- UserBeanEntity entity =
- UserBeanEntity().fromJson(json.decode(response.data.toString()));
- MyCookie().userBean = entity.data.data[0];
- EventBus().emit('userChange');
- }
- }, (error) {});
- }
- Future<void> getLocation() async {
- var locationStatus = await Permission.location.status;
- var cameraStatus = await Permission.camera.status;
- print('Permission.location.status--------------$locationStatus');
- print('Permission.camera.status--------------$cameraStatus');
- if (locationStatus.isDenied) {
- await Permission.location.request().then((value) async {
- print('Permission.location.request()-----$value');
- if (value == PermissionStatus.granted) {
- await AMapLocationClient.startup(new AMapLocationOption(
- desiredAccuracy:
- CLLocationAccuracy.kCLLocationAccuracyHundredMeters));
- await AMapLocationClient.getLocation(true).then((value) async {
- if (Platform.isAndroid) {
- MyCookie().location = MyLocation(
- province: value.province,
- city: value.city,
- formattedAddress: value.formattedAddress,
- district: value.district,
- latitude: value.latitude,
- longitude: value.longitude);
- EventBus().emit('getLocation');
- } else if (Platform.isIOS) {
- getAddressByLatitudeAndLongitude(value.latitude, value.longitude);
- }
- });
- }
- });
- } else if (locationStatus.isGranted) {
- await AMapLocationClient.startup(new AMapLocationOption(
- desiredAccuracy:
- CLLocationAccuracy.kCLLocationAccuracyHundredMeters));
- await AMapLocationClient.getLocation(true).then((value) async {
- if (Platform.isAndroid) {
- MyCookie().location = MyLocation(
- province: value.province,
- city: value.city,
- formattedAddress: value.formattedAddress,
- district: value.district,
- latitude: value.latitude,
- longitude: value.longitude);
- if ( value.province!= '四川省' ||
- value.city!= '成都市') {
- EventBus().emit('getLocation');
- }
- } else if (Platform.isIOS) {
- getAddressByLatitudeAndLongitude(value.latitude, value.longitude);
- }
- });
- }
- if (cameraStatus.isDenied) {
- await Permission.camera.request().then((value) {
- print('Permission.camera.request()-----$value');
- });
- }
- }
- //查询优惠券活动
- checkCouponEvent() {
- MyDio().query({
- "key": "coupon_distribute",
- "filters": {
- "conditions": ["valid==true"],
- },
- "dims": couponDistributeDims,
- "paging": [1, 100]
- }, (response, hasError) {
- if (!hasError) {
- CouponBeanEntity entity =
- CouponBeanEntity().fromJson(json.decode(response.data.toString()));
- PopUpQueue().coupon.addAll(entity.data.data);
- }
- }, (error) {});
- }
- }
|