Skip to content

Instantly share code, notes, and snippets.

@shahmirzali49
Last active October 2, 2023 14:27
Show Gist options
  • Save shahmirzali49/ec881375d09ae8f16d9eef7ce2e3d09a to your computer and use it in GitHub Desktop.
Save shahmirzali49/ec881375d09ae8f16d9eef7ce2e3d09a to your computer and use it in GitHub Desktop.
widget_to_image_and_share
import 'dart:developer';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:path_provider/path_provider.dart';
import 'package:quran_meal/core/utils/extension/context_extension.dart';
import 'package:share_plus/share_plus.dart';
enum Options { fullscreen, size1, size2, size3 }
Future<dynamic> shareImageDialog({required BuildContext context}) {
return showModalBottomSheet(
context: context,
constraints: BoxConstraints(
maxHeight: context.screenHeight * 0.9,
),
isScrollControlled: true,
enableDrag: true,
builder: (context) {
return HookBuilder(builder: (context) {
final popupMenuItemIndex = useState<int>(0);
final changedColorAccordingToMenuItem =
useState<double>(context.screenWidth / ((context.screenHeight * 0.9) - AppBar().preferredSize.height));
final GlobalKey globalKey = useMemoized(() => GlobalKey());
return Column(
children: [
AppBar(
backgroundColor: Colors.blue,
actions: [
IconButton(
onPressed: () async {
// ---------------------------------------------------
final box = context.findRenderObject() as RenderBox?;
// ---------------------------------------------------
RenderRepaintBoundary boundary =
globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List? pngBytes = byteData?.buffer.asUint8List();
final directory = (await getTemporaryDirectory()).path;
File imgFile = File('$directory/screenshot.png');
if (pngBytes != null) {
// log("pngBytes: $pngBytes");
await imgFile.writeAsBytes(pngBytes);
}
log("islem bitdi");
await Share.shareXFiles(
[
XFile(imgFile.path),
],
text: 'Great picture',
subject: 'Subject',
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);
},
icon: Icon(Icons.send)),
PopupMenuButton(
onSelected: (value) {
popupMenuItemIndex.value = value;
if (value == Options.size1.index) {
changedColorAccordingToMenuItem.value = 1;
} else if (value == Options.size2.index) {
changedColorAccordingToMenuItem.value = 4 / 3;
} else if (value == Options.size3.index) {
changedColorAccordingToMenuItem.value = 16 / 9;
} else {
changedColorAccordingToMenuItem.value =
context.screenWidth / ((context.screenHeight * 0.9) - AppBar().preferredSize.height);
}
},
offset: Offset(0.0, AppBar().preferredSize.height),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(8.0),
bottomRight: Radius.circular(8.0),
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
),
itemBuilder: (ctx) => [
_buildPopupMenuItem('Full screen size', Options.fullscreen.index),
_buildPopupMenuItem('1:1 size', Options.size1.index),
_buildPopupMenuItem('4:3 size', Options.size2.index),
_buildPopupMenuItem('16:9 size ', Options.size3.index),
],
)
],
),
Center(
child: AspectRatio(
aspectRatio: changedColorAccordingToMenuItem.value,
child: SingleChildScrollView(
child: RepaintBoundary(
key: globalKey,
child: Container(
decoration: BoxDecoration(
// gradient: LinearGradient(
// begin: Alignment.topLeft,
// end: Alignment.bottomRight,
// colors: [
// context.appColors.surahDetailInfoCardColors.surahDetailInfoCardGradientColor1,
// context.appColors.surahDetailInfoCardColors.surahDetailInfoCardGradientColor2,
// ],
// ),
image: DecorationImage(
image: AssetImage(
'assets/images/background_1.jpg',
),
fit: BoxFit.cover,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"يَٓا اَيُّهَا الَّذ۪ينَ اٰمَنُٓوا اِذَا تَدَايَنْتُمْ بِدَيْنٍ اِلٰٓى اَجَلٍ مُسَمًّى فَاكْتُبُوهُۜ وَلْيَكْتُبْ بَيْنَكُمْ كَاتِبٌ بِالْعَدْلِۖ وَلَا يَأْ لشَّهَادَةِ وَاَدْنٰٓى اَلَّا تَرْتَابُٓوا اِلَّٓا اَنْ تَكُونَ تِجَارَةً حَاضِرَةً تُد۪يرُونَهَا بَ للّٰهُۜ وَاللّٰهُ بِكُلِّ شَيْءٍ عَل۪يمٌ",
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 28.sp,
fontFamily: "Kitab",
color: Color.fromARGB(255, 75, 63, 49),
// height: 1.7,
),
),
10.verticalSpace,
Text(
"Ey iman edenler! Belli bir n daha sağlam, şüpheye düşmemeniz için daha elverişlidir. Yalnız, a inize bir günah yoktur. Alışveriş yaptığınız zaman da şahit tutun. Yazana da, şahide de bir zarar verilmesin.[79] Eğer aksini yaparsanız, bu sizin için günahkârca bir akının. Allah, size öğretiyor. Allah, her şeyi hakkıyla bilendir.",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontSize: 16.sp,
),
),
12.verticalSpace,
Text(
"Mâide Sûresi(5) 7. Ayet",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w500,
fontSize: 14.sp,
),
),
],
),
),
),
),
),
),
],
);
});
},
);
}
PopupMenuItem _buildPopupMenuItem(String title, int position) {
return PopupMenuItem(
value: position,
child: Text(title),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment