flutter_integrated_whb/lib/customWidget/BaiDuMap/BaiduMapWebView.dart

72 lines
2.1 KiB
Dart
Raw Normal View History

2025-08-19 11:06:16 +08:00
import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/custom_button.dart';
2025-08-19 11:06:16 +08:00
import 'package:webview_flutter/webview_flutter.dart';
import 'package:qhd_prevention/customWidget/toast_util.dart';
/// 可复用的地图 WebView 组件
/// BaiduMapWebView(
/// controller: _controller,
/// isLoading: _isLoading,
/// errorMessage: _errorMessage,
/// onRetry: _initLocation,
/// )
/// ```
class BaiduMapWebView extends StatelessWidget {
final WebViewController? controller;
final bool isLoading;
final String? errorMessage;
final VoidCallback onRetry;
2025-08-19 11:06:16 +08:00
const BaiduMapWebView({
super.key,
required this.controller,
required this.isLoading,
required this.errorMessage,
required this.onRetry,
});
2025-08-19 11:06:16 +08:00
@override
Widget build(BuildContext context) {
if (errorMessage != null) {
return Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(mainAxisSize: MainAxisSize.min, children: [
const Icon(Icons.error_outline, color: Colors.red, size: 50),
const SizedBox(height: 16),
Text(
errorMessage!,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 16),
),
const SizedBox(height: 24),
CustomButton(text: '重试', backgroundColor: Colors.blue, onPressed: onRetry)
]),
),
);
2025-08-19 11:06:16 +08:00
}
if (isLoading) {
return const Center(child: CircularProgressIndicator());
2025-08-19 11:06:16 +08:00
}
// 非加载且无错误时controller 应该存在;若为空则显示提示
if (controller == null) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.map_outlined, size: 48),
const SizedBox(height: 8),
const Text('地图未初始化'),
const SizedBox(height: 12),
ElevatedButton(onPressed: onRetry, child: const Text('重试初始化')),
],
),
);
2025-08-19 11:06:16 +08:00
}
return WebViewWidget(controller: controller!);
2025-08-19 11:06:16 +08:00
}
}