video_slides.dart 13.1 KB
import 'package:Parlando/poem/poem_router.dart';
import 'package:Parlando/routers/fluro_navigator.dart';
import 'package:animated_radial_menu/animated_radial_menu.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import '../../events/trans_event.dart';
import '../../net/dio_utils.dart';
import '../../net/http_api.dart';
import '../theme/tik_video_player.dart';

class VideoSlides extends StatefulWidget {
  const VideoSlides({
    Key? key,
    required this.poemId,
    required this.poemType,
    required this.url,
    this.isPraise = false,
    this.isCollect = false,
    required this.videoId,
  }) : super(key: key);
  final int videoId;
  final int poemId;
  final int poemType;
  final String url;
  final bool isPraise;
  final bool isCollect;

  @override
  VideoSlidesState createState() => VideoSlidesState();
}

class VideoSlidesState extends State<VideoSlides> {
  bool isSharing = false;
  bool isPraise = false;
  bool isCollect = false;

  @override
  void initState() {
    super.initState();
    isPraise = widget.isPraise;
    isCollect = widget.isCollect;
    recordVideoPlayStatus();
  }

  Future<void> recordVideoPlayStatus() async {
    String url = '${HttpApi.addView}/${widget.videoId}';
    // 统计观看数
    DioUtils.instance.asyncRequestNetwork(
      Method.get,
      url,
      params: [],
      onSuccess: (data) {},
      onError: (code, msg) {},
    );
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: AlignmentDirectional.center,
      children: [
        Container(
          width: double.infinity,
          height: double.infinity,
          constraints: const BoxConstraints(
            maxWidth: double.infinity,
            maxHeight: double.infinity,
          ),
          decoration: const BoxDecoration(
            color: Colors.black,
          ),
          child: TikVideoPlayer(
            path: widget.url,
            videoType: VideoType.network,
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height * 1,
            autoPlay: true,
            looping: true,
            showControls: false,
            allowFullScreen: false,
            allowPlaybackSpeedMenu: false,
          ),
        ),
        Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.end,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Column(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  width: 60,
                  height: 240,
                  decoration: const BoxDecoration(
                    color: Color(0x4DC4C4C4),
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(40),
                      bottomRight: Radius.circular(0),
                      topLeft: Radius.circular(40),
                      topRight: Radius.circular(0),
                    ),
                    shape: BoxShape.rectangle,
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Padding(
                        padding:
                            const EdgeInsetsDirectional.fromSTEB(0, 10, 0, 0),
                        child: Row(
                          mainAxisSize: MainAxisSize.max,
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Column(
                              mainAxisSize: MainAxisSize.max,
                              children: [
                                Padding(
                                  padding: const EdgeInsetsDirectional.fromSTEB(
                                      0, 20, 0, 0),
                                  child: InkWell(
                                    onTap: () {
                                      isPraise =
                                          isPraise == false ? true : false;
                                      setState(() {});
                                      String url =
                                          '${HttpApi.praise}/${widget.videoId}';
                                      DioUtils.instance.asyncRequestNetwork(
                                        Method.post,
                                        url,
                                        params: [],
                                        onSuccess: (data) {},
                                        onError: (code, msg) {},
                                      );
                                    },
                                    child: Container(
                                      width: 40,
                                      height: 40,
                                      decoration: const BoxDecoration(
                                        color: Color(0x69EEEEEE),
                                        shape: BoxShape.circle,
                                      ),
                                      child: Icon(
                                        Icons.favorite_rounded,
                                        color: isPraise
                                            ? Colors.red
                                            : Colors.white,
                                        size: 20,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding:
                            const EdgeInsetsDirectional.fromSTEB(0, 10, 0, 0),
                        child: Row(
                          mainAxisSize: MainAxisSize.max,
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Column(
                              mainAxisSize: MainAxisSize.max,
                              children: [
                                Padding(
                                  padding: const EdgeInsetsDirectional.fromSTEB(
                                      0, 20, 0, 0),
                                  child: InkWell(
                                    onTap: () async {
                                      isCollect =
                                          isCollect == false ? true : false;
                                      setState(() {});
                                      String url =
                                          '${HttpApi.collect}/${widget.videoId}';
                                      DioUtils.instance.asyncRequestNetwork(
                                        Method.post,
                                        url,
                                        params: [],
                                        onSuccess: (data) {},
                                        onError: (code, msg) {},
                                      );
                                    },
                                    child: Container(
                                      width: 40,
                                      height: 40,
                                      decoration: const BoxDecoration(
                                        color: Color(0x69EEEEEE),
                                        shape: BoxShape.circle,
                                      ),
                                      child: Icon(
                                        Icons.star,
                                        color: isCollect
                                            ? Colors.red
                                            : Colors.white,
                                        size: 20,
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding:
                            const EdgeInsetsDirectional.fromSTEB(0, 10, 0, 0),
                        child: Row(
                          mainAxisSize: MainAxisSize.max,
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Column(
                              mainAxisSize: MainAxisSize.max,
                              children: [
                                Padding(
                                  padding: const EdgeInsetsDirectional.fromSTEB(
                                      0, 20, 0, 0),
                                  child: InkWell(
                                    onTap: () async {
                                      isSharing = true;
                                      setState(() {});
                                      await downVideo(widget.url);
                                      isSharing = false;
                                      await Share.shareFiles([tempVideoPath],
                                          text: '一言以世界 临境不蹉跎');
                                    },
                                    child: Container(
                                      width: 40,
                                      height: 40,
                                      decoration: const BoxDecoration(
                                        color: Color(0x69EEEEEE),
                                        shape: BoxShape.circle,
                                      ),
                                      child: isSharing
                                          ? GFProgressBar(
                                              percentage: currentProgress,
                                              backgroundColor: Colors.black26,
                                              progressBarColor: GFColors.DANGER)
                                          : const Icon(
                                              Icons.share_rounded,
                                              color: Colors.white,
                                              size: 20,
                                            ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
        Positioned(
          bottom: -48,
          child: Container(
            alignment: Alignment.bottomCenter,
            width: 160,
            height: 180,
            child: RadialMenu(
              centerButtonAlignment: Alignment.bottomLeft,
              children: [
                RadialButton(
                    icon: const Icon(Icons.video_call_outlined),
                    buttonColor: Colors.teal,
                    onPress: () {
                      eventBus.fire(TransEvent());
                      String url =
                          '${PoemRouter.poemRecordVideoPage}?id=${widget.videoId}&poem=${widget.poemId}&type=${widget.poemType}';
                      NavigatorUtils.push(
                        context,
                        url,
                      );
                    }),
                RadialButton(
                    icon: const Icon(Icons.mic_none_outlined),
                    buttonColor: Colors.green,
                    onPress: () {
                      eventBus.fire(TransEvent());
                      NavigatorUtils.push(
                        context,
                        '${PoemRouter.poemRecordAudioPage}?id=${widget.videoId}&poem=${widget.poemId}&type=${widget.poemType}',
                      );
                    }),
              ],
            ),
          ),
        ),
      ],
    );
  }

  Future<String> getPhoneLocalPath() async {
    final directory = Theme.of(context).platform == TargetPlatform.android
        ? await getExternalStorageDirectory()
        : await getApplicationDocumentsDirectory();
    return directory!.path;
  }

  double currentProgress = 0.0;
  String tempVideoPath = "";

  Future<void> downVideo(String url) async {
    String savePath = await getPhoneLocalPath();
    String appName = "my_parlando.mp4";
    Dio dio = Dio();
    tempVideoPath = "$savePath$appName";
    Response response = await dio.download(url, tempVideoPath,
        onReceiveProgress: (received, total) {
      if (total != -1) {
        currentProgress = received / total;
        setState(() {});
      }
    });
  }
}