import 'dart:io'; import 'package:android_intent/android_intent.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:device_apps/device_apps.dart'; import 'package:flutter/material.dart'; import 'package:sport/bean/game.dart'; import 'package:sport/pages/game/game_detail.dart'; import 'package:sport/router/navigator_util.dart'; import 'package:sport/services/api/inject_api.dart'; import 'package:sport/services/game_manager.dart'; import 'package:sport/widgets/appbar.dart'; import 'package:sport/widgets/decoration.dart'; import 'package:sport/widgets/dialog/alert_dialog.dart'; import 'package:sport/widgets/error.dart'; import 'package:sport/widgets/image.dart'; import 'package:sport/widgets/loading.dart'; import 'package:sport/widgets/space.dart'; class GameListPage extends StatefulWidget { @override State createState() { return _GameListPageState(); } } class _GameListPageState extends State with InjectApi, WidgetsBindingObserver { int _type = 1; bool _isLoading = true; List _data = []; @override Future didChangeAppLifecycleState(AppLifecycleState state) async { super.didChangeAppLifecycleState(state); if (state == AppLifecycleState.resumed) { initData(); } } @override void initState() { super.initState(); WidgetsBinding.instance?.addObserver(this); initData(); } @override void dispose() { super.dispose(); WidgetsBinding.instance?.removeObserver(this); } initData() async { if (_data.isEmpty == true) { try { final data = await api.getGameAll(); _data = data.results.where((element) => (element.sum?.durationTotal??0)~/ 60 > 0).toList(); _data.sort((a, b) => (a.sum?.durationTotal??0) > (b.sum?.durationTotal??0) ? -1 : 1); _isLoading = false; } catch (e) { print(e); } } if (_data.isNotEmpty) { // var apps = await DeviceApps.getInstalledApplications(); // apps.forEach((element) { // print("44444444444444444444444${element.packageName}"); // }); // if (Platform.isAndroid) { // for (var item in _data) { // if (item.packageNameAndroid != null) item.isLocal = await DeviceApps.isAppInstalled(item.packageNameAndroid!); // } // _data = _data.where((element) => element.isLocal == true).toList(); // } } setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: CustomScrollView(slivers: [ buildSliverAppBar( context, "我的运动", // actions: [ // InkWell( // child: IconButton( // icon: Text( // _type == 1 ? "管理" : "完成", // style: _type == 1 ? TextStyle(fontSize: 15.0) : TextStyle(fontSize: 15.0, color: Theme.of(context).accentColor), // ), // onPressed: () { // setState(() { // _type == 2 ? _type = 1 : _type = 2; // }); // }, // ), // ) // ], ), _data.isEmpty == true ? _isLoading == true ? SliverToBoxAdapter(child: RequestLoadingWidget()) : SliverToBoxAdapter( child: RequestErrorWidget( null, msg: "你还没开始运动", assets: RequestErrorWidget.ASSETS_NO_INVITATION, )) : SliverList( delegate: SliverChildBuilderDelegate((content, index) { return _buildItemWidget(_type, data: _data[index]); }, childCount: _data.length), ) ])); } Widget _buildItemWidget(int type, {GameInfoData? data}) { if(data == null) return Container(); return Column( children: [ InkWell( onTap: () => NavigatorUtil.goGameHistory(context, data), child: Container( padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0), child: Row( children: [ // CircleAvatar(backgroundImage: CachedNetworkImageProvider(data.cover), radius: 35.0), ClipRRect( child: CachedNetworkImage( imageUrl: data.cover ?? "", fit: BoxFit.cover, height: 50.0, width: 50.0, ), // 也可控件一边圆角大小 borderRadius: new BorderRadius.all(Radius.circular(10.0)), ), Expanded( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 12.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( "${data.name}", style: Theme.of(context).textTheme.headline3, ), Space( height: 8, ), Text( "已运动${(data.sum?.durationTotal??0) ~/ 60}分钟", style: Theme.of(context).textTheme.bodyText1!, ), // Space( // height: 2, // ), // Text( // "最近打开:${data.sum?.lastPlayAt ?? "未进行运动"}", // style: Theme.of(context).textTheme.bodyText1!, // ) ], ), ), ), type == 1 ? arrowRight5() : InkWell( child: Padding(padding: EdgeInsets.fromLTRB(12.0, 12.0, 0, 12.0), child: Image.asset("lib/assets/img/list_icon_del.png")), onTap: () async { if (await showDialog( context: context, builder: (context) => CustomAlertDialog(title: '是否删除运动', ok: () => Navigator.of(context).pop(true)), ) == true) { GameManager.deleteFile(data); if (Platform.isAndroid) { AndroidIntent intent = AndroidIntent( action: 'android.intent.action.DELETE', data: 'package:${data.packageNameAndroid}', ); await intent.launch(); } } // }, ) ], ), ), ), Divider(height: 1.0,), ], ); } }