123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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<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) {
- 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: <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}) {
- 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: <Widget>[
- Row(
- children: <Widget>[
- CircleAvatar(backgroundImage: CachedNetworkImageProvider(data.cover), radius: 35.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: 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();
- }
- }
- //
- },
- )
- ],
- ),
- ],
- ),
- ),
- );
- }
- }
|