| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:webview_flutter/webview_flutter.dart';
- import 'package:webview_flutter_android/webview_flutter_android.dart';
- import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
- import '../../constant/config.dart';
- import '../../main.dart';
- import '../../model/user_model.dart';
- import '../../widget/my_txt.dart';
- class UserShopPage extends ConsumerStatefulWidget {
- // final String url;
- const UserShopPage({
- super.key,
- // required this.url,
- });
- @override
- ConsumerState<UserShopPage> createState() => _UserShopPageState();
- }
- class _UserShopPageState extends ConsumerState<UserShopPage> {
- String url = "https://news.xhrbxxf.com/shop";
- late final WebViewController _controller;
- var isLoading = true;
- var progress = 0;
- @override
- void dispose() {
- // TODO: implement dispose
- super.dispose();
- }
- @override
- void initState() {
- super.initState();
- UserModel user = ref.read(globalUserProvider);
- // 平台特定的初始化
- late final PlatformWebViewControllerCreationParams params;
- if (WebViewPlatform.instance is WebKitWebViewPlatform) {
- params = WebKitWebViewControllerCreationParams(
- allowsInlineMediaPlayback: true,
- mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
- );
- } else {
- params = const PlatformWebViewControllerCreationParams();
- }
- final WebViewController controller =
- WebViewController.fromPlatformCreationParams(params);
- // 通用设置
- controller
- ..setJavaScriptMode(JavaScriptMode.unrestricted)
- ..setNavigationDelegate(
- NavigationDelegate(
- onProgress: (int progress) {
- if (!mounted) return;
- setState(() {
- this.progress = progress;
- if (progress == 100) {
- isLoading = false;
- }
- });
- },
- onPageStarted: (String url) {
- if (!mounted) return;
- setState(() => isLoading = true);
- },
- onPageFinished: (String url) {
- if (!mounted) return;
- setState(() => isLoading = false);
- },
- onWebResourceError: (WebResourceError error) {
- if (!mounted) return;
- debugPrint('''
- WebView error:
- code: ${error.errorCode}
- description: ${error.description}
- errorType: ${error.errorType}
- isForMainFrame: ${error.isForMainFrame}
- ''');
- },
- ),
- )
- ..loadRequest(Uri.parse(user.shopUrl ?? url));
- // Android 特定设置
- if (controller.platform is AndroidWebViewController) {
- AndroidWebViewController.enableDebugging(true);
- (controller.platform as AndroidWebViewController)
- .setMediaPlaybackRequiresUserGesture(false);
- }
- _controller = controller;
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Colors.white,
- resizeToAvoidBottomInset: false,
- appBar: AppBar(
- title: myTxt(
- text: '积分商城',
- fontSize: 18.sp,
- fontWeight: FontWeight.bold,
- ),
- centerTitle: true,
- leading: IconButton(
- icon: const Icon(Icons.arrow_back_ios),
- onPressed: () async {
- if (!mounted) return;
- if (await _controller.canGoBack()) {
- _controller.goBack();
- } else {
- if (Navigator.canPop(context)) {
- eventBus.fire('mainCall');
- Navigator.pop(context);
- }
- }
- },
- ),
- // actions: [
- // IconButton(
- // icon: const Icon(Icons.refresh),
- // onPressed: () => _controller.reload(),
- // ),
- // ],
- ),
- body: Stack(
- children: [
- SafeArea(top: false, child: WebViewWidget(controller: _controller)),
- if (isLoading)
- LinearProgressIndicator(
- value: progress / 100,
- backgroundColor: Colors.grey[200],
- valueColor: AlwaysStoppedAnimation<Color>(
- Theme.of(context).primaryColor,
- ),
- ),
- ],
- ),
- );
- }
- }
|