Commit 8db55406 by tanghuan

dev

1 parent 3ce6d8ec
import 'dart:io';
import 'package:appframe/l10n/gen/app_localizations.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
......@@ -16,6 +17,8 @@ class App extends StatelessWidget {
theme: const CupertinoThemeData(primaryColor: CupertinoColors.systemBlue),
)
: MaterialApp.router(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
routerConfig: router,
title: '班小二',
theme: ThemeData(primarySwatch: Colors.blue),
......
......@@ -16,10 +16,13 @@ import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:uuid/uuid.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';
class WebState extends Equatable {
final bool loaded;
final String title;
final bool beBack;
final String? ip;
final String? sessionCode;
......@@ -36,9 +39,22 @@ class WebState extends Equatable {
final int playState;
final String playId;
/// getOrientationCmd
final bool orientationCmdFlag;
final String orientationCmdMessage;
/// getWindowInfoCmd
final bool windowInfoCmdFlag;
final String windowInfoCmdMessage;
/// chooseImageCmd
final bool chooseImageCmdFlag;
final String chooseImageCmdMessage;
const WebState({
this.loaded = false,
this.title = '界面加载中...',
this.beBack = false,
this.ip,
this.sessionCode,
this.userCode,
......@@ -51,11 +67,18 @@ class WebState extends Equatable {
this.playerIsInit = false,
this.playState = 0,
this.playId = '',
this.orientationCmdFlag = false,
this.orientationCmdMessage = '',
this.windowInfoCmdFlag = false,
this.windowInfoCmdMessage = '',
this.chooseImageCmdFlag = false,
this.chooseImageCmdMessage = '',
});
WebState copyWith({
bool? loaded,
String? title,
bool? beBack,
String? ip,
String? sessionCode,
String? userCode,
......@@ -68,10 +91,17 @@ class WebState extends Equatable {
bool? playerIsInit,
int? playState,
String? playId,
bool? orientationCmdFlag,
String? orientationCmdMessage,
bool? windowInfoCmdFlag,
String? windowInfoCmdMessage,
bool? chooseImageCmdFlag,
String? chooseImageCmdMessage,
}) {
return WebState(
loaded: loaded ?? this.loaded,
title: title ?? this.title,
beBack: beBack ?? this.beBack,
ip: ip ?? this.ip,
sessionCode: sessionCode ?? this.sessionCode,
userCode: userCode ?? this.userCode,
......@@ -84,6 +114,12 @@ class WebState extends Equatable {
playerIsInit: playerIsInit ?? this.playerIsInit,
playState: playState ?? this.playState,
playId: playId ?? this.playId,
orientationCmdFlag: orientationCmdFlag ?? this.orientationCmdFlag,
orientationCmdMessage: orientationCmdMessage ?? this.orientationCmdMessage,
windowInfoCmdFlag: windowInfoCmdFlag ?? this.windowInfoCmdFlag,
windowInfoCmdMessage: windowInfoCmdMessage ?? this.windowInfoCmdMessage,
chooseImageCmdFlag: chooseImageCmdFlag ?? this.chooseImageCmdFlag,
chooseImageCmdMessage: chooseImageCmdMessage ?? this.chooseImageCmdMessage,
);
}
......@@ -91,6 +127,7 @@ class WebState extends Equatable {
List<Object?> get props => [
loaded,
title,
beBack,
ip,
sessionCode,
userCode,
......@@ -103,6 +140,12 @@ class WebState extends Equatable {
playerIsInit,
playState,
playId,
orientationCmdFlag,
orientationCmdMessage,
windowInfoCmdFlag,
windowInfoCmdMessage,
chooseImageCmdFlag,
chooseImageCmdMessage,
];
}
......@@ -130,16 +173,21 @@ class WebCubit extends Cubit<WebState> {
..setNavigationDelegate(
NavigationDelegate(
onUrlChange: (UrlChange url) {},
onPageStarted: (String url) {
onPageStarted: (String url) async {
// 进行新页面加载时,关闭录音器和播放器,(如果有打开过)
// closeLocalRecorder();
// closeLocalPlayer();
},
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")',
);
finishLoading();
// 页面加载完成时,清空录音和音频(如果有打开过)
await clearRecording() ;
await clearAudio();
print('onPageFinished--------------------------------->');
print(url);
},
......@@ -173,10 +221,7 @@ class WebCubit extends Cubit<WebState> {
_onMessageReceived(JavaScriptMessage message) async {
try {
final Map<String, dynamic> data = json.decode(message.message);
H5Message h5Message = H5Message.fromJson(data);
_dispatcher.dispatch(h5Message, (response) {
_dispatcher.dispatch(message.message, (response) {
_sendResponse(response);
}, webCubit: this);
} catch (e) {
......@@ -193,12 +238,7 @@ class WebCubit extends Cubit<WebState> {
}
void finishLoading() {
emit(state.copyWith(loaded: true, title: '班小二测试H5'));
}
// 测试
void setTitle(String title) {
emit(state.copyWith(title: title));
emit(state.copyWith(loaded: true, title: '班小二测试', beBack: true));
}
// 测试
......@@ -243,9 +283,187 @@ class WebCubit extends Cubit<WebState> {
}
Future<void> handleBack() async {
if (await _controller.canGoBack()) {
_controller.goBack();
// navigateBack指令
var resp = {'unique': '', 'cmd': 'navigateBack', 'data': '', 'errMsg': ''};
_sendResponse(resp);
}
bool setTitle(String title, bool beBack) {
emit(state.copyWith(title: title, beBack: beBack));
return true;
}
Future<void> refresh() async {
clearRecording();
clearAudio();
_controller.reload();
}
void setChooseImageCmdFlag(bool chooseImageCmdFlag, String chooseImageCmdMessage) {
emit(state.copyWith(chooseImageCmdFlag: chooseImageCmdFlag, chooseImageCmdMessage: chooseImageCmdMessage));
}
void chooseImage(BuildContext context) async {
final Map<String, dynamic> data = json.decode(state.chooseImageCmdMessage);
H5Message h5Message = H5Message.fromJson(data);
setChooseImageCmdFlag(false, '');
final params = h5Message.params;
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
var sourceType = params['sourceType'] as String;
if (sourceType != 'album' && sourceType != 'camera') {
sourceType = 'album';
}
// 暂时忽略 sizeType 参数
int count = 9;
if (params.containsKey('count')) {
count = params['count'] as int;
if (count < 1 || count > 9) {
count = 9;
}
}
// 相册
if (sourceType == 'album') {
_chooseFromAlbum(context, count, h5Message.unique, h5Message.cmd);
}
// 拍照
else {
_chooseFromCamera(context, h5Message.unique, h5Message.cmd);
}
}
void _chooseFromAlbum(BuildContext context, int count, String unique, String cmd) async {
final List<AssetEntity>? result = await AssetPicker.pickAssets(
context,
pickerConfig: AssetPickerConfig(maxAssets: count, requestType: RequestType.image),
);
if (result == null || result.isEmpty) {
var resp = {'unique': unique, 'cmd': cmd, 'data': null, 'errMsg': '未选择图片'};
_sendResponse(resp);
return;
}
// 获取临时目录
final Directory tempDir = await getTemporaryDirectory();
final List<Map<String, dynamic>> resultList = [];
for (var asset in result) {
resultList.add(await _handleSingleImage(asset, tempDir));
}
var resp = {'unique': unique, 'cmd': cmd, 'data': resultList, 'errMsg': ''};
_sendResponse(resp);
}
void _chooseFromCamera(BuildContext context, String unique, String cmd) async {
AssetEntity? asset = await CameraPicker.pickFromCamera(context, pickerConfig: const CameraPickerConfig());
if (asset == null) {
var resp = {'unique': unique, 'cmd': cmd, 'data': null, 'errMsg': '未选择图片'};
_sendResponse(resp);
return;
}
final Directory tempDir = await getTemporaryDirectory();
final Map<String, dynamic> result = await _handleSingleImage(asset, tempDir);
var resp = {
'unique': unique,
'cmd': cmd,
'data': [result],
'errMsg': '',
};
_sendResponse(resp);
}
Future<Map<String, dynamic>> _handleSingleImage(AssetEntity asset, Directory tempDir) async {
final file = await asset.file;
// 生成缩略图
final data = await asset.thumbnailData;
final thumbnailFile = await File(
'${tempDir.path}/${DateTime.now().millisecondsSinceEpoch}.png',
).writeAsBytes(data!);
return {
"tempFilePath": file!.path,
"size": file.lengthSync(),
"width": asset.width,
"height": asset.height,
"thumbTempFilePath": '/temp${thumbnailFile.path}',
"fileType": file.path.split('/').last.split('.').last,
};
}
void setOrientationCmdFlag(bool orientationCmdFlag, String orientationCmdMessage) {
emit(state.copyWith(orientationCmdFlag: orientationCmdFlag, orientationCmdMessage: orientationCmdMessage));
}
void getOrientation(BuildContext context) async {
final Map<String, dynamic> data = json.decode(state.orientationCmdMessage);
H5Message h5Message = H5Message.fromJson(data);
setOrientationCmdFlag(false, '');
final orientation = MediaQuery.of(context).orientation;
var resp = {
'unique': h5Message.unique,
'cmd': h5Message.cmd,
'data': {'orientation': orientation == Orientation.portrait ? "portrait" : "landscape"},
'errMsg': '',
};
_sendResponse(resp);
}
void setWindowInfoCmdFlag(bool windowInfoCmdFlag, String windowInfoCmdMessage) {
emit(state.copyWith(windowInfoCmdFlag: windowInfoCmdFlag, windowInfoCmdMessage: windowInfoCmdMessage));
}
void getWindowInfo(BuildContext context) async {
final Map<String, dynamic> data = json.decode(state.windowInfoCmdMessage);
H5Message h5Message = H5Message.fromJson(data);
setWindowInfoCmdFlag(false, '');
final mediaQuery = MediaQuery.of(context);
final viewPadding = mediaQuery.viewPadding;
final size = mediaQuery.size;
final safeArea = mediaQuery.padding;
final devicePixelRatio = mediaQuery.devicePixelRatio;
// 计算安全区域坐标
final safeAreaLeft = safeArea.left;
final safeAreaRight = size.width - safeArea.right;
final safeAreaTop = safeArea.top;
final safeAreaBottom = size.height - safeArea.bottom;
final safeAreaWidth = size.width - safeArea.horizontal;
final safeAreaHeight = size.height - safeArea.vertical;
final windowInfo = {
'pixelRatio': devicePixelRatio,
'screenWidth': size.width * devicePixelRatio,
'screenHeight': size.height * devicePixelRatio,
'windowWidth': size.width,
'windowHeight': size.height,
'statusBarHeight': viewPadding.top,
'screenTop': 0, // Flutter中通常不使用此值,设为0
'safeArea': {
'left': safeAreaLeft,
'right': safeAreaRight,
'top': safeAreaTop,
'bottom': safeAreaBottom,
'width': safeAreaWidth,
'height': safeAreaHeight,
},
};
var resp = {'unique': h5Message.unique, 'cmd': h5Message.cmd, 'data': windowInfo, 'errMsg': ''};
_sendResponse(resp);
}
/// 录音初始化
......@@ -340,15 +558,22 @@ class WebCubit extends Cubit<WebState> {
if (url == null || url.isEmpty) {
throw Exception("录音失败");
}
// 获取音频文件时长
emit(state.copyWith(recorderIsInit: false, recordState: 0, recordPath: ''));
return {'url': url};
return {'tempFilePath': url};
}
/// 清空录音
Future<bool> clearRecording() async {
// await _recorder!.stopRecorder();
await _recorder!.closeRecorder();
try {
await _recorder?.closeRecorder();
} catch (e) {
// ignore: empty_catches
print(e);
}
emit(state.copyWith(recorderIsInit: false, recordState: 0, recordPath: ''));
return true;
}
......@@ -445,7 +670,12 @@ class WebCubit extends Cubit<WebState> {
/// 清空播放
Future<bool> clearAudio() async {
try {
await _player?.closePlayer();
} catch (e) {
// ignore: empty_catches
print(e);
}
emit(state.copyWith(playerIsInit: false, playState: 0, playId: ''));
return true;
}
......@@ -457,8 +687,20 @@ class WebCubit extends Cubit<WebState> {
// closeLocalRecorder();
// closeLocalPlayer();
try {
await _recorder?.closeRecorder();
} catch (e) {
// ignore: empty_catches
print(e);
}
try {
await _player?.closePlayer();
} catch (e) {
// ignore: empty_catches
print(e);
}
return super.close();
}
}
......@@ -62,6 +62,7 @@ class WechatAuthCubit extends Cubit<WechatAuthState> {
// _textEditingController = TextEditingController()..text = '127.0.0.1';
_textEditingController = TextEditingController()..text = 'appdev-th.banxiaoer.net';
// _textEditingController = TextEditingController()..text = 'appdev-xj.banxiaoer.net';
// _textEditingController = TextEditingController()..text = '192.168.1.136';
_wechatAuthRepository = getIt<WechatAuthRepository>();
......
......@@ -17,6 +17,7 @@ import 'package:appframe/data/repositories/message/orientation_handler.dart';
import 'package:appframe/data/repositories/message/save_file_to_disk_handler.dart';
import 'package:appframe/data/repositories/message/save_to_album_handler.dart';
import 'package:appframe/data/repositories/message/scan_code_handler.dart';
import 'package:appframe/data/repositories/message/set_title_handler.dart';
import 'package:appframe/data/repositories/message/storage_handler.dart';
import 'package:appframe/data/repositories/message/upload_file.dart';
import 'package:appframe/data/repositories/message/vibrate_short_handler.dart';
......@@ -107,7 +108,7 @@ Future<void> setupLocator() async {
/// 获取视频信息
getIt.registerLazySingleton<MessageHandler>(() => VideoInfoHandler(), instanceName: 'getVideoInfo');
/// 保存文件
/// 保存文件到客户端文件系统
getIt.registerLazySingleton<MessageHandler>(() => SaveFileToDisKHandler(), instanceName: 'saveFileToDisk');
/// 保存文件/视频到相册
......@@ -120,7 +121,7 @@ Future<void> setupLocator() async {
/// 视频压缩
getIt.registerLazySingleton<MessageHandler>(() => CompressVideoHandler(), instanceName: 'compressVideo');
/// 保存文件到客户端文件系统
/// 打开文档
getIt.registerLazySingleton<MessageHandler>(() => OpenDocumentHandler(), instanceName: 'openDocument');
/// 录音
......@@ -150,6 +151,9 @@ Future<void> setupLocator() async {
/// 下载文件
getIt.registerLazySingleton<MessageHandler>(() => DownloadFileHandler(), instanceName: 'downloadFile');
/// 设置标题和返回按钮
getIt.registerLazySingleton<MessageHandler>(() => SetTitleHandler(), instanceName: 'setTitle');
/// service
getIt.registerLazySingleton<ApiService>(() => ApiService(baseUrl: 'https://dev.banxiaoer.net'));
......
......@@ -3,5 +3,5 @@ import 'package:fluwx/fluwx.dart';
final Fluwx fluwx = Fluwx();
Future<void> registerWechatApi() async {
await fluwx.registerApi(appId: "wx8c32ea248f0c7765", universalLink: "https://univerallink.banxe.cn/link/");
await fluwx.registerApi(appId: "wx8c32ea248f0c7765", universalLink: "https://dev.banxiaoer.net/path/to/wechat/");
}
......@@ -3,6 +3,6 @@ import 'package:appframe/services/dispatcher.dart';
class AppInfoHandler extends MessageHandler {
@override
Future<Map<String, dynamic>> handleMessage(params) async {
return {"version": "0.1", "build": "light"};
return {"version": "0.1", "theme": "light"};
}
}
import 'dart:io';
import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_size_getter/file_input.dart';
import 'package:image_size_getter/image_size_getter.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
class ChooseImageHandler extends MessageHandler {
late WebCubit? _webCubit;
late String? _message;
@override
void setCubit(WebCubit cubit) {
this._webCubit = cubit;
}
void _unfollowCubit() {
this._webCubit = null;
}
@override
Future<dynamic> handleMessage(dynamic params) async {
void setMessage(String message) {
this._message = message;
}
@override
Future<dynamic> handleMessage(params) async {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
......@@ -35,151 +45,192 @@ class ChooseImageHandler extends MessageHandler {
}
}
// 从相册选择
if (sourceType == 'album') {
if (count == 1) {
return await _selectSingle();
} else {
return await _selectMulti(count);
}
}
// 拍照
else {
return await _cameraSingle();
}
}
///
/// 选择单张图片
///
/// 将选择的图片放到应用的临时目录中,并在路径前面添加“/temp”,返回给调用方后,调用方可通过http方式访问图片
///
Future<List<Map<String, dynamic>>?> _selectSingle() async {
final ImagePicker picker = ImagePicker();
final XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
// 用户取消选择,返回空数组
if (pickedFile == null) {
return [];
}
// 获取临时目录
final Directory tempDir = await getTemporaryDirectory();
return [await _handleOne(pickedFile, tempDir)];
}
///
/// 选择多张图片
///
/// 将选择的图片放到应用的临时目录中,并在每个文件路径前面添加“/temp”,返回给调用方后,调用方可通过http方式访问图片
///
Future<List<Map<String, dynamic>>?> _selectMulti(int limit) async {
final ImagePicker picker = ImagePicker();
final List<XFile> pickedFileList = await picker.pickMultiImage(limit: limit);
// 用户取消选择,返回空数组
if (pickedFileList.isEmpty) {
return [];
}
// 限制最多limit张
if (pickedFileList.length > limit) {
pickedFileList.removeRange(limit, pickedFileList.length);
}
// 获取临时目录
final Directory tempDir = await getTemporaryDirectory();
final List<Map<String, dynamic>> result = [];
for (final XFile? file in pickedFileList) {
if (file != null) {
result.add(await _handleOne(file, tempDir));
}
}
return result;
}
///
/// 拍照
///
Future<List<Map<String, dynamic>>?> _cameraSingle() async {
final ImagePicker picker = ImagePicker();
final XFile? pickedFile = await picker.pickImage(source: ImageSource.camera);
// 用户取消选择,返回空数组
if (pickedFile == null) {
return [];
}
// 获取临时目录
final Directory tempDir = await getTemporaryDirectory();
return [await _handleOne(pickedFile, tempDir)];
}
Future<Map<String, dynamic>> _handleOne3(XFile pickedFile, Directory tempDir) async {
// 生成唯一文件名
final String fileName = path.basename(pickedFile.path);
final String uniqueFileName = '${DateTime.now().millisecondsSinceEpoch}_$fileName';
// 创建目标文件路径
final String tempFilePath = path.join(tempDir.path, uniqueFileName);
// 复制文件到临时目录
var sourceFile = File(pickedFile.path);
final File copiedFile = await sourceFile.copy(tempFilePath);
// 通过image_size_getter获取图片尺寸
final sizeResult = ImageSizeGetter.getSizeResult(FileInput(sourceFile));
final thumbnailPath = await _genThumbnail(sourceFile, tempDir);
// 返回一个元素的数组
return {
"tempFilePath": "/temp${copiedFile.path}",
"size": copiedFile.lengthSync(),
"width": sizeResult.size.width,
"height": sizeResult.size.height,
"thumbTempFilePath": '/temp$thumbnailPath',
"fileType": copiedFile.path.split('/').last.split('.').last,
};
}
Future<Map<String, dynamic>> _handleOne(XFile pickedFile, Directory tempDir) async {
var sourceFile = File(pickedFile.path);
// 通过image_size_getter获取图片尺寸
final sizeResult = ImageSizeGetter.getSizeResult(FileInput(sourceFile));
final thumbnailPath = await _genThumbnail(sourceFile, tempDir);
// 返回一个元素的数组
return {
"tempFilePath": "/temp${sourceFile.path}",
"size": sourceFile.lengthSync(),
"width": sizeResult.size.width,
"height": sizeResult.size.height,
"thumbTempFilePath": '/temp$thumbnailPath',
"fileType": sourceFile.path.split('/').last.split('.').last,
};
}
Future<String?> _genThumbnail(File imageFile, Directory tempDir) async {
try {
// 缩略图路径
final tempPath = tempDir.path;
final targetPath = '$tempPath/thumbnail_${DateTime.now().millisecondsSinceEpoch}.jpg';
// 压缩生成缩略图文件
final compressedFile = await FlutterImageCompress.compressAndGetFile(imageFile.absolute.path, targetPath);
return compressedFile!.path;
} catch (e) {
print('生成缩略图出错: $e');
return null;
}
_webCubit!.setChooseImageCmdFlag(true, _message!);
}
}
// import 'dart:io';
//
// import 'package:appframe/services/dispatcher.dart';
// import 'package:flutter_image_compress/flutter_image_compress.dart';
// import 'package:image_picker/image_picker.dart';
// import 'package:image_size_getter/file_input.dart';
// import 'package:image_size_getter/image_size_getter.dart';
// import 'package:path/path.dart' as path;
// import 'package:path_provider/path_provider.dart';
//
// class ChooseImageHandler extends MessageHandler {
// @override
// Future<dynamic> handleMessage(dynamic params) async {
// if (params is! Map<String, dynamic>) {
// throw Exception('参数错误');
// }
// var sourceType = params['sourceType'] as String;
// if (sourceType != 'album' && sourceType != 'camera') {
// throw Exception('参数错误');
// }
// // 暂时忽略对此参数的处理
// List<dynamic>? sizeType;
// if (params.containsKey('sizeType')) {
// sizeType = params['sizeType'] as List<dynamic>;
// if (sizeType.isEmpty || sizeType.length > 2) {
// throw Exception('参数错误');
// }
// }
//
// int count = 9;
// if (params.containsKey('count')) {
// count = params['count'] as int;
// if (count < 1 || count > 9) {
// throw Exception('参数错误');
// }
// }
//
// // 从相册选择
// if (sourceType == 'album') {
// if (count == 1) {
// return await _selectSingle();
// } else {
// return await _selectMulti(count);
// }
// }
// // 拍照
// else {
// return await _cameraSingle();
// }
// }
//
// ///
// /// 选择单张图片
// ///
// /// 将选择的图片放到应用的临时目录中,并在路径前面添加“/temp”,返回给调用方后,调用方可通过http方式访问图片
// ///
// Future<List<Map<String, dynamic>>?> _selectSingle() async {
// final ImagePicker picker = ImagePicker();
//
// final XFile? pickedFile = await picker.pickImage(source: ImageSource.gallery);
//
// // 用户取消选择,返回空数组
// if (pickedFile == null) {
// return [];
// }
//
// // 获取临时目录
// final Directory tempDir = await getTemporaryDirectory();
//
// return [await _handleOne(pickedFile, tempDir)];
// }
//
// ///
// /// 选择多张图片
// ///
// /// 将选择的图片放到应用的临时目录中,并在每个文件路径前面添加“/temp”,返回给调用方后,调用方可通过http方式访问图片
// ///
// Future<List<Map<String, dynamic>>?> _selectMulti(int limit) async {
// final ImagePicker picker = ImagePicker();
//
// final List<XFile> pickedFileList = await picker.pickMultiImage(limit: limit);
//
// // 用户取消选择,返回空数组
// if (pickedFileList.isEmpty) {
// return [];
// }
//
// // 限制最多limit张
// if (pickedFileList.length > limit) {
// pickedFileList.removeRange(limit, pickedFileList.length);
// }
//
// // 获取临时目录
// final Directory tempDir = await getTemporaryDirectory();
//
// final List<Map<String, dynamic>> result = [];
// for (final XFile? file in pickedFileList) {
// if (file != null) {
// result.add(await _handleOne(file, tempDir));
// }
// }
//
// return result;
// }
//
// ///
// /// 拍照
// ///
// Future<List<Map<String, dynamic>>?> _cameraSingle() async {
// final ImagePicker picker = ImagePicker();
//
// final XFile? pickedFile = await picker.pickImage(source: ImageSource.camera);
//
// // 用户取消选择,返回空数组
// if (pickedFile == null) {
// return [];
// }
//
// // 获取临时目录
// final Directory tempDir = await getTemporaryDirectory();
//
// return [await _handleOne(pickedFile, tempDir)];
// }
//
// Future<Map<String, dynamic>> _handleOne3(XFile pickedFile, Directory tempDir) async {
// // 生成唯一文件名
// final String fileName = path.basename(pickedFile.path);
// final String uniqueFileName = '${DateTime.now().millisecondsSinceEpoch}_$fileName';
//
// // 创建目标文件路径
// final String tempFilePath = path.join(tempDir.path, uniqueFileName);
//
// // 复制文件到临时目录
// var sourceFile = File(pickedFile.path);
// final File copiedFile = await sourceFile.copy(tempFilePath);
//
// // 通过image_size_getter获取图片尺寸
// final sizeResult = ImageSizeGetter.getSizeResult(FileInput(sourceFile));
// final thumbnailPath = await _genThumbnail(sourceFile, tempDir);
//
// // 返回一个元素的数组
// return {
// "tempFilePath": "/temp${copiedFile.path}",
// "size": copiedFile.lengthSync(),
// "width": sizeResult.size.width,
// "height": sizeResult.size.height,
// "thumbTempFilePath": '/temp$thumbnailPath',
// "fileType": copiedFile.path.split('/').last.split('.').last,
// };
// }
//
// Future<Map<String, dynamic>> _handleOne(XFile pickedFile, Directory tempDir) async {
// var sourceFile = File(pickedFile.path);
//
// // 通过image_size_getter获取图片尺寸
// final sizeResult = ImageSizeGetter.getSizeResult(FileInput(sourceFile));
// final thumbnailPath = await _genThumbnail(sourceFile, tempDir);
//
// // 返回一个元素的数组
// return {
// "tempFilePath": "/temp${sourceFile.path}",
// "size": sourceFile.lengthSync(),
// "width": sizeResult.size.width,
// "height": sizeResult.size.height,
// "thumbTempFilePath": '/temp$thumbnailPath',
// "fileType": sourceFile.path.split('/').last.split('.').last,
// };
// }
//
// Future<String?> _genThumbnail(File imageFile, Directory tempDir) async {
// try {
// // 缩略图路径
// final tempPath = tempDir.path;
// final targetPath = '$tempPath/thumbnail_${DateTime.now().millisecondsSinceEpoch}.jpg';
//
// // 压缩生成缩略图文件
// final compressedFile = await FlutterImageCompress.compressAndGetFile(imageFile.absolute.path, targetPath);
//
// return compressedFile!.path;
// } catch (e) {
// print('生成缩略图出错: $e');
// return null;
// }
// }
// }
import 'dart:io';
import 'package:appframe/services/dispatcher.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'package:url_launcher/url_launcher.dart';
class OpenDocumentHandler extends MessageHandler {
@override
......@@ -10,80 +7,22 @@ class OpenDocumentHandler extends MessageHandler {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
final url = params['url'] as String?;
if (url == null || url.isEmpty) {
final url = params['url'] as String;
if (url.isEmpty) {
throw Exception('参数错误');
}
if (url.startsWith("http://")) {
await saveNetworkImageToExternalStorage(url);
} else {
await saveLocalFileToExternalStorage(url);
}
return await _launchInBrowser(Uri.parse(url));
}
Future<bool> saveLocalFileToExternalStorage(String sourcePath) async {
try {
// 获取外部存储目录
final directory = await getExternalStorageDirectory();
// 获取文件名
final fileName = sourcePath.split('/').last;
var destinationPath = '${directory?.path}/$fileName';
// 检查文件是否存在,如果存在则生成新文件名
var counter = 1;
while (await File(destinationPath).exists()) {
final extension = fileName.split('.').last;
final nameWithoutExtension = fileName.substring(0, fileName.length - extension.length - 1);
destinationPath = '${directory?.path}/${nameWithoutExtension}_$counter.$extension}';
counter++;
}
// 复制文件
final sourceFile = File(sourcePath);
await sourceFile.copy(destinationPath);
return true;
} catch (e) {
print('保存文件失败: $e');
return false;
}
}
Future<bool> saveNetworkImageToExternalStorage(String imageUrl) async {
try {
// 获取外部存储目录
final directory = await getExternalStorageDirectory();
// 从URL获取文件名
final fileName = imageUrl.split('/').last;
var filePath = '${directory?.path}/$fileName';
// 检查文件是否存在,如果存在则生成新文件名
var counter = 1;
while (await File(filePath).exists()) {
final extension = fileName.split('.').last;
final nameWithoutExtension = fileName.substring(0, fileName.length - extension.length - 1);
filePath = '${directory?.path}/${nameWithoutExtension}_$counter.$extension}';
counter++;
}
// 下载网络图片
final response = await http.get(Uri.parse(imageUrl));
if (response.statusCode == 200) {
// 保存到外部存储
final file = File(filePath);
await file.writeAsBytes(response.bodyBytes);
return true;
Future<bool> _launchInBrowser(Uri url) async {
if (await canLaunchUrl(url)) {
return await launchUrl(url, mode: LaunchMode.platformDefault);
} else {
print('下载失败: ${response.statusCode}');
return false;
}
} catch (e) {
print('保存失败: $e');
return false;
throw Exception('Could not launch $url');
}
/*if (!await launchUrl(url, mode: LaunchMode.externalNonBrowserApplication)) {
throw Exception('Could not launch $url');
}*/
}
}
import 'package:appframe/config/locator.dart';
import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:shared_preferences/shared_preferences.dart';
class OrientationHandler extends MessageHandler {
late WebCubit? _webCubit;
late String? _message;
@override
void setCubit(WebCubit cubit) {
this._webCubit = cubit;
}
void _unfollowCubit() {
this._webCubit = null;
}
@override
void setMessage(String message) {
this._message = message;
}
@override
Future<dynamic> handleMessage(params) async {
final orientation = getIt.get<SharedPreferences>().get("orientation");
return {'type': orientation ?? 'portrait'};
_webCubit!.setOrientationCmdFlag(true, _message!);
}
}
import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/services/dispatcher.dart';
class SetTitleHandler extends MessageHandler {
late WebCubit? _webCubit;
@override
void setCubit(WebCubit cubit) {
this._webCubit = cubit;
}
void _unfollowCubit() {
this._webCubit = null;
}
@override
Future<dynamic> handleMessage(params) async {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
final String title = params['title'] as String;
final bool showBack = params['showBack'] as bool;
return _webCubit!.setTitle(title, showBack);
}
}
......@@ -19,8 +19,8 @@ class UploadFileHandler extends MessageHandler {
throw Exception('参数错误');
}
// final result = await compute(_handleUpload, {'filePath': tempFilePath});
final result = await _handleUpload({'filePath': tempFilePath});
final result = await compute(_handleUpload, {'filePath': tempFilePath});
// final result = await _handleUpload({'filePath': tempFilePath});
return result;
}
......@@ -57,7 +57,7 @@ class UploadFileHandler extends MessageHandler {
bxeApiService.close();
obsApiService.close();
return {'location': response.data['location']};
return {'url': response.data['location']};
}
/// 并行上传
......
import 'dart:convert';
import 'package:appframe/config/locator.dart';
import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:shared_preferences/shared_preferences.dart';
class WindowInfoHandler extends MessageHandler {
late WebCubit? _webCubit;
late String? _message;
@override
void setCubit(WebCubit cubit) {
this._webCubit = cubit;
}
void _unfollowCubit() {
this._webCubit = null;
}
@override
void setMessage(String message) {
this._message = message;
}
@override
Future<dynamic> handleMessage(dynamic params) async {
final windowInfo = getIt.get<SharedPreferences>().getString('windowInfo');
return jsonDecode(windowInfo!);
_webCubit!.setWindowInfoCmdFlag(true, _message!);
}
}
{
"@@locale": "en",
"appTitle": "WeChat Asset Picker Demo",
"appVersion": "Version: {version}",
"appVersionUnknown": "unknown",
"navMulti": "Multi",
"navSingle": "Single",
"navCustom": "Custom",
"selectedAssetsText": "Selected Assets",
"pickMethodNotice": "Pickers in this page are located at the {dist}, defined by `pickMethods`.",
"pickMethodImageName": "Image picker",
"pickMethodImageDescription": "Only pick image from device.",
"pickMethodVideoName": "Video picker",
"pickMethodVideoDescription": "Only pick video from device. (Includes Live Photos on iOS and macOS.)",
"pickMethodAudioName": "Audio picker",
"pickMethodAudioDescription": "Only pick audio from device.",
"pickMethodLivePhotoName": "Live Photo picker",
"pickMethodLivePhotoDescription": "Only pick Live Photos from device.",
"pickMethodCameraName": "Pick from camera",
"pickMethodCameraDescription": "Allow to pick an asset through camera.",
"pickMethodCameraAndStayName": "Pick from camera and stay",
"pickMethodCameraAndStayDescription": "Take a photo or video with the camera picker, select the result and stay in the entities list.",
"pickMethodCommonName": "Common picker",
"pickMethodCommonDescription": "Pick images and videos.",
"pickMethodThreeItemsGridName": "3 items grid",
"pickMethodThreeItemsGridDescription": "Picker will served as 3 items on cross axis. (pageSize must be a multiple of the gridCount)",
"pickMethodCustomFilterOptionsName": "Custom filter options",
"pickMethodCustomFilterOptionsDescription": "Add filter options for the picker.",
"pickMethodPrependItemName": "Prepend special item",
"pickMethodPrependItemDescription": "A special item will prepend to the assets grid.",
"pickMethodNoPreviewName": "No preview",
"pickMethodNoPreviewDescription": "You cannot preview assets during the picking, the behavior is like the WhatsApp/MegaTok pattern.",
"pickMethodKeepScrollOffsetName": "Keep scroll offset",
"pickMethodKeepScrollOffsetDescription": "Pick assets from same scroll position.",
"pickMethodChangeLanguagesName": "Change Languages",
"pickMethodChangeLanguagesDescription": "Pass AssetPickerTextDelegate to change between languages (e.g. EnglishAssetPickerTextDelegate).",
"pickMethodPreventGIFPickedName": "Prevent GIF being picked",
"pickMethodPreventGIFPickedDescription": "Use selectPredicate to banned GIF picking when tapped.",
"pickMethodCustomizableThemeName": "Customizable theme (ThemeData)",
"pickMethodCustomizableThemeDescription": "Picking assets with the light theme or with a different color.",
"pickMethodPathNameBuilderName": "Path name builder",
"pickMethodPathNameBuilderDescription": "Add \uD83C\uDF6D after paths name.",
"pickMethodWeChatMomentName": "WeChat Moment",
"pickMethodWeChatMomentDescription": "Pick assets with images or only 1 video.",
"pickMethodCustomImagePreviewThumbSizeName": "Custom image preview thumb size",
"pickMethodCustomImagePreviewThumbSizeDescription": "You can reduce the thumb size to get faster load speed.",
"customPickerNotice": "This page contains customized pickers with different asset types, different UI layouts, or some use case for specific apps. Contribute to add your custom picker are welcomed.\nPickers in this page are located at the lib/customs/pickers folder.",
"customPickerCallThePickerButton": "\uD83C\uDF81 Call the Picker",
"customPickerDirectoryAndFileName": "Directory+File picker",
"customPickerDirectoryAndFileDescription": "This is a custom picker built for `File`.\nBy browsing this picker, we want you to know that you can build your own picker components using the entity's type you desired.\n\nIn this page, picker will grab files from `getApplicationDocumentsDirectory`, then check whether it contains images. Put files into the path to see how this custom picker work.",
"customPickerMultiTabName": "Multi tab picker",
"customPickerMultiTabDescription": "The picker contains multiple tab with different types of assets for the picking at the same time.",
"customPickerMultiTabTab1": "All",
"customPickerMultiTabTab2": "Videos",
"customPickerMultiTabTab3": "Images",
"customPickerInstagramLayoutName": "Instagram layout picker",
"customPickerInstagramLayoutDescription": "The picker reproduces Instagram layout with preview and scroll animations. It's also published as the package insta_assets_picker."
}
\ No newline at end of file
{
"@@locale": "zh",
"appTitle": "WeChat Asset Picker 示例",
"appVersion": "版本:{version}",
"appVersionUnknown": "未知",
"navMulti": "多选",
"navSingle": "单选",
"navCustom": "自定义",
"selectedAssetsText": "已选的资源",
"pickMethodNotice": "该页面的所有选择器的代码位于 {dist},由 `pickMethods` 定义。",
"pickMethodCommonName": "常用选择",
"pickMethodCommonDescription": "选择图片和视频。",
"pickMethodImageName": "图片选择",
"pickMethodImageDescription": "仅选择图片。",
"pickMethodVideoName": "视频选择",
"pickMethodVideoDescription": "仅选择视频。",
"pickMethodAudioName": "音频选择",
"pickMethodAudioDescription": "仅选择音频。",
"pickMethodLivePhotoName": "实况图片选择",
"pickMethodLivePhotoDescription": "仅选择实况图片。",
"pickMethodCameraName": "从相机生成选择",
"pickMethodCameraDescription": "通过相机拍照生成并选择资源",
"pickMethodCameraAndStayName": "从相机生成选择并停留",
"pickMethodCameraAndStayDescription": "通过相机拍照生成选择资源,并停留在选择界面。",
"pickMethodThreeItemsGridName": "横向 3 格",
"pickMethodThreeItemsGridDescription": "选择器每行为 3 格。(pageSize 必须为 gridCount 的倍数)",
"pickMethodCustomFilterOptionsName": "自定义过滤条件",
"pickMethodCustomFilterOptionsDescription": "为选择器添加自定义过滤条件。",
"pickMethodPrependItemName": "往网格前插入 widget",
"pickMethodPrependItemDescription": "网格的靠前位置会添加一个自定义的 widget。",
"pickMethodNoPreviewName": "禁止预览",
"pickMethodNoPreviewDescription": "无法预览选择的资源,与 WhatsApp/MegaTok 的行为类似。",
"pickMethodKeepScrollOffsetName": "保持滚动位置",
"pickMethodKeepScrollOffsetDescription": "可以从上次滚动到的位置再次开始选择。",
"pickMethodChangeLanguagesName": "更改语言",
"pickMethodChangeLanguagesDescription": "传入 AssetPickerTextDelegate 手动更改选择器的语言(例如 EnglishAssetPickerTextDelegate)。",
"pickMethodPreventGIFPickedName": "禁止选择 GIF 图片",
"pickMethodPreventGIFPickedDescription": "通过 selectPredicate 来禁止 GIF 图片在点击时被选择。",
"pickMethodCustomizableThemeName": "自定义主题 (ThemeData)",
"pickMethodCustomizableThemeDescription": "可以用亮色或其他颜色及自定义的主题进行选择。",
"pickMethodPathNameBuilderName": "构建路径名称",
"pickMethodPathNameBuilderDescription": "在路径后添加 \uD83C\uDF6D 进行自定义。",
"pickMethodWeChatMomentName": "微信朋友圈模式",
"pickMethodWeChatMomentDescription": "允许选择图片或仅 1 个视频。",
"pickMethodCustomImagePreviewThumbSizeName": "自定义图片预览的缩略图大小",
"pickMethodCustomImagePreviewThumbSizeDescription": "通过降低缩略图的质量来获得更快的加载速度。",
"customPickerNotice": "本页面包含了多种方式、不同界面和特定应用的自定义选择器。欢迎贡献添加你自定义的选择器。\n该页面的所有选择器的代码位于 lib/customs/pickers 目录。",
"customPickerCallThePickerButton": "\uD83C\uDF81 开始选择资源",
"customPickerDirectoryAndFileName": "Directory+File 选择器",
"customPickerDirectoryAndFileDescription": "为 `File` 构建的自定义选择器。\n通过阅读该选择器的源码,你可以学习如何完全以你自定义的资源类型来构建并选择器的界面。\n\n该选择器会从 `getApplicationDocumentsDirectory` 目录获取资源,然后检查它是否包含图片。你需要将图片放在该目录来查看选择器的效果。",
"customPickerMultiTabName": "多 Tab 选择器",
"customPickerMultiTabDescription": "该选择器会以多 Tab 的形式同时展示多种资源类型的选择器。",
"customPickerMultiTabTab1": "全部",
"customPickerMultiTabTab2": "视频",
"customPickerMultiTabTab3": "图片",
"customPickerInstagramLayoutName": "Instagram 布局的选择器",
"customPickerInstagramLayoutDescription": "该选择器以 Instagram 的布局模式构建,在选择时可以同时预览。其已发布为单独的 package:insta_assets_picker。"
}
\ No newline at end of file
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart' as intl;
import 'app_localizations_en.dart';
import 'app_localizations_zh.dart';
// ignore_for_file: type=lint
/// Callers can lookup localized strings with an instance of AppLocalizations
/// returned by `AppLocalizations.of(context)`.
///
/// Applications need to include `AppLocalizations.delegate()` in their app's
/// `localizationDelegates` list, and the locales they support in the app's
/// `supportedLocales` list. For example:
///
/// ```dart
/// import 'gen/app_localizations.dart';
///
/// return MaterialApp(
/// localizationsDelegates: AppLocalizations.localizationsDelegates,
/// supportedLocales: AppLocalizations.supportedLocales,
/// home: MyApplicationHome(),
/// );
/// ```
///
/// ## Update pubspec.yaml
///
/// Please make sure to update your pubspec.yaml to include the following
/// packages:
///
/// ```yaml
/// dependencies:
/// # Internationalization support.
/// flutter_localizations:
/// sdk: flutter
/// intl: any # Use the pinned version from flutter_localizations
///
/// # Rest of dependencies
/// ```
///
/// ## iOS Applications
///
/// iOS applications define key application metadata, including supported
/// locales, in an Info.plist file that is built into the application bundle.
/// To configure the locales supported by your app, you’ll need to edit this
/// file.
///
/// First, open your project’s ios/Runner.xcworkspace Xcode workspace file.
/// Then, in the Project Navigator, open the Info.plist file under the Runner
/// project’s Runner folder.
///
/// Next, select the Information Property List item, select Add Item from the
/// Editor menu, then select Localizations from the pop-up menu.
///
/// Select and expand the newly-created Localizations item then, for each
/// locale your application supports, add a new item and select the locale
/// you wish to add from the pop-up menu in the Value field. This list should
/// be consistent with the languages listed in the AppLocalizations.supportedLocales
/// property.
abstract class AppLocalizations {
AppLocalizations(String locale)
: localeName = intl.Intl.canonicalizedLocale(locale.toString());
final String localeName;
static AppLocalizations? of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
static const LocalizationsDelegate<AppLocalizations> delegate =
_AppLocalizationsDelegate();
/// A list of this localizations delegate along with the default localizations
/// delegates.
///
/// Returns a list of localizations delegates containing this delegate along with
/// GlobalMaterialLocalizations.delegate, GlobalCupertinoLocalizations.delegate,
/// and GlobalWidgetsLocalizations.delegate.
///
/// Additional delegates can be added by appending to this list in
/// MaterialApp. This list does not have to be used at all if a custom list
/// of delegates is preferred or required.
static const List<LocalizationsDelegate<dynamic>> localizationsDelegates =
<LocalizationsDelegate<dynamic>>[
delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
];
/// A list of this localizations delegate's supported locales.
static const List<Locale> supportedLocales = <Locale>[
Locale('en'),
Locale('zh')
];
/// No description provided for @appTitle.
///
/// In en, this message translates to:
/// **'WeChat Asset Picker Demo'**
String get appTitle;
/// No description provided for @appVersion.
///
/// In en, this message translates to:
/// **'Version: {version}'**
String appVersion(Object version);
/// No description provided for @appVersionUnknown.
///
/// In en, this message translates to:
/// **'unknown'**
String get appVersionUnknown;
/// No description provided for @navMulti.
///
/// In en, this message translates to:
/// **'Multi'**
String get navMulti;
/// No description provided for @navSingle.
///
/// In en, this message translates to:
/// **'Single'**
String get navSingle;
/// No description provided for @navCustom.
///
/// In en, this message translates to:
/// **'Custom'**
String get navCustom;
/// No description provided for @selectedAssetsText.
///
/// In en, this message translates to:
/// **'Selected Assets'**
String get selectedAssetsText;
/// No description provided for @pickMethodNotice.
///
/// In en, this message translates to:
/// **'Pickers in this page are located at the {dist}, defined by `pickMethods`.'**
String pickMethodNotice(Object dist);
/// No description provided for @pickMethodImageName.
///
/// In en, this message translates to:
/// **'Image picker'**
String get pickMethodImageName;
/// No description provided for @pickMethodImageDescription.
///
/// In en, this message translates to:
/// **'Only pick image from device.'**
String get pickMethodImageDescription;
/// No description provided for @pickMethodVideoName.
///
/// In en, this message translates to:
/// **'Video picker'**
String get pickMethodVideoName;
/// No description provided for @pickMethodVideoDescription.
///
/// In en, this message translates to:
/// **'Only pick video from device. (Includes Live Photos on iOS and macOS.)'**
String get pickMethodVideoDescription;
/// No description provided for @pickMethodAudioName.
///
/// In en, this message translates to:
/// **'Audio picker'**
String get pickMethodAudioName;
/// No description provided for @pickMethodAudioDescription.
///
/// In en, this message translates to:
/// **'Only pick audio from device.'**
String get pickMethodAudioDescription;
/// No description provided for @pickMethodLivePhotoName.
///
/// In en, this message translates to:
/// **'Live Photo picker'**
String get pickMethodLivePhotoName;
/// No description provided for @pickMethodLivePhotoDescription.
///
/// In en, this message translates to:
/// **'Only pick Live Photos from device.'**
String get pickMethodLivePhotoDescription;
/// No description provided for @pickMethodCameraName.
///
/// In en, this message translates to:
/// **'Pick from camera'**
String get pickMethodCameraName;
/// No description provided for @pickMethodCameraDescription.
///
/// In en, this message translates to:
/// **'Allow to pick an asset through camera.'**
String get pickMethodCameraDescription;
/// No description provided for @pickMethodCameraAndStayName.
///
/// In en, this message translates to:
/// **'Pick from camera and stay'**
String get pickMethodCameraAndStayName;
/// No description provided for @pickMethodCameraAndStayDescription.
///
/// In en, this message translates to:
/// **'Take a photo or video with the camera picker, select the result and stay in the entities list.'**
String get pickMethodCameraAndStayDescription;
/// No description provided for @pickMethodCommonName.
///
/// In en, this message translates to:
/// **'Common picker'**
String get pickMethodCommonName;
/// No description provided for @pickMethodCommonDescription.
///
/// In en, this message translates to:
/// **'Pick images and videos.'**
String get pickMethodCommonDescription;
/// No description provided for @pickMethodThreeItemsGridName.
///
/// In en, this message translates to:
/// **'3 items grid'**
String get pickMethodThreeItemsGridName;
/// No description provided for @pickMethodThreeItemsGridDescription.
///
/// In en, this message translates to:
/// **'Picker will served as 3 items on cross axis. (pageSize must be a multiple of the gridCount)'**
String get pickMethodThreeItemsGridDescription;
/// No description provided for @pickMethodCustomFilterOptionsName.
///
/// In en, this message translates to:
/// **'Custom filter options'**
String get pickMethodCustomFilterOptionsName;
/// No description provided for @pickMethodCustomFilterOptionsDescription.
///
/// In en, this message translates to:
/// **'Add filter options for the picker.'**
String get pickMethodCustomFilterOptionsDescription;
/// No description provided for @pickMethodPrependItemName.
///
/// In en, this message translates to:
/// **'Prepend special item'**
String get pickMethodPrependItemName;
/// No description provided for @pickMethodPrependItemDescription.
///
/// In en, this message translates to:
/// **'A special item will prepend to the assets grid.'**
String get pickMethodPrependItemDescription;
/// No description provided for @pickMethodNoPreviewName.
///
/// In en, this message translates to:
/// **'No preview'**
String get pickMethodNoPreviewName;
/// No description provided for @pickMethodNoPreviewDescription.
///
/// In en, this message translates to:
/// **'You cannot preview assets during the picking, the behavior is like the WhatsApp/MegaTok pattern.'**
String get pickMethodNoPreviewDescription;
/// No description provided for @pickMethodKeepScrollOffsetName.
///
/// In en, this message translates to:
/// **'Keep scroll offset'**
String get pickMethodKeepScrollOffsetName;
/// No description provided for @pickMethodKeepScrollOffsetDescription.
///
/// In en, this message translates to:
/// **'Pick assets from same scroll position.'**
String get pickMethodKeepScrollOffsetDescription;
/// No description provided for @pickMethodChangeLanguagesName.
///
/// In en, this message translates to:
/// **'Change Languages'**
String get pickMethodChangeLanguagesName;
/// No description provided for @pickMethodChangeLanguagesDescription.
///
/// In en, this message translates to:
/// **'Pass AssetPickerTextDelegate to change between languages (e.g. EnglishAssetPickerTextDelegate).'**
String get pickMethodChangeLanguagesDescription;
/// No description provided for @pickMethodPreventGIFPickedName.
///
/// In en, this message translates to:
/// **'Prevent GIF being picked'**
String get pickMethodPreventGIFPickedName;
/// No description provided for @pickMethodPreventGIFPickedDescription.
///
/// In en, this message translates to:
/// **'Use selectPredicate to banned GIF picking when tapped.'**
String get pickMethodPreventGIFPickedDescription;
/// No description provided for @pickMethodCustomizableThemeName.
///
/// In en, this message translates to:
/// **'Customizable theme (ThemeData)'**
String get pickMethodCustomizableThemeName;
/// No description provided for @pickMethodCustomizableThemeDescription.
///
/// In en, this message translates to:
/// **'Picking assets with the light theme or with a different color.'**
String get pickMethodCustomizableThemeDescription;
/// No description provided for @pickMethodPathNameBuilderName.
///
/// In en, this message translates to:
/// **'Path name builder'**
String get pickMethodPathNameBuilderName;
/// No description provided for @pickMethodPathNameBuilderDescription.
///
/// In en, this message translates to:
/// **'Add 🍭 after paths name.'**
String get pickMethodPathNameBuilderDescription;
/// No description provided for @pickMethodWeChatMomentName.
///
/// In en, this message translates to:
/// **'WeChat Moment'**
String get pickMethodWeChatMomentName;
/// No description provided for @pickMethodWeChatMomentDescription.
///
/// In en, this message translates to:
/// **'Pick assets with images or only 1 video.'**
String get pickMethodWeChatMomentDescription;
/// No description provided for @pickMethodCustomImagePreviewThumbSizeName.
///
/// In en, this message translates to:
/// **'Custom image preview thumb size'**
String get pickMethodCustomImagePreviewThumbSizeName;
/// No description provided for @pickMethodCustomImagePreviewThumbSizeDescription.
///
/// In en, this message translates to:
/// **'You can reduce the thumb size to get faster load speed.'**
String get pickMethodCustomImagePreviewThumbSizeDescription;
/// No description provided for @customPickerNotice.
///
/// In en, this message translates to:
/// **'This page contains customized pickers with different asset types, different UI layouts, or some use case for specific apps. Contribute to add your custom picker are welcomed.\nPickers in this page are located at the lib/customs/pickers folder.'**
String get customPickerNotice;
/// No description provided for @customPickerCallThePickerButton.
///
/// In en, this message translates to:
/// **'🎁 Call the Picker'**
String get customPickerCallThePickerButton;
/// No description provided for @customPickerDirectoryAndFileName.
///
/// In en, this message translates to:
/// **'Directory+File picker'**
String get customPickerDirectoryAndFileName;
/// No description provided for @customPickerDirectoryAndFileDescription.
///
/// In en, this message translates to:
/// **'This is a custom picker built for `File`.\nBy browsing this picker, we want you to know that you can build your own picker components using the entity\'s type you desired.\n\nIn this page, picker will grab files from `getApplicationDocumentsDirectory`, then check whether it contains images. Put files into the path to see how this custom picker work.'**
String get customPickerDirectoryAndFileDescription;
/// No description provided for @customPickerMultiTabName.
///
/// In en, this message translates to:
/// **'Multi tab picker'**
String get customPickerMultiTabName;
/// No description provided for @customPickerMultiTabDescription.
///
/// In en, this message translates to:
/// **'The picker contains multiple tab with different types of assets for the picking at the same time.'**
String get customPickerMultiTabDescription;
/// No description provided for @customPickerMultiTabTab1.
///
/// In en, this message translates to:
/// **'All'**
String get customPickerMultiTabTab1;
/// No description provided for @customPickerMultiTabTab2.
///
/// In en, this message translates to:
/// **'Videos'**
String get customPickerMultiTabTab2;
/// No description provided for @customPickerMultiTabTab3.
///
/// In en, this message translates to:
/// **'Images'**
String get customPickerMultiTabTab3;
/// No description provided for @customPickerInstagramLayoutName.
///
/// In en, this message translates to:
/// **'Instagram layout picker'**
String get customPickerInstagramLayoutName;
/// No description provided for @customPickerInstagramLayoutDescription.
///
/// In en, this message translates to:
/// **'The picker reproduces Instagram layout with preview and scroll animations. It\'s also published as the package insta_assets_picker.'**
String get customPickerInstagramLayoutDescription;
}
class _AppLocalizationsDelegate
extends LocalizationsDelegate<AppLocalizations> {
const _AppLocalizationsDelegate();
@override
Future<AppLocalizations> load(Locale locale) {
return SynchronousFuture<AppLocalizations>(lookupAppLocalizations(locale));
}
@override
bool isSupported(Locale locale) =>
<String>['en', 'zh'].contains(locale.languageCode);
@override
bool shouldReload(_AppLocalizationsDelegate old) => false;
}
AppLocalizations lookupAppLocalizations(Locale locale) {
// Lookup logic when only language code is specified.
switch (locale.languageCode) {
case 'en':
return AppLocalizationsEn();
case 'zh':
return AppLocalizationsZh();
}
throw FlutterError(
'AppLocalizations.delegate failed to load unsupported locale "$locale". This is likely '
'an issue with the localizations generation tool. Please file an issue '
'on GitHub with a reproducible sample app and the gen-l10n configuration '
'that was used.');
}
// ignore: unused_import
import 'package:intl/intl.dart' as intl;
import 'app_localizations.dart';
// ignore_for_file: type=lint
/// The translations for English (`en`).
class AppLocalizationsEn extends AppLocalizations {
AppLocalizationsEn([String locale = 'en']) : super(locale);
@override
String get appTitle => 'WeChat Asset Picker Demo';
@override
String appVersion(Object version) {
return 'Version: $version';
}
@override
String get appVersionUnknown => 'unknown';
@override
String get navMulti => 'Multi';
@override
String get navSingle => 'Single';
@override
String get navCustom => 'Custom';
@override
String get selectedAssetsText => 'Selected Assets';
@override
String pickMethodNotice(Object dist) {
return 'Pickers in this page are located at the $dist, defined by `pickMethods`.';
}
@override
String get pickMethodImageName => 'Image picker';
@override
String get pickMethodImageDescription => 'Only pick image from device.';
@override
String get pickMethodVideoName => 'Video picker';
@override
String get pickMethodVideoDescription =>
'Only pick video from device. (Includes Live Photos on iOS and macOS.)';
@override
String get pickMethodAudioName => 'Audio picker';
@override
String get pickMethodAudioDescription => 'Only pick audio from device.';
@override
String get pickMethodLivePhotoName => 'Live Photo picker';
@override
String get pickMethodLivePhotoDescription =>
'Only pick Live Photos from device.';
@override
String get pickMethodCameraName => 'Pick from camera';
@override
String get pickMethodCameraDescription =>
'Allow to pick an asset through camera.';
@override
String get pickMethodCameraAndStayName => 'Pick from camera and stay';
@override
String get pickMethodCameraAndStayDescription =>
'Take a photo or video with the camera picker, select the result and stay in the entities list.';
@override
String get pickMethodCommonName => 'Common picker';
@override
String get pickMethodCommonDescription => 'Pick images and videos.';
@override
String get pickMethodThreeItemsGridName => '3 items grid';
@override
String get pickMethodThreeItemsGridDescription =>
'Picker will served as 3 items on cross axis. (pageSize must be a multiple of the gridCount)';
@override
String get pickMethodCustomFilterOptionsName => 'Custom filter options';
@override
String get pickMethodCustomFilterOptionsDescription =>
'Add filter options for the picker.';
@override
String get pickMethodPrependItemName => 'Prepend special item';
@override
String get pickMethodPrependItemDescription =>
'A special item will prepend to the assets grid.';
@override
String get pickMethodNoPreviewName => 'No preview';
@override
String get pickMethodNoPreviewDescription =>
'You cannot preview assets during the picking, the behavior is like the WhatsApp/MegaTok pattern.';
@override
String get pickMethodKeepScrollOffsetName => 'Keep scroll offset';
@override
String get pickMethodKeepScrollOffsetDescription =>
'Pick assets from same scroll position.';
@override
String get pickMethodChangeLanguagesName => 'Change Languages';
@override
String get pickMethodChangeLanguagesDescription =>
'Pass AssetPickerTextDelegate to change between languages (e.g. EnglishAssetPickerTextDelegate).';
@override
String get pickMethodPreventGIFPickedName => 'Prevent GIF being picked';
@override
String get pickMethodPreventGIFPickedDescription =>
'Use selectPredicate to banned GIF picking when tapped.';
@override
String get pickMethodCustomizableThemeName =>
'Customizable theme (ThemeData)';
@override
String get pickMethodCustomizableThemeDescription =>
'Picking assets with the light theme or with a different color.';
@override
String get pickMethodPathNameBuilderName => 'Path name builder';
@override
String get pickMethodPathNameBuilderDescription => 'Add 🍭 after paths name.';
@override
String get pickMethodWeChatMomentName => 'WeChat Moment';
@override
String get pickMethodWeChatMomentDescription =>
'Pick assets with images or only 1 video.';
@override
String get pickMethodCustomImagePreviewThumbSizeName =>
'Custom image preview thumb size';
@override
String get pickMethodCustomImagePreviewThumbSizeDescription =>
'You can reduce the thumb size to get faster load speed.';
@override
String get customPickerNotice =>
'This page contains customized pickers with different asset types, different UI layouts, or some use case for specific apps. Contribute to add your custom picker are welcomed.\nPickers in this page are located at the lib/customs/pickers folder.';
@override
String get customPickerCallThePickerButton => '🎁 Call the Picker';
@override
String get customPickerDirectoryAndFileName => 'Directory+File picker';
@override
String get customPickerDirectoryAndFileDescription =>
'This is a custom picker built for `File`.\nBy browsing this picker, we want you to know that you can build your own picker components using the entity\'s type you desired.\n\nIn this page, picker will grab files from `getApplicationDocumentsDirectory`, then check whether it contains images. Put files into the path to see how this custom picker work.';
@override
String get customPickerMultiTabName => 'Multi tab picker';
@override
String get customPickerMultiTabDescription =>
'The picker contains multiple tab with different types of assets for the picking at the same time.';
@override
String get customPickerMultiTabTab1 => 'All';
@override
String get customPickerMultiTabTab2 => 'Videos';
@override
String get customPickerMultiTabTab3 => 'Images';
@override
String get customPickerInstagramLayoutName => 'Instagram layout picker';
@override
String get customPickerInstagramLayoutDescription =>
'The picker reproduces Instagram layout with preview and scroll animations. It\'s also published as the package insta_assets_picker.';
}
// ignore: unused_import
import 'package:intl/intl.dart' as intl;
import 'app_localizations.dart';
// ignore_for_file: type=lint
/// The translations for Chinese (`zh`).
class AppLocalizationsZh extends AppLocalizations {
AppLocalizationsZh([String locale = 'zh']) : super(locale);
@override
String get appTitle => 'WeChat Asset Picker 示例';
@override
String appVersion(Object version) {
return '版本:$version';
}
@override
String get appVersionUnknown => '未知';
@override
String get navMulti => '多选';
@override
String get navSingle => '单选';
@override
String get navCustom => '自定义';
@override
String get selectedAssetsText => '已选的资源';
@override
String pickMethodNotice(Object dist) {
return '该页面的所有选择器的代码位于 $dist,由 `pickMethods` 定义。';
}
@override
String get pickMethodImageName => '图片选择';
@override
String get pickMethodImageDescription => '仅选择图片。';
@override
String get pickMethodVideoName => '视频选择';
@override
String get pickMethodVideoDescription => '仅选择视频。';
@override
String get pickMethodAudioName => '音频选择';
@override
String get pickMethodAudioDescription => '仅选择音频。';
@override
String get pickMethodLivePhotoName => '实况图片选择';
@override
String get pickMethodLivePhotoDescription => '仅选择实况图片。';
@override
String get pickMethodCameraName => '从相机生成选择';
@override
String get pickMethodCameraDescription => '通过相机拍照生成并选择资源';
@override
String get pickMethodCameraAndStayName => '从相机生成选择并停留';
@override
String get pickMethodCameraAndStayDescription => '通过相机拍照生成选择资源,并停留在选择界面。';
@override
String get pickMethodCommonName => '常用选择';
@override
String get pickMethodCommonDescription => '选择图片和视频。';
@override
String get pickMethodThreeItemsGridName => '横向 3 格';
@override
String get pickMethodThreeItemsGridDescription =>
'选择器每行为 3 格。(pageSize 必须为 gridCount 的倍数)';
@override
String get pickMethodCustomFilterOptionsName => '自定义过滤条件';
@override
String get pickMethodCustomFilterOptionsDescription => '为选择器添加自定义过滤条件。';
@override
String get pickMethodPrependItemName => '往网格前插入 widget';
@override
String get pickMethodPrependItemDescription => '网格的靠前位置会添加一个自定义的 widget。';
@override
String get pickMethodNoPreviewName => '禁止预览';
@override
String get pickMethodNoPreviewDescription =>
'无法预览选择的资源,与 WhatsApp/MegaTok 的行为类似。';
@override
String get pickMethodKeepScrollOffsetName => '保持滚动位置';
@override
String get pickMethodKeepScrollOffsetDescription => '可以从上次滚动到的位置再次开始选择。';
@override
String get pickMethodChangeLanguagesName => '更改语言';
@override
String get pickMethodChangeLanguagesDescription =>
'传入 AssetPickerTextDelegate 手动更改选择器的语言(例如 EnglishAssetPickerTextDelegate)。';
@override
String get pickMethodPreventGIFPickedName => '禁止选择 GIF 图片';
@override
String get pickMethodPreventGIFPickedDescription =>
'通过 selectPredicate 来禁止 GIF 图片在点击时被选择。';
@override
String get pickMethodCustomizableThemeName => '自定义主题 (ThemeData)';
@override
String get pickMethodCustomizableThemeDescription => '可以用亮色或其他颜色及自定义的主题进行选择。';
@override
String get pickMethodPathNameBuilderName => '构建路径名称';
@override
String get pickMethodPathNameBuilderDescription => '在路径后添加 🍭 进行自定义。';
@override
String get pickMethodWeChatMomentName => '微信朋友圈模式';
@override
String get pickMethodWeChatMomentDescription => '允许选择图片或仅 1 个视频。';
@override
String get pickMethodCustomImagePreviewThumbSizeName => '自定义图片预览的缩略图大小';
@override
String get pickMethodCustomImagePreviewThumbSizeDescription =>
'通过降低缩略图的质量来获得更快的加载速度。';
@override
String get customPickerNotice =>
'本页面包含了多种方式、不同界面和特定应用的自定义选择器。欢迎贡献添加你自定义的选择器。\n该页面的所有选择器的代码位于 lib/customs/pickers 目录。';
@override
String get customPickerCallThePickerButton => '🎁 开始选择资源';
@override
String get customPickerDirectoryAndFileName => 'Directory+File 选择器';
@override
String get customPickerDirectoryAndFileDescription =>
'为 `File` 构建的自定义选择器。\n通过阅读该选择器的源码,你可以学习如何完全以你自定义的资源类型来构建并选择器的界面。\n\n该选择器会从 `getApplicationDocumentsDirectory` 目录获取资源,然后检查它是否包含图片。你需要将图片放在该目录来查看选择器的效果。';
@override
String get customPickerMultiTabName => '多 Tab 选择器';
@override
String get customPickerMultiTabDescription =>
'该选择器会以多 Tab 的形式同时展示多种资源类型的选择器。';
@override
String get customPickerMultiTabTab1 => '全部';
@override
String get customPickerMultiTabTab2 => '视频';
@override
String get customPickerMultiTabTab3 => '图片';
@override
String get customPickerInstagramLayoutName => 'Instagram 布局的选择器';
@override
String get customPickerInstagramLayoutDescription =>
'该选择器以 Instagram 的布局模式构建,在选择时可以同时预览。其已发布为单独的 package:insta_assets_picker。';
}
import 'dart:convert';
import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/config/locator.dart';
import 'package:appframe/data/models/message/h5_message.dart';
......@@ -5,11 +7,11 @@ import 'package:appframe/data/models/message/h5_resp.dart';
// 消息处理器抽象类
abstract class MessageHandler {
// Future<Map<String, dynamic>> handleMessage(Map<String, dynamic> params);
// Future<dynamic> handleMessage(Map<String, dynamic> params);
Future<dynamic> handleMessage(dynamic params);
void setCubit(WebCubit cubit) {}
void setMessage(String message) {}
}
// 消息分发器
......@@ -27,7 +29,10 @@ class MessageDispatcher {
}
// 分发处理
Future<void> dispatch(H5Message h5Message, Function callback, {WebCubit? webCubit}) async {
Future<void> dispatch(String message, Function callback, {WebCubit? webCubit}) async {
final Map<String, dynamic> data = json.decode(message);
H5Message h5Message = H5Message.fromJson(data);
var handler = _handlers[h5Message.cmd];
if (handler == null) {
try {
......@@ -43,11 +48,21 @@ class MessageDispatcher {
try {
// 设置传递的cubit,进行业务操作
if (h5Message.cmd == 'scanCode' || h5Message.cmd.startsWith("audio")) {
if (h5Message.cmd == 'scanCode' ||
h5Message.cmd == "getOrientation" ||
h5Message.cmd == "getWindowInfo" ||
h5Message.cmd == "chooseImage" ||
h5Message.cmd.startsWith("audio") ||
h5Message.cmd.startsWith("setTitle")) {
handler.setCubit(webCubit!);
handler.setMessage(message);
}
final result = await handler.handleMessage(h5Message.params);
// 有些命令需要通过监听器调用Cubit,触发调用时不需要返回结果,不处理回调
if (result == null) {
return;
}
H5Resp h5Resp = H5Resp(h5Message.unique, h5Message.cmd, result, '');
callback(h5Resp.toJson());
} catch (e) {
......
......@@ -6,8 +6,8 @@ import 'package:flutter/services.dart';
class LocalServerService {
// 启动本地HTTP服务器
Future<HttpServer> startLocalServer() async {
//HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, 35982);
HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, 35982);
// HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
print('本地服务器启动在端口: ${server.port}');
server.listen((HttpRequest request) async {
......
import 'dart:convert';
import 'package:appframe/bloc/web_cubit.dart';
import 'package:appframe/config/locator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebPage extends StatefulWidget {
class WebPage extends StatelessWidget {
const WebPage({super.key});
@override
State<WebPage> createState() => _WebPageState();
}
class _WebPageState extends State<WebPage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Orientation? _lastOrientation;
/// 用于监听处理屏幕方向变化
@override
Future<void> didChangeMetrics() async {
print('didChangeMetrics------------------------------->');
final mediaQuery = MediaQuery.of(context);
// 方向
final orientation = mediaQuery.orientation;
if (orientation != _lastOrientation) {
_lastOrientation = orientation;
await getIt.get<SharedPreferences>().setString(
'orientation',
orientation == Orientation.portrait ? "portrait" : "landscape",
);
final viewPadding = mediaQuery.viewPadding;
final size = mediaQuery.size;
final safeArea = mediaQuery.padding;
final devicePixelRatio = mediaQuery.devicePixelRatio;
// 计算安全区域坐标
final safeAreaLeft = safeArea.left;
final safeAreaRight = size.width - safeArea.right;
final safeAreaTop = safeArea.top;
final safeAreaBottom = size.height - safeArea.bottom;
final safeAreaWidth = size.width - safeArea.horizontal;
final safeAreaHeight = size.height - safeArea.vertical;
final windowInfo = {
'pixelRatio': devicePixelRatio,
'screenWidth': size.width * devicePixelRatio,
'screenHeight': size.height * devicePixelRatio,
'windowWidth': size.width,
'windowHeight': size.height,
'statusBarHeight': viewPadding.top,
'screenTop': 0, // Flutter中通常不使用此值,设为0
'safeArea': {
'left': safeAreaLeft,
'right': safeAreaRight,
'top': safeAreaTop,
'bottom': safeAreaBottom,
'width': safeAreaWidth,
'height': safeAreaHeight,
},
};
await getIt.get<SharedPreferences>().setString('windowInfo', jsonEncode(windowInfo));
}
}
// @override
// State<WebPage> createState() => _WebPageState();
@override
Widget build(BuildContext context) {
final Map<String, dynamic>? extraData = GoRouterState.of(context).extra as Map<String, dynamic>?;
Widget build(BuildContext buildContext) {
final Map<String, dynamic>? extraData = GoRouterState.of(buildContext).extra as Map<String, dynamic>?;
var ip = extraData?['ip'] ?? '127.0.0.1';
var sessionCode = extraData?['sessionCode'];
......@@ -101,18 +33,65 @@ class _WebPageState extends State<WebPage> with WidgetsBindingObserver {
),
),
child: BlocConsumer<WebCubit, WebState>(
builder: (context, state) {
builder: (ctx, state) {
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) {
context.read<WebCubit>().handleBack();
ctx.read<WebCubit>().handleBack();
},
child: Scaffold(
appBar: AppBar(title: Text(state.title)),
appBar: AppBar(
title: Text(state.title),
centerTitle: true,
automaticallyImplyLeading: false,
leading: state.beBack
? IconButton(
icon: const Icon(Icons.arrow_back_ios),
onPressed: () {
ctx.read<WebCubit>().handleBack();
},
)
: null,
actions: [
Builder(
builder: (BuildContext context) {
return IconButton(
icon: const Icon(Icons.more_vert),
onPressed: () {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return SizedBox(
height: 200,
child: Column(
children: [
ListTile(
title: Text('选项1'),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
title: Text('选项2'),
onTap: () {
Navigator.pop(context);
},
),
],
),
);
},
);
},
);
},
),
],
),
body: state.loaded
? SizedBox(
height: MediaQuery.of(context).size.height - 120, // 减去100像素留空
child: WebViewWidget(controller: context.read<WebCubit>().controller),
height: MediaQuery.of(ctx).size.height - 120, // 减去100像素留空
child: WebViewWidget(controller: ctx.read<WebCubit>().controller),
)
: const Center(child: CircularProgressIndicator()),
// 用于测试一下点击跳转路由
......@@ -122,7 +101,7 @@ class _WebPageState extends State<WebPage> with WidgetsBindingObserver {
FloatingActionButton(
heroTag: "btn1",
onPressed: () {
context.read<WebCubit>().goWechatAuth();
ctx.read<WebCubit>().goWechatAuth();
},
child: const Icon(Icons.chat_outlined),
),
......@@ -130,7 +109,7 @@ class _WebPageState extends State<WebPage> with WidgetsBindingObserver {
FloatingActionButton(
heroTag: "btn2",
onPressed: () {
context.read<WebCubit>().goAuth();
ctx.read<WebCubit>().goAuth();
},
child: const Icon(Icons.accessibility_new),
),
......@@ -138,23 +117,236 @@ class _WebPageState extends State<WebPage> with WidgetsBindingObserver {
FloatingActionButton(
heroTag: "btn3",
onPressed: () {
context.read<WebCubit>().goMiniProgram();
ctx.read<WebCubit>().goMiniProgram();
},
child: const Icon(Icons.app_blocking_sharp),
),
const SizedBox(height: 16),
FloatingActionButton(
heroTag: "btn4",
onPressed: () async {
ctx.read<WebCubit>().refresh();
},
child: const Icon(Icons.refresh),
),
],
),
),
);
},
listener: (context, state) {
if (!state.recorderIsInit) {}
if (state.orientationCmdFlag) {
context.read<WebCubit>().getOrientation(context);
} else if (state.windowInfoCmdFlag) {
context.read<WebCubit>().getWindowInfo(context);
} else if (state.chooseImageCmdFlag) {
context.read<WebCubit>().chooseImage(context);
}
},
),
);
}
}
// class _WebPageState extends State<WebPage> with WidgetsBindingObserver {
// @override
// void initState() {
// super.initState();
// WidgetsBinding.instance.addObserver(this);
// }
//
// @override
// void dispose() {
// WidgetsBinding.instance.removeObserver(this);
// super.dispose();
// }
//
// Orientation? _lastOrientation;
//
// /// 用于监听处理屏幕方向变化
// @override
// Future<void> didChangeMetrics() async {
// print('didChangeMetrics------------------------------->');
// final mediaQuery = MediaQuery.of(context);
// // 方向
// final orientation = mediaQuery.orientation;
// if (orientation != _lastOrientation) {
// _lastOrientation = orientation;
// await getIt.get<SharedPreferences>().setString(
// 'orientation',
// orientation == Orientation.portrait ? "portrait" : "landscape",
// );
//
// final viewPadding = mediaQuery.viewPadding;
// final size = mediaQuery.size;
// final safeArea = mediaQuery.padding;
// final devicePixelRatio = mediaQuery.devicePixelRatio;
//
// // 计算安全区域坐标
// final safeAreaLeft = safeArea.left;
// final safeAreaRight = size.width - safeArea.right;
// final safeAreaTop = safeArea.top;
// final safeAreaBottom = size.height - safeArea.bottom;
// final safeAreaWidth = size.width - safeArea.horizontal;
// final safeAreaHeight = size.height - safeArea.vertical;
//
// final windowInfo = {
// 'pixelRatio': devicePixelRatio,
// 'screenWidth': size.width * devicePixelRatio,
// 'screenHeight': size.height * devicePixelRatio,
// 'windowWidth': size.width,
// 'windowHeight': size.height,
// 'statusBarHeight': viewPadding.top,
// 'screenTop': 0, // Flutter中通常不使用此值,设为0
// 'safeArea': {
// 'left': safeAreaLeft,
// 'right': safeAreaRight,
// 'top': safeAreaTop,
// 'bottom': safeAreaBottom,
// 'width': safeAreaWidth,
// 'height': safeAreaHeight,
// },
// };
// await getIt.get<SharedPreferences>().setString('windowInfo', jsonEncode(windowInfo));
// }
// }
//
// @override
// Widget build(BuildContext buildContext) {
// final Map<String, dynamic>? extraData = GoRouterState.of(buildContext).extra as Map<String, dynamic>?;
//
// var ip = extraData?['ip'] ?? '127.0.0.1';
// var sessionCode = extraData?['sessionCode'];
// var userCode = extraData?['userCode'];
// var classCode = extraData?['classCode'];
// var userType = extraData?['userType'];
// var stuId = extraData?['stuId'];
//
// return BlocProvider(
// create: (context) => WebCubit(
// WebState(
// ip: ip,
// sessionCode: sessionCode,
// userCode: userCode,
// classCode: classCode,
// userType: userType,
// stuId: stuId,
// ),
// ),
// child: BlocConsumer<WebCubit, WebState>(
// builder: (ctx, state) {
// return PopScope(
// canPop: false,
// onPopInvokedWithResult: (didPop, result) {
// ctx.read<WebCubit>().handleBack();
// },
// child: Scaffold(
// appBar: AppBar(
// title: Text(state.title),
// centerTitle: true,
// automaticallyImplyLeading: false,
// leading: state.beBack
// ? IconButton(
// icon: const Icon(Icons.arrow_back_ios),
// onPressed: () {
// ctx.read<WebCubit>().handleBack();
// },
// )
// : null,
// actions: [
// Builder(
// builder: (BuildContext context) {
// return IconButton(
// icon: const Icon(Icons.more_vert),
// onPressed: () {
// showModalBottomSheet(
// context: context,
// builder: (BuildContext context) {
// return SizedBox(
// height: 200,
// child: Column(
// children: [
// ListTile(
// title: Text('选项1'),
// onTap: () {
// Navigator.pop(context);
// },
// ),
// ListTile(
// title: Text('选项2'),
// onTap: () {
// Navigator.pop(context);
// },
// ),
// ],
// ),
// );
// },
// );
// },
// );
// },
// ),
// ],
// ),
// body: state.loaded
// ? SizedBox(
// height: MediaQuery.of(ctx).size.height - 120, // 减去100像素留空
// child: WebViewWidget(controller: ctx.read<WebCubit>().controller),
// )
// : const Center(child: CircularProgressIndicator()),
// // 用于测试一下点击跳转路由
// floatingActionButton: Column(
// mainAxisAlignment: MainAxisAlignment.end,
// children: [
// FloatingActionButton(
// heroTag: "btn1",
// onPressed: () {
// ctx.read<WebCubit>().goWechatAuth();
// },
// child: const Icon(Icons.chat_outlined),
// ),
// const SizedBox(height: 16),
// FloatingActionButton(
// heroTag: "btn2",
// onPressed: () {
// ctx.read<WebCubit>().goAuth();
// },
// child: const Icon(Icons.accessibility_new),
// ),
// const SizedBox(height: 16),
// FloatingActionButton(
// heroTag: "btn3",
// onPressed: () {
// ctx.read<WebCubit>().goMiniProgram();
// },
// child: const Icon(Icons.app_blocking_sharp),
// ),
// const SizedBox(height: 16),
// FloatingActionButton(
// heroTag: "btn4",
// onPressed: () async {
// ctx.read<WebCubit>().refresh();
// },
// child: const Icon(Icons.refresh),
// ),
// ],
// ),
// ),
// );
// },
// listener: (context, state) {
// if (state.orientationCmdFlag) {
// context.read<WebCubit>().getOrientation(context);
// } else if (state.windowInfoCmdFlag) {
// context.read<WebCubit>().getWindowInfo(context);
// }
// },
// ),
// );
// }
// }
// class WebPage extends StatelessWidget {
// const WebPage({super.key});
//
......
......@@ -21,7 +21,7 @@ class _RecorderWidgetState extends State<RecorderWidget> {
late BuildContext _context;
@override
Widget build(BuildContext context) {
Widget build(BuildContext buildContext) {
return SizedBox(
);
}
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!