Files
stadtbote-app/lib/popups/tasks_popups/take_photos_popup.dart
T
2025-10-23 11:28:03 +02:00

150 lines
6.5 KiB
Dart

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as image_lib;
import 'package:stadtbote/model/task_info.dart';
import 'package:stadtbote/tools/util.dart';
Future<void> showTakePhotosPopup(BuildContext context, TaskInfo taskInfo, Function(List<Uint8List>) onFinished) async {
List<Uint8List> imageDataList = [];
List<Widget> imageWidgetsList = [];
int currentCarousselSliderIndex = 0;
ValueNotifier<List<Widget>> imageWidgetsListNotifier = ValueNotifier<List<Widget>>([]);
await showDialog(
context: context,
builder: (BuildContext context) {
return PopScope(
canPop: false,
onPopInvokedWithResult: (bool didPop, dynamic result) async {
if (!didPop) {
final shouldPop = await taskDialogSecurityException(context);
if (shouldPop && context.mounted) {
Navigator.of(context).pop();
}
}
},
child: AlertDialog(
contentPadding: EdgeInsets.zero,
shadowColor: Colors.transparent,
backgroundColor: Colors.white,
content: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
void capturePhoto() async {
try {
var photo = await Util.capturePhoto();
if (photo != null) {
var fullsizeImageData = await photo.readAsBytes();
image_lib.Image? image = image_lib.decodeImage(fullsizeImageData);
if (image != null) {
var thumbnail = image_lib.copyResize(image, width: 800);
var resizedImageData = image_lib.encodeJpg(thumbnail, quality: 75);
photo.delete();
imageDataList.add(resizedImageData);
imageWidgetsList.add(GestureDetector(
onTap: () {},
child: GestureDetector(
onTap: () async {
await showQuestionDialog(context, "Achtung", "Wollen Sie das Bild wirklich löschen?", [
TextButton(onPressed: () => {Navigator.of(context).pop(),
imageDataList.removeAt(currentCarousselSliderIndex),
imageWidgetsList.removeAt(currentCarousselSliderIndex),
Util.logInfo("image deleted $currentCarousselSliderIndex"),
currentCarousselSliderIndex = 0,
setState(() {})
}, child: const Text('Ja')),
TextButton(onPressed: () => {Navigator.of(context).pop()}, child: const Text('Nein')),
]);
},
child: Image.memory(resizedImageData),
)
));
imageWidgetsListNotifier.value = imageWidgetsList;
setState(() {});
}
}
} catch (ex) {
Util.logException(ex.toString());
}
}
return SizedBox(
key: GlobalKey(),
height: 545,
width: MediaQuery.of(context).size.width * 0.8,
child: Container(
height: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 0.5,
),
),
child: Padding(
padding: const EdgeInsets.all(35.0),
child: Column(
children: [
const Text('Aufgenommene Fotos'),
const SizedBox(height: 20),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
border: Border.all(
color: Colors.grey,
width: 0.5,
),
),
height: 300,
child: ValueListenableBuilder<List<Widget>>(
valueListenable: imageWidgetsListNotifier,
builder: (context, value, child) {
return CarouselSlider(
options: CarouselOptions(
disableCenter: false,
enableInfiniteScroll: false,
onPageChanged: (index, reason) {
currentCarousselSliderIndex = index;
},
),
items: imageWidgetsList.toList());
},
),
),
const SizedBox(height: 20),
Visibility(
visible: imageDataList.length < (taskInfo.task?.max ?? 999),
child: getDefaultButton('Hinzufügen', buttonHeight: 50, () => {capturePhoto()})),
const SizedBox(height: 12),
getDefaultButton(
'Übernehmen',
buttonHeight: 50,
() => {
if (imageDataList.length >= (taskInfo.task?.min ?? 0) && imageDataList.length <= (taskInfo.task?.max ?? 999))
{Navigator.of(context).pop(), onFinished(imageDataList)}
else
{showAlertDialog('Fehler', 'Es müssen mindestens ${taskInfo.task?.min ?? 0} Foto(s) aufgenommen werden')}
})
],
),
),
));
})));
});
}