game_list_page.dart 6.7 KB

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