game_list_page.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. Expanded(
  119. child: Padding(
  120. padding: const EdgeInsets.symmetric(horizontal: 12.0),
  121. child: Column(
  122. crossAxisAlignment: CrossAxisAlignment.start,
  123. children: <Widget>[
  124. Text(
  125. "${data.name}",
  126. style: Theme.of(context).textTheme.headline3,
  127. ),
  128. Space(
  129. height: 3,
  130. ),
  131. Text(
  132. "总时长:${data.sum.durationTotal ~/ 60 != 0 ? "${data.sum.durationTotal ~/ 60}时" : ""}${data.sum.durationTotal % 60}分钟",
  133. style: Theme.of(context).textTheme.bodyText1,
  134. ),Space(
  135. height: 2,
  136. ),
  137. Text(
  138. "最近打开:${data.sum.lastPlayAt ?? "未进行运动"}",
  139. style: Theme.of(context).textTheme.bodyText1,
  140. )
  141. ],
  142. ),
  143. ),
  144. ),
  145. type == 1
  146. ? arrowRight5()
  147. : InkWell(
  148. child: Padding(padding: EdgeInsets.fromLTRB(12.0, 12.0, 0, 12.0), child: Image.asset("lib/assets/img/list_icon_del.png")),
  149. onTap: () async {
  150. if (await showDialog(
  151. context: context,
  152. builder: (context) => CustomAlertDialog(title: '是否删除运动', ok: () => Navigator.of(context).pop(true)),
  153. ) ==
  154. true) {
  155. GameManager.deleteFile(data);
  156. if (Platform.isAndroid) {
  157. AndroidIntent intent = AndroidIntent(
  158. action: 'android.intent.action.DELETE',
  159. data: 'package:${data.packageNameAndroid}',
  160. );
  161. await intent.launch();
  162. }
  163. }
  164. //
  165. },
  166. )
  167. ],
  168. ),
  169. ],
  170. ),
  171. ),
  172. );
  173. }
  174. }