photo_chat.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import 'dart:convert';
  2. import 'package:camera/camera.dart';
  3. import 'package:easy_localization/easy_localization.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:image_picker/image_picker.dart';
  7. import 'package:page_transition/page_transition.dart';
  8. import 'package:photo_view/photo_view.dart';
  9. import 'package:telnow_mobile_new/src/api/api_auth_provider.dart';
  10. import 'package:telnow_mobile_new/src/layouts/components/camera.dart';
  11. import 'package:telnow_mobile_new/src/utils/U.dart';
  12. import 'package:image/image.dart' as img;
  13. import 'package:telnow_mobile_new/src/utils/ui_service.dart';
  14. class PhotoChat extends StatefulWidget {
  15. final Map<String, dynamic> data;
  16. final ImageSource? media;
  17. final bool? isBroadcast;
  18. final Uint8List? image;
  19. const PhotoChat(this.data, this.media, this.isBroadcast, this.image, {super.key});
  20. @override
  21. PhotoChatState createState() => PhotoChatState();
  22. }
  23. class PhotoChatState extends State<PhotoChat> {
  24. final ApiAuthProvider apiAuthProvider = ApiAuthProvider();
  25. TextEditingController controllerPesan = TextEditingController();
  26. Uint8List? image;
  27. Future getImage() async {
  28. XFile? pickedFile;
  29. if(widget.media == ImageSource.camera){
  30. await availableCameras().then((camera) => Navigator.push(UIService.navigatorKey.currentContext!, PageTransition(type: PageTransitionType.rightToLeft, child: OpenCamera(cameras: camera))).then((value) async{
  31. if(value!=null){
  32. pickedFile = value;
  33. }
  34. }));
  35. }
  36. else{
  37. pickedFile = await ImagePicker().pickImage(source: ImageSource.gallery);
  38. }
  39. if (pickedFile != null) {
  40. var images = img.decodeImage(await pickedFile!.readAsBytes());
  41. var imgPercent = (1000 / (images!.width / 100)).toDouble();
  42. if (images.width > 1000) {
  43. images = img.copyResize(images, width: ((images.width / 100) * imgPercent).toInt(), height: ((images.height / 100) * imgPercent).toInt());
  44. }
  45. var compressed = img.encodeJpg(images, quality: 60);
  46. setState(() => image = compressed);
  47. } else {
  48. UIService.navigatorKey.currentState?.pop(null);
  49. // Navigator.of(context).pop(null);
  50. }
  51. }
  52. @override
  53. void initState() {
  54. if (widget.image == null) {
  55. getImage();
  56. } else {
  57. image = widget.image!;
  58. }
  59. super.initState();
  60. }
  61. @override
  62. Widget build(BuildContext context) {
  63. return Scaffold(
  64. backgroundColor: Colors.black,
  65. appBar: AppBar(
  66. elevation: 0,
  67. bottomOpacity: 0,
  68. titleSpacing: 0,
  69. title: Text('sendPicture'.tr(), style: TextStyle(color: Colors.white, fontSize: 17, fontWeight: FontWeight.w500), overflow: TextOverflow.ellipsis),
  70. backgroundColor: Colors.black.withValues(alpha: 0.4),
  71. leading: GestureDetector(
  72. child: U.iconsax('arrow-left', color: Colors.white),
  73. onTap: () => Navigator.of(context).pop(),
  74. ),
  75. ),
  76. body: Center(
  77. child: SizedBox(
  78. width: U.bodyWidth(context),
  79. child: Stack(
  80. children: [
  81. Container(
  82. child: image != null ? PhotoView(
  83. maxScale: 5.0,
  84. minScale: 0.3,
  85. imageProvider: MemoryImage(kIsWeb ? widget.image! : image!),
  86. )
  87. : Container()),
  88. Container(
  89. alignment: Alignment.bottomCenter,
  90. child: Container(
  91. padding: const EdgeInsets.all(10),
  92. color: Colors.black54,
  93. child: Row(
  94. crossAxisAlignment: CrossAxisAlignment.end,
  95. children: [
  96. Expanded(
  97. child: Padding(
  98. padding: const EdgeInsets.only(right: 5),
  99. child: TextField(
  100. controller: controllerPesan,
  101. maxLength: 256,
  102. buildCounter: (BuildContext? context, {int? currentLength, int? maxLength, bool? isFocused}) => null,
  103. style: TextStyle(color: Colors.white, fontSize: 15),
  104. keyboardType: TextInputType.multiline,
  105. minLines: 1,
  106. maxLines: 5,
  107. decoration: InputDecoration(
  108. hintText: 'writeMessage'.tr(),
  109. hintStyle: TextStyle(fontSize: 15, color: Colors.white54),
  110. contentPadding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
  111. border: InputBorder.none,
  112. )),
  113. ),
  114. ),
  115. Expanded(
  116. flex: 0,
  117. child: SizedBox(
  118. width: 50,
  119. height: 50,
  120. child: ElevatedButton(
  121. style: ButtonStyle(
  122. backgroundColor: WidgetStateProperty.all<Color>(primaryColor),
  123. shape: WidgetStateProperty.all<RoundedRectangleBorder>(RoundedRectangleBorder(
  124. borderRadius: BorderRadius.circular(50),
  125. )),
  126. elevation: WidgetStateProperty.all(2),
  127. padding: WidgetStateProperty.all(const EdgeInsets.only(left: 1)),
  128. ),
  129. child: Image.asset('assets/image/icon/Send.png', width: 25, height: 25),
  130. onPressed: () async {
  131. var data = widget.data;
  132. data['text'] = controllerPesan.text.trim();
  133. data['images'] = kIsWeb ? base64Encode(widget.image!) : base64Encode(image!);
  134. Navigator.of(context).pop(data);
  135. },
  136. )),
  137. )
  138. ],
  139. ),
  140. ),
  141. )
  142. ],
  143. ),
  144. ),
  145. ),
  146. );
  147. }
  148. }