fix:第一轮跑
This commit is contained in:
1 parent
e834ec7586
commit
33235f450b
56 files changed
+4357
-210
No files matched your search
@@ -0,0 +1,122 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io' show File;
|
||||
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:video_player/video_player.dart';
|
||||
|
||||
import '../data/ads_api_client.dart';
|
||||
|
||||
/// 统一渲染广告图片/GIF/视频;开屏可优先使用已缓存的本地素材。
|
||||
class AdMedia extends StatefulWidget {
|
||||
const AdMedia({
|
||||
super.key,
|
||||
required this.ad,
|
||||
this.assetPath,
|
||||
this.fit = BoxFit.cover,
|
||||
});
|
||||
final AdCreative ad;
|
||||
final String? assetPath;
|
||||
final BoxFit fit;
|
||||
@override
|
||||
State<AdMedia> createState() => _AdMediaState();
|
||||
}
|
||||
|
||||
class _AdMediaState extends State<AdMedia> {
|
||||
VideoPlayerController? player;
|
||||
bool ready = false, failed = false;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant AdMedia old) {
|
||||
super.didUpdateWidget(old);
|
||||
if (old.ad.adId != widget.ad.adId || old.assetPath != widget.assetPath) {
|
||||
unawaited(player?.dispose() ?? Future.value());
|
||||
player = null;
|
||||
ready = false;
|
||||
failed = false;
|
||||
_load();
|
||||
}
|
||||
}
|
||||
|
||||
void _load() {
|
||||
if (widget.ad.mediaType != AdMediaType.video) return;
|
||||
final uri = widget.ad.mediaUri;
|
||||
if (uri == null) return;
|
||||
final path = widget.assetPath;
|
||||
final controller = !kIsWeb && path != null && path.isNotEmpty
|
||||
? VideoPlayerController.file(File(path))
|
||||
: VideoPlayerController.networkUrl(uri);
|
||||
player = controller;
|
||||
unawaited(
|
||||
controller
|
||||
.initialize()
|
||||
.then((_) async {
|
||||
await controller.setLooping(true);
|
||||
await controller.setVolume(0);
|
||||
await controller.play();
|
||||
if (mounted) setState(() => ready = true);
|
||||
})
|
||||
.catchError((_) {
|
||||
if (mounted) setState(() => failed = true);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
player?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (widget.ad.mediaType != AdMediaType.video) {
|
||||
final path = widget.assetPath;
|
||||
if (!kIsWeb && path != null && path.isNotEmpty) {
|
||||
return Image.file(
|
||||
File(path),
|
||||
fit: widget.fit,
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
errorBuilder: (_, _, _) => _error(),
|
||||
);
|
||||
}
|
||||
return Image.network(
|
||||
widget.ad.imageUrl,
|
||||
fit: widget.fit,
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
errorBuilder: (_, _, _) => _error(),
|
||||
);
|
||||
}
|
||||
final c = player;
|
||||
if (failed) return _error();
|
||||
if (c == null || !ready) {
|
||||
return const ColoredBox(
|
||||
color: Color(0xFF161616),
|
||||
child: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
return FittedBox(
|
||||
fit: widget.fit,
|
||||
clipBehavior: Clip.hardEdge,
|
||||
child: SizedBox(
|
||||
width: c.value.size.width,
|
||||
height: c.value.size.height,
|
||||
child: VideoPlayer(c),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _error() => const ColoredBox(
|
||||
color: Color(0xFF161616),
|
||||
child: Center(
|
||||
child: Icon(Icons.broken_image_outlined, color: Colors.white54),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../../../common/router/app_router.dart';
|
||||
import '../data/ads_api_client.dart';
|
||||
import 'ad_media.dart';
|
||||
|
||||
class HomeAdBanner extends ConsumerStatefulWidget {
|
||||
const HomeAdBanner({super.key});
|
||||
@override
|
||||
ConsumerState<HomeAdBanner> createState() => _HomeAdBannerState();
|
||||
}
|
||||
|
||||
class _HomeAdBannerState extends ConsumerState<HomeAdBanner> {
|
||||
final pages = PageController();
|
||||
final impressed = <String>{};
|
||||
Timer? timer;
|
||||
int index = 0;
|
||||
int _length = 0;
|
||||
@override
|
||||
void dispose() {
|
||||
timer?.cancel();
|
||||
pages.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _sync(List<AdCreative> ads) {
|
||||
if (_length == ads.length) return;
|
||||
_length = ads.length;
|
||||
timer?.cancel();
|
||||
if (ads.length > 1) {
|
||||
timer = Timer.periodic(const Duration(seconds: 4), (_) {
|
||||
if (pages.hasClients) {
|
||||
pages.nextPage(
|
||||
duration: const Duration(milliseconds: 320),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _impression(AdCreative ad, String? id) {
|
||||
if (!impressed.add(ad.adId)) return;
|
||||
unawaited(
|
||||
ref
|
||||
.read(adsApiClientProvider)
|
||||
.report(ad, 'ad_impression', trackingId: id),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _open(AdCreative ad, String? tracking) async {
|
||||
final client = ref.read(adsApiClientProvider);
|
||||
final id = client.newClickId();
|
||||
final uri = ad.clickUri(id);
|
||||
if (uri == null) return;
|
||||
unawaited(client.report(ad, 'ad_click', clickId: id, trackingId: tracking));
|
||||
if (!mounted) return;
|
||||
if (ad.openMode == AdOpenMode.inApp) {
|
||||
context.push(
|
||||
'${AppRoutes.web}?url=${Uri.encodeComponent(uri.toString())}',
|
||||
);
|
||||
} else {
|
||||
await launchUrl(uri, mode: LaunchMode.externalApplication);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final async = ref.watch(featuredAdsProvider(homeBannerAdPlacement));
|
||||
return async.maybeWhen(
|
||||
data: (value) {
|
||||
if (!value.hasAds) return const SizedBox.shrink();
|
||||
final ads = value.ads;
|
||||
_sync(ads);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
_impression(ads[index.clamp(0, ads.length - 1)], value.trackingId);
|
||||
}
|
||||
});
|
||||
return Column(
|
||||
children: [
|
||||
AspectRatio(
|
||||
aspectRatio: 750 / 200,
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: PageView.builder(
|
||||
controller: pages,
|
||||
onPageChanged: (page) {
|
||||
setState(() => index = page % ads.length);
|
||||
_impression(ads[index], value.trackingId);
|
||||
},
|
||||
itemBuilder: (_, page) {
|
||||
final ad = ads[page % ads.length];
|
||||
return GestureDetector(
|
||||
onTap: () => _open(ad, value.trackingId),
|
||||
child: AdMedia(ad: ad),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
if (ads.length > 1)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: List.generate(
|
||||
ads.length,
|
||||
(i) => Container(
|
||||
width: i == index ? 14 : 6,
|
||||
height: 6,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 3),
|
||||
decoration: BoxDecoration(
|
||||
color: i == index
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: Theme.of(context).dividerColor,
|
||||
borderRadius: BorderRadius.circular(3),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
orElse: () => const SizedBox.shrink(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../application/splash_ad_controller.dart';
|
||||
import 'ad_media.dart';
|
||||
|
||||
enum SplashAdResult { clicked, skipped, completed }
|
||||
|
||||
class SplashAdDialog extends StatefulWidget {
|
||||
const SplashAdDialog({
|
||||
super.key,
|
||||
required this.entry,
|
||||
required this.onClick,
|
||||
required this.onShown,
|
||||
});
|
||||
final SplashAdEntry entry;
|
||||
final Future<void> Function() onClick;
|
||||
final VoidCallback onShown;
|
||||
@override
|
||||
State<SplashAdDialog> createState() => _SplashAdDialogState();
|
||||
}
|
||||
|
||||
class _SplashAdDialogState extends State<SplashAdDialog> {
|
||||
Timer? timer;
|
||||
late int remain;
|
||||
bool opening = false;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
remain = widget.entry.ad.countdownSeconds.clamp(1, 5);
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
widget.onShown();
|
||||
});
|
||||
timer = Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (remain <= 1) {
|
||||
Navigator.of(context).pop(SplashAdResult.completed);
|
||||
} else if (mounted) {
|
||||
setState(() => remain--);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> open() async {
|
||||
if (opening) return;
|
||||
opening = true;
|
||||
timer?.cancel();
|
||||
await widget.onClick();
|
||||
if (mounted) Navigator.of(context).pop(SplashAdResult.clicked);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Material(
|
||||
color: Colors.black,
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: GestureDetector(
|
||||
onTap: open,
|
||||
child: AdMedia(
|
||||
ad: widget.entry.ad,
|
||||
assetPath: widget.entry.assetPath,
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: MediaQuery.paddingOf(context).top + 16,
|
||||
right: 18,
|
||||
child: FilledButton(
|
||||
onPressed: () => Navigator.of(context).pop(SplashAdResult.skipped),
|
||||
child: Text('跳过 ${remain}s'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user