Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
januwA committed Jun 22, 2019
1 parent eb8aac5 commit a642ae3
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 89 deletions.
57 changes: 40 additions & 17 deletions lib/pages/detail/detail.store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ abstract class _DetailStore with Store {
getDetailData();
}

@observable
String weekDataerror = '';

@observable
String videoSrcerror = '';
final ErrorState error = ErrorState();

@observable
DetailData detailData;
Expand All @@ -45,7 +41,11 @@ abstract class _DetailStore with Store {
@observable
bool isPageLoading = true;

var client = http.Client();
/// get detail
http.Client client = http.Client();

/// get video src
http.Client clientSrc;

@action
void setSrc(String s) {
Expand All @@ -66,7 +66,7 @@ abstract class _DetailStore with Store {
isPageLoading = false;
// initVideoPlaer();
} else {
weekDataerror = r.body.toString();
error.detail = r.body.toString();
isPageLoading = false;
}
} catch (_) {
Expand All @@ -76,17 +76,26 @@ abstract class _DetailStore with Store {

/// 获取指定集的src
@action
Future<void> getVideoSrc(Function onErrorCb) async {
var e = detailData.playUrlTab[currentPlayIndex];
Future<void> getVideoSrc({
String id,
Function onError,
}) async {
src = '';
var url = Uri.http(baseUrl, videoSrcUrl, {"id": e.id});
var r = await http.get(url);
if (r.statusCode == 200) {
src = jsonDecode(r.body)['src'];
} else {
// error
videoSrcerror = r.body.toString();
onErrorCb();
try {
clientSrc?.close();
src = '';
var url = Uri.http(baseUrl, videoSrcUrl, {"id": id});
clientSrc = http.Client();
var r = await clientSrc.get(url);
if (r.statusCode == HttpStatus.ok) {
src = jsonDecode(r.body)['src'];
} else {
// error
error.src = r.body.toString();
onError();
}
} on http.ClientException catch (_) {
/// 抓取中断错误
}
}

Expand All @@ -98,6 +107,20 @@ abstract class _DetailStore with Store {
@override
void dispose() {
client?.close();
clientSrc?.close();
super.dispose();
}
}

/// error model
class ErrorState = _ErrorState with _$ErrorState;

abstract class _ErrorState with Store {
/// 获取detail数据时错误
@observable
String detail;

/// 获取video src时错误
@observable
String src;
}
71 changes: 37 additions & 34 deletions lib/pages/detail/detail.store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 24 additions & 13 deletions lib/pages/detail/detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class _DetailPageState extends State<DetailPage> {
Widget build(BuildContext context) {
return Observer(
builder: (_) {
if (!isNull(detailStore.weekDataerror)) {
if (!isNull(detailStore.error.detail)) {
return HttpErrorPage(
body: Text(detailStore.weekDataerror),
body: Text(detailStore.error.detail),
);
}

Expand All @@ -63,7 +63,10 @@ class _DetailPageState extends State<DetailPage> {
body: ListView(
children: <Widget>[
Observer(
builder: (_) => VideoBox(src: detailStore.src),
builder: (_) => VideoBox(
key: ValueKey(detailStore.src),
src: detailStore.src,
),
),

/// anime的信息资料
Expand Down Expand Up @@ -91,6 +94,7 @@ class _DetailPageState extends State<DetailPage> {
children: <Widget>[
for (PlayUrlTab t in detailStore.detailData.playUrlTab)
Padding(
key: ValueKey(t.id),
padding: EdgeInsets.symmetric(horizontal: 4.0),
child: PlayTab(t: t)),
],
Expand All @@ -111,33 +115,40 @@ class PlayTab extends StatelessWidget {
@override
Widget build(BuildContext context) {
final detailStore = Provider.of<DetailStore>(context);
var index = detailStore.detailData.playUrlTab.indexOf(t);
int index = detailStore.detailData.playUrlTab.indexOf(t);
return Observer(
builder: (_) => FlatButton(
color: index == detailStore.currentPlayIndex
? Colors.blue[400]
: Colors.grey[300],
child: Text(t.text),
onPressed: () async {
detailStore.setCurrentPlayIndex(index);
if (t.isBox) {
// 网盘资源
if (await canLaunch(t.src)) {
await launch(t.src);
} else {
throw 'Could not launch ${t.src}';
}
} else {
detailStore.setSrc('');
// 视频资源,准备切换播放点击的视频
detailStore.setCurrentPlayIndex(index);
detailStore.getVideoSrc(() {
alertHttpGetError(
context: context,
title: '提示',
text: detailStore.videoSrcerror,
text: '无法启动${t.src}',
okText: '确定',
);
});
}
} else {
// 视频资源,准备切换播放点击的视频
detailStore.getVideoSrc(
id: t.id,
onError: () {
alertHttpGetError(
context: context,
title: '提示',
text: detailStore.error.src,
okText: '确定',
);
},
);
}
},
),
Expand Down
54 changes: 30 additions & 24 deletions lib/shared/widgets/video_box/video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,42 @@ class _VideoBoxState extends State<VideoBox> {
}

@override
Widget build(BuildContext context) {
if (isNull(widget.src)) return VideoLoading();
void dispose() {
videoStore.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Observer(
builder: (_) => MultiProvider(
providers: [Provider<VideoStore>.value(value: videoStore)],
child: GestureDetector(
onTap: () =>
videoStore.showVideoCtrl(!videoStore.isShowVideoCtrl),
child: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
videoStore.isVideoLoading
? VideoLoading()
: Container(
color: Colors.black,
child: Center(
child: AspectRatio(
aspectRatio:
videoStore.videoCtrl.value.aspectRatio,
child: VideoPlayer(videoStore.videoCtrl),
builder: (_) => isNull(videoStore.src)
? VideoLoading()
: MultiProvider(
providers: [Provider<VideoStore>.value(value: videoStore)],
child: GestureDetector(
onTap: () =>
videoStore.showVideoCtrl(!videoStore.isShowVideoCtrl),
child: Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
videoStore.isVideoLoading
? VideoLoading()
: Container(
color: Colors.black,
child: Center(
child: AspectRatio(
aspectRatio:
videoStore.videoCtrl.value.aspectRatio,
child: VideoPlayer(videoStore.videoCtrl),
),
),
),
),
PlayButton(),
VideoBottomCtrl(),
],
PlayButton(),
VideoBottomCtrl(),
],
),
),
),
),
);
}
}
Expand Down
11 changes: 10 additions & 1 deletion lib/shared/widgets/video_box/video.store.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/services.dart';
import 'package:mobx/mobx.dart';
import 'package:validators/validators.dart';
import 'package:video_player/video_player.dart';
part 'video.store.g.dart';

Expand All @@ -10,10 +11,14 @@ abstract class _VideoStore with Store {
initVideoPlaer();
}

/// 播放地址
@observable
String src;

@observable
VideoPlayerController videoCtrl;

/// 加载视频中...
@observable
bool isVideoLoading = true;

Expand Down Expand Up @@ -80,10 +85,11 @@ abstract class _VideoStore with Store {
/// 初始化viedo控制器
@action
Future<void> initVideoPlaer() async {
if (isNull(src)) return;
// 尝试关闭上一个video
videoCtrl?.pause();
videoCtrl?.setVolume(0.0);

isVideoLoading = true;
videoCtrl = VideoPlayerController.network(src);

Expand All @@ -96,6 +102,7 @@ abstract class _VideoStore with Store {
videoCtrl.addListener(videoListenner);
}

/// 开启声音或关闭
void setVolume() {
if (videoCtrl.value.volume > 0) {
videoCtrl.setVolume(0.0);
Expand All @@ -104,10 +111,12 @@ abstract class _VideoStore with Store {
}
}

/// 快进
void seekTo(double v) {
videoCtrl.seekTo(Duration(seconds: (v * duration.inSeconds).toInt()));
}

/// 播放或暂停
@action
void togglePlay() {
if (videoCtrl.value.isPlaying) {
Expand Down

0 comments on commit a642ae3

Please sign in to comment.