108 lines
3.0 KiB
Dart
108 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
import 'package:tugas_akhir_supabase/core/theme/app_colors.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
class NewsWebView extends StatefulWidget {
|
|
final String url;
|
|
final String title;
|
|
|
|
const NewsWebView({super.key, required this.url, required this.title});
|
|
|
|
@override
|
|
_NewsWebViewState createState() => _NewsWebViewState();
|
|
}
|
|
|
|
class _NewsWebViewState extends State<NewsWebView> {
|
|
late WebViewController _controller;
|
|
bool _isLoading = true;
|
|
double _loadingProgress = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initWebView();
|
|
}
|
|
|
|
void _initWebView() {
|
|
_controller =
|
|
WebViewController()
|
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
|
..setNavigationDelegate(
|
|
NavigationDelegate(
|
|
onPageStarted: (String url) {
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
},
|
|
onProgress: (int progress) {
|
|
setState(() {
|
|
_loadingProgress = progress / 100;
|
|
});
|
|
},
|
|
onPageFinished: (String url) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
},
|
|
onWebResourceError: (WebResourceError error) {
|
|
debugPrint('WebView error: ${error.description}');
|
|
},
|
|
),
|
|
)
|
|
..loadRequest(Uri.parse(widget.url));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
backgroundColor: AppColors.primary,
|
|
foregroundColor: Colors.white,
|
|
title: Text(
|
|
widget.title,
|
|
style: TextStyle(fontSize: 16),
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
actions: [
|
|
IconButton(
|
|
icon: Icon(Icons.open_in_browser),
|
|
onPressed: () async {
|
|
final Uri url = Uri.parse(widget.url);
|
|
try {
|
|
await launchUrl(url, mode: LaunchMode.externalApplication);
|
|
} catch (e) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Tidak dapat membuka browser eksternal'),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
tooltip: 'Buka di browser',
|
|
),
|
|
IconButton(
|
|
icon: Icon(Icons.refresh),
|
|
onPressed: () {
|
|
_controller.reload();
|
|
},
|
|
tooltip: 'Muat ulang',
|
|
),
|
|
],
|
|
),
|
|
body: Stack(
|
|
children: [
|
|
WebViewWidget(controller: _controller),
|
|
if (_isLoading)
|
|
LinearProgressIndicator(
|
|
value: _loadingProgress > 0 ? _loadingProgress : null,
|
|
backgroundColor: Colors.white70,
|
|
valueColor: AlwaysStoppedAnimation<Color>(AppColors.primary),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|