poem_video_player.dart 4.64 KB
import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/material.dart';
import 'package:Parlando/res/resources.dart';
import 'package:Parlando/routers/fluro_navigator.dart';
import 'package:Parlando/widgets/my_app_bar.dart';
import 'package:Parlando/extension/int_extension.dart';
import 'package:path_provider/path_provider.dart';

import '../../net/dio_utils.dart';
import '../../net/http_api.dart';
import '../models/poem_entity.dart';
import '../poem_router.dart';

class PoemVideoPlayer extends StatefulWidget {
  final String url;
  final String? title;
  final int id;
  final int type;
  final int poemId;

  const PoemVideoPlayer({
    Key? key,
    required this.url,
    this.title,
    required this.id,
    required this.type,
    required this.poemId,
  }) : super(key: key);

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

class PoemVideoPlayerState extends State<PoemVideoPlayer> {
  final FijkPlayer player = FijkPlayer();
  PoemData? _poem;

  PoemVideoPlayerState();

  Future<String> _getDir() async {
    final directory = await getApplicationDocumentsDirectory();
    return directory.path;
  }

  @override
  void initState() {
    super.initState();
    _getDir().then((value) {
      String path = "$value/${widget.url}";
      player.setDataSource(
        path,
        autoPlay: true,
      );
      player.setLoop(0);
    });

    fetchPoemDetail();
  }

  Future<void> fetchPoemDetail() async {
    DioUtils.instance.asyncRequestNetwork<PoemEntity>(
      Method.get,
      "${HttpApi.poem}/${widget.poemId}",
      params: [],
      onSuccess: (data) {
        _poem = data!.data!;
        setState(() {});
      },
      onError: (code, msg) {},
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: MyAppBar(
          homeMenuHeader: Container(
            alignment: Alignment.center,
            child: Text(
              widget.title ?? "视频播放",
              style: const TextStyle(
                color: Colors.white,
              ),
            ),
          ),
        ),
        body: Stack(
          children: [
            FijkView(
              height: MediaQuery.of(context).size.height,
              player: player,
              fit: FijkFit.fill,
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(10.px, 30.px, 10.px, 10.px),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    _poem != null ? _poem!.title.toString() : "",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.px,
                    ),
                  ),
                  Gaps.vGap10,
                  Text(
                    _poem != null ? _poem!.author.toString() : "",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 15.px,
                    ),
                  ),
                  Gaps.vGap5,
                  Text(
                    _poem != null ? _poem!.content.toString() : "",
                    style: TextStyle(
                      color: Colors.white,
                      fontFamily: "ZCOOLXiaoWei",
                      fontSize: 25.px,
                    ),
                  ),
                  Gaps.vGap5,
                  Text(
                    "临者:老魔取西经",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 15.px,
                    ),
                  ),
                  Text(
                    "2022年01月25日 辰时三刻",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 15.px,
                    ),
                  ),
                ],
              ),
            ),
            Positioned(
              bottom: 10.px,
              right: 20.px,
              child: ElevatedButton(
                onPressed: () {
                  NavigatorUtils.push(context,
                      '${PoemRouter.poemPublish}?data=${widget.url}&id=${widget.id}&type=${widget.type}',
                      clearStack: true);
                },
                style: TextButton.styleFrom(
                  primary: Colors.white,
                  backgroundColor: Colors.black54,
                ),
                child: Text(
                  "下一步",
                  style: TextStyle(fontSize: 15.px),
                ),
              ),
            ),
          ],
        ));
  }

  @override
  void dispose() {
    player.release();
    super.dispose();
  }
}