game_list_page.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import 'dart:io';
  2. import 'package:android_intent/android_intent.dart';
  3. import 'package:cached_network_image/cached_network_image.dart';
  4. import 'package:device_apps/device_apps.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:sport/bean/game.dart';
  7. import 'package:sport/pages/game/game_detail.dart';
  8. import 'package:sport/router/navigator_util.dart';
  9. import 'package:sport/services/api/inject_api.dart';
  10. import 'package:sport/services/game_manager.dart';
  11. import 'package:sport/widgets/appbar.dart';
  12. import 'package:sport/widgets/decoration.dart';
  13. import 'package:sport/widgets/dialog/alert_dialog.dart';
  14. import 'package:sport/widgets/error.dart';
  15. import 'package:sport/widgets/image.dart';
  16. import 'package:sport/widgets/loading.dart';
  17. import 'package:sport/widgets/space.dart';
  18. class GameListPage extends StatefulWidget {
  19. @override
  20. State<StatefulWidget> createState() {
  21. return _GameListPageState();
  22. }
  23. }
  24. class _GameListPageState extends State<GameListPage> with InjectApi, WidgetsBindingObserver {
  25. int _type = 1;
  26. bool _isLoading = true;
  27. List<GameInfoData> _data = [];
  28. @override
  29. Future didChangeAppLifecycleState(AppLifecycleState state) async {
  30. super.didChangeAppLifecycleState(state);
  31. if (state == AppLifecycleState.resumed) {
  32. initData();
  33. }
  34. }
  35. @override
  36. void initState() {
  37. super.initState();
  38. WidgetsBinding.instance?.addObserver(this);
  39. initData();
  40. }
  41. @override
  42. void dispose() {
  43. super.dispose();
  44. WidgetsBinding.instance?.removeObserver(this);
  45. }
  46. initData() async {
  47. if (_data.isEmpty == true) {
  48. try {
  49. final data = await api.getGameAll();
  50. _data = data.results.where((element) => (element.sum?.durationTotal??0)~/ 60 > 0).toList();
  51. _data.sort((a, b) => (a.sum?.durationTotal??0) > (b.sum?.durationTotal??0) ? -1 : 1);
  52. _isLoading = false;
  53. } catch (e) {
  54. print(e);
  55. }
  56. }
  57. if (_data.isNotEmpty) {
  58. // var apps = await DeviceApps.getInstalledApplications();
  59. // apps.forEach((element) {
  60. // print("44444444444444444444444${element.packageName}");
  61. // });
  62. // if (Platform.isAndroid) {
  63. // for (var item in _data) {
  64. // if (item.packageNameAndroid != null) item.isLocal = await DeviceApps.isAppInstalled(item.packageNameAndroid!);
  65. // }
  66. // _data = _data.where((element) => element.isLocal == true).toList();
  67. // }
  68. }
  69. setState(() {});
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. return Scaffold(
  74. backgroundColor: Colors.white,
  75. body: CustomScrollView(slivers: <Widget>[
  76. buildSliverAppBar(
  77. context,
  78. "我的运动",
  79. // actions: <Widget>[
  80. // InkWell(
  81. // child: IconButton(
  82. // icon: Text(
  83. // _type == 1 ? "管理" : "完成",
  84. // style: _type == 1 ? TextStyle(fontSize: 15.0) : TextStyle(fontSize: 15.0, color: Theme.of(context).accentColor),
  85. // ),
  86. // onPressed: () {
  87. // setState(() {
  88. // _type == 2 ? _type = 1 : _type = 2;
  89. // });
  90. // },
  91. // ),
  92. // )
  93. // ],
  94. ),
  95. _data.isEmpty == true
  96. ? _isLoading == true
  97. ? SliverToBoxAdapter(child: RequestLoadingWidget())
  98. : SliverToBoxAdapter(
  99. child: RequestErrorWidget(
  100. null,
  101. msg: "你还没开始运动",
  102. assets: RequestErrorWidget.ASSETS_NO_INVITATION,
  103. ))
  104. : SliverList(
  105. delegate: SliverChildBuilderDelegate((content, index) {
  106. return _buildItemWidget(_type, data: _data[index]);
  107. }, childCount: _data.length),
  108. )
  109. ]));
  110. }
  111. Widget _buildItemWidget(int type, {GameInfoData? data}) {
  112. if(data == null)
  113. return Container();
  114. return Column(
  115. children: [
  116. InkWell(
  117. onTap: () => NavigatorUtil.goGameHistory(context, data),
  118. child: Container(
  119. padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 16.0),
  120. child: Row(
  121. children: <Widget>[
  122. // CircleAvatar(backgroundImage: CachedNetworkImageProvider(data.cover), radius: 35.0),
  123. ClipRRect(
  124. child: CachedNetworkImage(
  125. imageUrl: data.cover ?? "",
  126. fit: BoxFit.cover,
  127. height: 50.0,
  128. width: 50.0,
  129. ),
  130. // 也可控件一边圆角大小
  131. borderRadius: new BorderRadius.all(Radius.circular(10.0)),
  132. ),
  133. Expanded(
  134. child: Padding(
  135. padding: const EdgeInsets.symmetric(horizontal: 12.0),
  136. child: Column(
  137. crossAxisAlignment: CrossAxisAlignment.start,
  138. children: <Widget>[
  139. Text(
  140. "${data.name}",
  141. style: Theme.of(context).textTheme.headline3,
  142. ),
  143. Space(
  144. height: 8,
  145. ),
  146. Text(
  147. "已运动${(data.sum?.durationTotal??0) ~/ 60}分钟",
  148. style: Theme.of(context).textTheme.bodyText1!,
  149. ),
  150. // Space(
  151. // height: 2,
  152. // ),
  153. // Text(
  154. // "最近打开:${data.sum?.lastPlayAt ?? "未进行运动"}",
  155. // style: Theme.of(context).textTheme.bodyText1!,
  156. // )
  157. ],
  158. ),
  159. ),
  160. ),
  161. type == 1
  162. ? arrowRight5()
  163. : InkWell(
  164. child: Padding(padding: EdgeInsets.fromLTRB(12.0, 12.0, 0, 12.0), child: Image.asset("lib/assets/img/list_icon_del.png")),
  165. onTap: () async {
  166. if (await showDialog(
  167. context: context,
  168. builder: (context) => CustomAlertDialog(title: '是否删除运动', ok: () => Navigator.of(context).pop(true)),
  169. ) ==
  170. true) {
  171. GameManager.deleteFile(data);
  172. if (Platform.isAndroid) {
  173. AndroidIntent intent = AndroidIntent(
  174. action: 'android.intent.action.DELETE',
  175. data: 'package:${data.packageNameAndroid}',
  176. );
  177. await intent.launch();
  178. }
  179. }
  180. //
  181. },
  182. )
  183. ],
  184. ),
  185. ),
  186. ),
  187. Divider(height: 1.0,),
  188. ],
  189. );
  190. }
  191. }