import 'package:cookie_jar/cookie_jar.dart'; import 'package:flutter/material.dart'; import 'package:freezer/api/deezer.dart'; import 'package:freezer/settings.dart'; import 'package:webview_flutter/webview_flutter.dart'; class ExternalLinkRoute extends StatefulWidget { final String title; final String target; const ExternalLinkRoute( {required this.title, required this.target, super.key}); @override State createState() => _ExternalLinkRouteState(); } class _ExternalLinkRouteState extends State { late final WebViewController _controller; @override void initState() { _controller = WebViewController(); _initStateAsync(); super.initState(); } void _initStateAsync() async { final uri = _resolveTarget(widget.target); final headers = await _resolveHeaders(uri); _controller.addJavaScriptChannel('test', onMessageReceived: print); _controller.loadRequest(uri, headers: headers); } Uri _resolveTarget(String target) { print('target: $target'); if (target == 'story/mdy' || target == '/story/mdy') { // my deezer year redirect to iframe URL return Uri.parse( 'https://mydeezerstory.deezer.com/inapp?campaign=mdy&lang=${settings.deezerLanguage}'); } // resolve relative to www.deezer.com return Uri.https('www.deezer.com', '/us').resolve(target); } Future> _resolveHeaders(Uri uri) async { List cookies = await deezerAPI.cookieJar.loadForRequest(uri); print(cookies); return {'Cookie': cookies.join(';')}; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(widget.title)), body: WebViewWidget( controller: _controller, ), ); } }