Browse Source

添加保存分享统计图

weicu
tanlongsheng 1 year ago
parent
commit
d344486644
  1. 84
      lib/modules/meeting/views/bottomSheet/statistics_bottom_sheet.dart
  2. 2
      pubspec.yaml

84
lib/modules/meeting/views/bottomSheet/statistics_bottom_sheet.dart

@ -1,8 +1,15 @@
import 'dart:io'; import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:image_gallery_saver_plus/image_gallery_saver_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import '../../../profile/controllers/profile_controller.dart'; import '../../../profile/controllers/profile_controller.dart';
import '../../controllers/meeting_mine_controller.dart'; import '../../controllers/meeting_mine_controller.dart';
@ -18,6 +25,8 @@ class _StatisticsBottomSheetState extends State<StatisticsBottomSheet> {
final _mineController = Get.find<MeetingMineController>(); final _mineController = Get.find<MeetingMineController>();
final _profileCtrl = Get.find<ProfileController>(); final _profileCtrl = Get.find<ProfileController>();
final GlobalKey _posterKey = GlobalKey();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Stack( return Stack(
@ -30,12 +39,16 @@ class _StatisticsBottomSheetState extends State<StatisticsBottomSheet> {
height: 1.sh, height: 1.sh,
), ),
), ),
Column( Padding(
padding: EdgeInsets.symmetric(horizontal: 10.w),
child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Container( RepaintBoundary(
padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 20.w), key: _posterKey,
margin: EdgeInsets.symmetric(horizontal: 10.w), child: Container(
padding:
EdgeInsets.symmetric(horizontal: 15.w, vertical: 20.w),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white, color: Colors.white,
borderRadius: BorderRadius.circular(16.w), borderRadius: BorderRadius.circular(16.w),
@ -56,10 +69,12 @@ class _StatisticsBottomSheetState extends State<StatisticsBottomSheet> {
), ),
child: CircleAvatar( child: CircleAvatar(
radius: 25.w, radius: 25.w,
backgroundImage: _profileCtrl.localAvatar.value.isNotEmpty backgroundImage: _profileCtrl
.localAvatar.value.isNotEmpty
? FileImage(File(_profileCtrl.localAvatar.value)) ? FileImage(File(_profileCtrl.localAvatar.value))
: (_profileCtrl.userInfo.value.avatar.isNotEmpty : (_profileCtrl.userInfo.value.avatar.isNotEmpty
? NetworkImage(_profileCtrl.userInfo.value.avatar) ? NetworkImage(
_profileCtrl.userInfo.value.avatar)
: null) as ImageProvider<Object>?, : null) as ImageProvider<Object>?,
backgroundColor: Colors.grey[200], backgroundColor: Colors.grey[200],
child: (_profileCtrl.localAvatar.value.isEmpty && child: (_profileCtrl.localAvatar.value.isEmpty &&
@ -180,7 +195,8 @@ class _StatisticsBottomSheetState extends State<StatisticsBottomSheet> {
child: GridView.builder( child: GridView.builder(
shrinkWrap: true, shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 52, crossAxisCount: 52,
mainAxisSpacing: 1.w, mainAxisSpacing: 1.w,
crossAxisSpacing: 1.w, crossAxisSpacing: 1.w,
@ -194,7 +210,8 @@ class _StatisticsBottomSheetState extends State<StatisticsBottomSheet> {
int type = 0; int type = 0;
if (_mineController.statistics if (_mineController.statistics
.containsKey(vIndex + 1)) { .containsKey(vIndex + 1)) {
if (_mineController.statistics[vIndex + 1]['count'] <= if (_mineController.statistics[vIndex + 1]
['count'] <=
_mineController.averageNumberFiles) { _mineController.averageNumberFiles) {
type = 1; type = 1;
} else { } else {
@ -237,19 +254,49 @@ class _StatisticsBottomSheetState extends State<StatisticsBottomSheet> {
], ],
), ),
), ),
),
SizedBox(height: 10.w), SizedBox(height: 10.w),
Padding( Row(
padding: EdgeInsets.symmetric(horizontal: 10.w),
child: Row(
children: [ children: [
_button(Icons.share, '分享图片', Colors.deepOrange, () {}), _button(Icons.share, '分享图片', Colors.deepOrange, () async {
EasyLoading.show();
Uint8List pngBytes = await _generate();
final tempDir = await getTemporaryDirectory();
final String fileName =
'share_${DateTime.now().millisecondsSinceEpoch}.png';
final file = File('${tempDir.path}/$fileName');
await file.writeAsBytes(pngBytes);
final result = await SharePlus.instance.share(ShareParams(
text: '分享 DEAPSOUND',
files: [XFile(file.path)],
));
if (result.status == ShareResultStatus.success) {
Get.back();
EasyLoading.showToast('分享完成!');
}
EasyLoading.dismiss();
}),
10.horizontalSpace, 10.horizontalSpace,
_button(Icons.download, '保存图片', Colors.cyan, () {}), _button(Icons.download, '保存图片', Colors.cyan, () async {
EasyLoading.show();
Uint8List pngBytes = await _generate();
final result = await ImageGallerySaverPlus.saveImage(
pngBytes,
quality: 100,
);
if (result['isSuccess']) {
Get.back();
EasyLoading.showToast('保存成功');
} else {
EasyLoading.showToast('保存失败');
}
EasyLoading.dismiss();
}),
], ],
), ),
),
], ],
), ),
),
], ],
); );
} }
@ -324,4 +371,13 @@ class _StatisticsBottomSheetState extends State<StatisticsBottomSheet> {
), ),
); );
} }
Future<Uint8List> _generate() async {
RenderRepaintBoundary boundary =
_posterKey.currentContext!.findRenderObject()! as RenderRepaintBoundary;
ui.Image image = await boundary.toImage(pixelRatio: 6.0);
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData!.buffer.asUint8List();
return pngBytes;
}
} }

2
pubspec.yaml

@ -69,7 +69,7 @@ dependencies:
flutter_facebook_auth: ^7.1.1 flutter_facebook_auth: ^7.1.1
flutter_image_compress: ^2.4.0 flutter_image_compress: ^2.4.0
common_utils: ^2.1.0 common_utils: ^2.1.0
# image_cropper: ^9.0.0 image_gallery_saver_plus: ^4.0.1
dio: ^5.8.0+1 dio: ^5.8.0+1
sqflite: ^2.4.2 sqflite: ^2.4.2
path: ^1.9.1 path: ^1.9.1

Loading…
Cancel
Save