123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- 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<StatefulWidget> createState() {
- return _GameListPageState();
- }
- }
- class _GameListPageState extends State<GameListPage> with InjectApi, WidgetsBindingObserver {
- int _type = 1;
- bool _isLoading = true;
- List<GameInfoData> _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: <Widget>[
- buildSliverAppBar(
- context,
- "我的运动",
- // actions: <Widget>[
- // 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: <Widget>[
- // 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: <Widget>[
- 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,),
- ],
- );
- }
- }
|