Commit db88c05c by tanghuan

openLink打开URL,不传递title参数时,读取所打开页面的title

1 parent 6a0055b5
......@@ -2,22 +2,37 @@ import 'package:appframe/config/routes.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:webview_flutter/webview_flutter.dart';
class LinkState extends Equatable {
final bool loaded;
final String url;
final String title;
const LinkState({this.loaded = false, this.url = ''});
const LinkState({
this.loaded = false,
this.url = '',
this.title = '',
});
LinkState copyWith({bool? loaded, String? url}) {
return LinkState(loaded: loaded ?? this.loaded, url: url ?? this.url);
LinkState copyWith({
bool? loaded,
String? url,
String? title,
}) {
return LinkState(
loaded: loaded ?? this.loaded,
url: url ?? this.url,
title: title ?? this.title,
);
}
@override
// TODO: implement props
List<Object?> get props => [loaded, url];
List<Object?> get props => [
loaded,
url,
title,
];
}
class LinkCubit extends Cubit<LinkState> {
......@@ -32,16 +47,24 @@ class LinkCubit extends Cubit<LinkState> {
NavigationDelegate(
onUrlChange: (UrlChange url) {},
onPageStarted: (String url) {},
onPageFinished: (String url) {
onPageFinished: (String url) async {
_controller.runJavaScript(
'document.querySelector("meta[name=viewport]").setAttribute("content", "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no")',
);
// 如果 state.title 为空,则读取网页的 title
if (state.title.isEmpty) {
final pageTitle = await _controller.runJavaScriptReturningResult('document.title') as String?;
// 移除可能存在的引号
final cleanTitle = pageTitle?.replaceAll('"', '');
emit(state.copyWith(title: cleanTitle ?? ''));
}
_finishLoading();
},
),
)
..loadRequest(Uri.parse(state.url));
..loadRequest(Uri.parse(state.url));
}
void _finishLoading() {
......
import 'package:appframe/bloc/link_cubit.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
......@@ -16,7 +14,7 @@ class LinkPage extends StatelessWidget {
final String? title = extraData?['title'];
return BlocProvider(
create: (context) => LinkCubit(LinkState(loaded: false, url: url!)),
create: (context) => LinkCubit(LinkState(loaded: false, url: url!, title: title ?? '')),
child: BlocConsumer<LinkCubit, LinkState>(
builder: (ctx, state) {
return PopScope(
......@@ -26,7 +24,7 @@ class LinkPage extends StatelessWidget {
},
child: Scaffold(
appBar: AppBar(
title: Text(title ?? '', style: TextStyle(color: Colors.white, fontSize: 18)),
title: Text(state.title, style: TextStyle(color: Colors.white, fontSize: 18)),
centerTitle: true,
backgroundColor: Color(0xFF7691FA),
iconTheme: IconThemeData(color: Colors.white),
......@@ -36,18 +34,9 @@ class LinkPage extends StatelessWidget {
height: MediaQuery.of(ctx).size.height - 120, // 减去100像素留空
child: WebViewWidget(
controller: ctx.read<LinkCubit>().controller,
gestureRecognizers: {
// 允许所有手势
Factory<VerticalDragGestureRecognizer>(
() => VerticalDragGestureRecognizer(),
),
Factory<HorizontalDragGestureRecognizer>(
() => HorizontalDragGestureRecognizer(),
),
},
),
)
: const Center(child: CircularProgressIndicator()),
: const Center(child: CircularProgressIndicator(color: Color(0xFF7691fa))),
),
);
},
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!