31 lines
1.0 KiB
Dart
31 lines
1.0 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
|
|
class MallWebHostState {
|
|
const MallWebHostState({this.url, this.visible = false});
|
|
final String? url;
|
|
final bool visible;
|
|
}
|
|
|
|
/// 进程常驻的商城 WebView 状态,切换原生 Tab 时不销毁商城页面。
|
|
class MallWebHostController {
|
|
MallWebHostController._();
|
|
static final instance = MallWebHostController._();
|
|
final state = ValueNotifier(const MallWebHostState());
|
|
Future<void> Function()? _backHandler;
|
|
|
|
void preload(String url) {
|
|
final old = state.value;
|
|
if (old.url == url) return;
|
|
state.value = MallWebHostState(url: url, visible: old.visible);
|
|
}
|
|
|
|
void open(String url) =>
|
|
state.value = MallWebHostState(url: url, visible: true);
|
|
void dismiss() => state.value = MallWebHostState(url: state.value.url);
|
|
void clear() => state.value = const MallWebHostState();
|
|
void attachBackHandler(Future<void> Function() handler) =>
|
|
_backHandler = handler;
|
|
void detachBackHandler() => _backHandler = null;
|
|
Future<void> handleBack() => _backHandler?.call() ?? Future.value();
|
|
}
|