| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- import 'package:flutter/material.dart';
- import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
- import 'package:ijkplayer_example/i18n/i18n.dart';
- class InOverlayPage extends StatefulWidget {
- @override
- _InOverlayPageState createState() => _InOverlayPageState();
- }
- class _InOverlayPageState extends State<InOverlayPage> {
- IjkMediaController controller = IjkMediaController();
- OverlayEntry entry;
- @override
- void initState() {
- super.initState();
- controller.setDataSource(
- DataSource.network(
- "http://img.ksbbs.com/asset/Mon_1703/05cacb4e02f9d9e.mp4",
- ),
- autoPlay: true,
- );
- }
- @override
- void deactivate() {
- super.deactivate();
- entry?.remove();
- entry = null;
- }
- @override
- void dispose() {
- controller?.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(currentI18n.overlayPageTitle),
- ),
- body: ListView(
- children: <Widget>[
- AspectRatio(
- child: IjkPlayer(
- mediaController: controller,
- ),
- aspectRatio: 1,
- ),
- FlatButton(
- child: Text("showInOverlay"),
- onPressed: showInOverlay,
- ),
- ],
- ),
- );
- }
- void showInOverlay() async {
- var info = await controller.getVideoInfo();
- OverlayEntry _entry;
- _entry = OverlayEntry(
- builder: (BuildContext context) {
- return OverlayWidget(
- controller: controller,
- initVideoInfo: info,
- onTapClose: () {
- _entry?.remove();
- },
- );
- },
- );
- Overlay.of(context).insert(_entry);
- }
- }
- class OverlayWidget extends StatefulWidget {
- final VideoInfo initVideoInfo;
- final IjkMediaController controller;
- final Function onTapClose;
- const OverlayWidget({
- Key key,
- this.initVideoInfo,
- this.controller,
- this.onTapClose,
- }) : super(key: key);
- @override
- _OverlayWidgetState createState() => _OverlayWidgetState();
- }
- const double _overlayWidth = 100;
- class _OverlayWidgetState extends State<OverlayWidget> {
- double dx = 0;
- double dy = 1;
- @override
- Widget build(BuildContext context) {
- return NotificationListener<OffsetNotication>(
- onNotification: _onOffsetNoticiation,
- child: Align(
- alignment: Alignment(dx, dy),
- child: Container(
- width: _overlayWidth,
- child: AspectRatio(
- aspectRatio: widget.initVideoInfo.ratio,
- child: IjkPlayer(
- mediaController: widget.controller,
- controllerWidgetBuilder: (ctl) {
- return OverlayControllerWidget(
- controller: ctl,
- onTapClose: widget.onTapClose,
- );
- },
- ),
- ),
- ),
- ),
- );
- }
- Offset _startOffset;
- bool _onOffsetNoticiation(OffsetNotication notification) {
- if (notification.type == OffsetType.start) {
- _startOffset = Offset(dx, dy);
- return true;
- }
- var offset = notification.offset;
- var size = MediaQuery.of(context).size;
- dx = _startOffset.dx + offset.dx / size.width * 2;
- dy = _startOffset.dy + offset.dy / size.height * 2;
- print("dx = $dx");
- print("dy = $dy");
- if (dx > 1) {
- dx = 1;
- } else if (dx < -1) {
- dx = -1;
- }
- if (dy > 1) {
- dy = 1;
- } else if (dy < -1) {
- dy = -1;
- }
- setState(() {});
- return true;
- }
- }
- class OverlayControllerWidget extends StatefulWidget {
- final IjkMediaController controller;
- final Function onTapClose;
- const OverlayControllerWidget({
- Key key,
- this.controller,
- this.onTapClose,
- }) : super(key: key);
- @override
- _OverlayControllerWidgetState createState() =>
- _OverlayControllerWidgetState();
- }
- class _OverlayControllerWidgetState extends State<OverlayControllerWidget> {
- bool showController = false;
- @override
- Widget build(BuildContext context) {
- Widget child;
- if (!showController) {
- child = Container();
- } else {
- child = Container(
- color: Colors.black.withOpacity(0.6),
- child: Align(
- alignment: Alignment.topRight,
- child: Container(
- child: IconButton(
- color: Colors.white,
- icon: Icon(Icons.close),
- onPressed: widget.onTapClose,
- ),
- ),
- ),
- );
- }
- return GestureDetector(
- child: child,
- behavior: HitTestBehavior.opaque,
- onTap: () => setState(() => showController = !showController),
- // onPanUpdate: (detail) {
- // var notification = OffsetNotication()
- // ..offset = detail.delta
- // ..type = OffsetType.update;
- // notification.dispatch(context);
- // },
- // onPanStart: (detail) {
- // var notication = OffsetNotication()..type = OffsetType.start;
- // notication.dispatch(context);
- // },
- onLongPressMoveUpdate: (detail) {
- var notification = OffsetNotication()
- ..offset = detail.offsetFromOrigin
- ..type = OffsetType.update;
- notification.dispatch(context);
- },
- onLongPressStart: (detail) {
- var n = OffsetNotication()..type = OffsetType.start;
- n.dispatch(context);
- },
- );
- }
- }
- class OffsetNotication extends Notification {
- Offset offset;
- OffsetType type;
- }
- enum OffsetType {
- start,
- update,
- }
|