Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.9 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:image_picker/image_picker.dart';
  3. class FilePicker extends StatelessWidget {
  4. FilePicker({
  5. Key? key,
  6. this.cameraHandle,
  7. this.galleryHandleSingle,
  8. this.galleryHandleMultiple,
  9. this.fileHandle,
  10. }) : super(key: key);
  11. final Function(XFile image)? cameraHandle;
  12. final Function(XFile image)? galleryHandleSingle;
  13. final Function(List<XFile> images)? galleryHandleMultiple;
  14. // TODO: Implement. Perhaps after first release?
  15. final Function()? fileHandle;
  16. final ImagePicker _picker = ImagePicker();
  17. @override
  18. Widget build(BuildContext context) {
  19. return Padding(
  20. padding: const EdgeInsets.only(top: 10, bottom: 10, left: 5, right: 5),
  21. child: Row(
  22. mainAxisAlignment: MainAxisAlignment.center,
  23. children: [
  24. _filePickerSelection(
  25. hasHandle: cameraHandle != null,
  26. icon: Icons.camera_alt,
  27. onTap: () async {
  28. final XFile? image = await _picker.pickImage(source: ImageSource.camera);
  29. if (image == null) {
  30. return;
  31. }
  32. cameraHandle!(image);
  33. },
  34. context: context,
  35. ),
  36. _filePickerSelection(
  37. hasHandle: galleryHandleSingle != null,
  38. icon: Icons.image,
  39. onTap: () async {
  40. final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
  41. if (image == null) {
  42. return;
  43. }
  44. galleryHandleSingle!(image);
  45. },
  46. context: context,
  47. ),
  48. _filePickerSelection(
  49. hasHandle: galleryHandleMultiple != null,
  50. icon: Icons.image,
  51. onTap: () async {
  52. final List<XFile>? images = await _picker.pickMultiImage();
  53. if (images == null) {
  54. return;
  55. }
  56. galleryHandleMultiple!(images);
  57. },
  58. context: context,
  59. ),
  60. _filePickerSelection(
  61. hasHandle: fileHandle != null,
  62. icon: Icons.file_present_sharp,
  63. onTap: () {
  64. },
  65. context: context,
  66. ),
  67. ],
  68. )
  69. );
  70. }
  71. Widget _filePickerSelection({
  72. required bool hasHandle,
  73. required IconData icon,
  74. required Function() onTap,
  75. required BuildContext context
  76. }) {
  77. if (!hasHandle) {
  78. return const SizedBox.shrink();
  79. }
  80. return Padding(
  81. padding: const EdgeInsets.only(left: 5, right: 5),
  82. child: GestureDetector(
  83. onTap: onTap,
  84. child: Container(
  85. height: 75,
  86. width: 75,
  87. decoration: BoxDecoration(
  88. color: Theme.of(context).primaryColor,
  89. borderRadius: BorderRadius.circular(25),
  90. ),
  91. child: Icon(
  92. icon,
  93. size: 40,
  94. color: Colors.white,
  95. ),
  96. ),
  97. ),
  98. );
  99. }
  100. }