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/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) { final data = await api.getGameAll(); _data = data.results ?? []; _isLoading = false; } if (_data.isNotEmpty) { // var apps = await DeviceApps.getInstalledApplications(); // apps.forEach((element) { // print("44444444444444444444444${element.packageName}"); // }); if (Platform.isAndroid) { for (var item in _data) { 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}) { return Container( decoration: card(), margin: EdgeInsets.fromLTRB(12, 6, 12, 6), padding: const EdgeInsets.all(12.0), child: InkWell( onTap: () => NavigatorUtil.goGameDetails(context, details: data), child: Column( children: [ Row( children: [ CircleAvatar(backgroundImage: CachedNetworkImageProvider(data.cover), radius: 35.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: 3, ), Text( "总时长:${data.sum.durationTotal ~/ 60 != 0 ? "${data.sum.durationTotal ~/ 60}时" : ""}${data.sum.durationTotal % 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(); } } // }, ) ], ), ], ), ), ); } }