first commit
This commit is contained in:
103
lib/modules/preview/preview_screen.dart
Normal file
103
lib/modules/preview/preview_screen.dart
Normal file
@@ -0,0 +1,103 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:photobooth_mobile_app/core/image/image_cleanup_service.dart';
|
||||
import 'package:photobooth_mobile_app/core/image/image_compositor.dart';
|
||||
import 'package:photobooth_mobile_app/core/print/print_service.dart';
|
||||
|
||||
class PreviewScreen extends StatefulWidget {
|
||||
final String imagePath;
|
||||
final String frameAsset;
|
||||
final bool isFrontCamera;
|
||||
|
||||
const PreviewScreen({
|
||||
super.key,
|
||||
required this.imagePath,
|
||||
required this.frameAsset,
|
||||
required this.isFrontCamera,
|
||||
});
|
||||
|
||||
@override
|
||||
State<PreviewScreen> createState() => _PreviewScreenState();
|
||||
}
|
||||
|
||||
class _PreviewScreenState extends State<PreviewScreen> {
|
||||
final ImageCompositor _compositor = ImageCompositor();
|
||||
final PrintService _printer = PrintService();
|
||||
final ImageCleanupService _cleanup = ImageCleanupService();
|
||||
|
||||
late String _finalPath;
|
||||
String? _lastFinalPath;
|
||||
bool _loading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_init();
|
||||
}
|
||||
|
||||
Future<void> _init() async {
|
||||
final dir = await getTemporaryDirectory();
|
||||
final twibbonData = await rootBundle.load(widget.frameAsset);
|
||||
|
||||
await _cleanup.deleteIfExists(_lastFinalPath);
|
||||
|
||||
final newPath = await _compositor.composeFinalA5(
|
||||
imagePath: widget.imagePath,
|
||||
twibbonBytes: twibbonData.buffer.asUint8List(),
|
||||
tempDir: dir.path,
|
||||
isFrontCamera: widget.isFrontCamera,
|
||||
);
|
||||
|
||||
_lastFinalPath = newPath;
|
||||
_finalPath = newPath;
|
||||
|
||||
await _cleanup.keepLastN(tempDir: dir.path, maxFiles: 10);
|
||||
|
||||
if (mounted) {
|
||||
setState(() => _loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_cleanup.deleteIfExists(_finalPath);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Pratinjau')),
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _loading
|
||||
? const Center(child: CupertinoActivityIndicator())
|
||||
: Center(
|
||||
child: AspectRatio(
|
||||
aspectRatio: 148 / 210,
|
||||
child: Image.file(File(_finalPath), fit: BoxFit.contain),
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: ElevatedButton.icon(
|
||||
icon: const Icon(Icons.print),
|
||||
label: Text(_loading ? 'Menyiapkan...' : 'Cetak'),
|
||||
onPressed: _loading
|
||||
? null
|
||||
: () async {
|
||||
final pdfPath = await _printer.generateA5Pdf(_finalPath);
|
||||
await _printer.sharePdf(pdfPath);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user