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.
 
 
 
 
 
 

325 lines
9.9 KiB

import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:file_picker/file_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import '../Components/appDrawer.dart';
import '../Components/app_bar.dart';
import '../Models/produit.dart';
import '../Services/productDatabase.dart';
class AddProductPage extends StatefulWidget {
const AddProductPage({super.key});
@override
_AddProductPageState createState() => _AddProductPageState();
}
class _AddProductPageState extends State<AddProductPage> {
// Controllers for text fields
final TextEditingController _nameController = TextEditingController();
final TextEditingController _priceController = TextEditingController();
final TextEditingController _imageController = TextEditingController();
final TextEditingController _descriptionController = TextEditingController();
String? _qrData; // Variable to store QR code data
final List<String> _categories = ['Sucré', 'Salé', 'Jus', 'Gateaux']; // List of product categories
String? _selectedCategory; // Selected category
File? _pickedImage; // Variable to store the selected image file
late ProductDatabase _productDatabase; // Database instance
@override
void initState() {
super.initState();
_productDatabase = ProductDatabase.instance;
_productDatabase.initDatabase();
_nameController.addListener(_updateQrData);
}
@override
void dispose() {
_nameController.removeListener(_updateQrData);
_nameController.dispose();
super.dispose();
}
// Function to select an image from files or drop
void _selectImage() async {
final action = await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Sélectionner une image'),
content: const Text('Choisissez comment sélectionner une image'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop('pick');
},
child: const Text('Choisir depuis les fichiers'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop('drop');
},
child: const Text('Déposer une image'),
),
],
);
},
);
if (action == 'pick') {
final result = await FilePicker.platform.pickFiles(
type: FileType.image,
);
if (result != null) {
setState(() {
_pickedImage = File(result.files.single.path!);
_imageController.text = _pickedImage!.path;
});
}
} else if (action == 'drop') {
// Code to handle image drop
}
}
// Function to update QR data based on product name
void _updateQrData() {
if (_nameController.text.isNotEmpty) {
final reference = 'PROD_PREVIEW_${_nameController.text}_${DateTime.now().millisecondsSinceEpoch}';
setState(() {
_qrData = 'https://tonsite.com/$reference';
});
}
}
// Function to get the database location
Future<void> _getDatabaseLocation() async {
final directory = await getApplicationDocumentsDirectory();
final dbPath = directory.path;
print('Emplacement de la base de données : $dbPath');
}
// Function to generate and save QR code
Future<String> _generateAndSaveQRCode(String reference) async {
final qrValidationResult = QrValidator.validate(
data: 'https://tonsite.com/$reference',
version: QrVersions.auto,
errorCorrectionLevel: QrErrorCorrectLevel.L,
);
final qrCode = qrValidationResult.qrCode;
final painter = QrPainter.withQr(
qr: qrCode!,
color: Colors.black,
emptyColor: Colors.white,
gapless: true,
);
final tempDir = await getApplicationDocumentsDirectory();
final file = File('${tempDir.path}/$reference.png');
final picData = await painter.toImageData(2048, format: ImageByteFormat.png);
await file.writeAsBytes(picData!.buffer.asUint8List());
return file.path;
}
// Function to add a product to the database
void _addProduct() async {
final name = _nameController.text;
final price = double.tryParse(_priceController.text) ?? 0.0;
final image = _imageController.text;
final category = _selectedCategory;
final description = _descriptionController.text;
if (name.isNotEmpty && price > 0 && image.isNotEmpty && category != null) {
final reference = 'PROD_${DateTime.now().millisecondsSinceEpoch}';
final qrPath = await _generateAndSaveQRCode(reference);
final product = Product(
name: name,
price: price,
image: image,
category: category,
description: description,
qrCode: qrPath,
reference: reference,
);
_productDatabase.createProduct(product).then((_) {
Get.snackbar('Succès', 'Produit ajouté avec succès');
setState(() {
_nameController.clear();
_priceController.clear();
_imageController.clear();
_descriptionController.clear();
_selectedCategory = null;
_pickedImage = null;
});
}).catchError((error) {
Get.snackbar('Erreur', 'Impossible d\'ajouter le produit : $error');
});
} else {
Get.snackbar(
'Saisie invalide',
'Veuillez entrer tous les champs requis.',
);
}
}
// Function to display the selected image
Widget _displayImage() {
if (_pickedImage != null) {
return ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.file(
_pickedImage!,
width: 100,
height: 100,
fit: BoxFit.cover,
),
);
} else {
return Stack(
alignment: Alignment.center,
children: [
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(8.0),
),
),
Icon(
Icons.image,
size: 48,
color: Colors.grey[400],
),
Positioned(
bottom: 4,
child: Text(
'Aucune image',
style: TextStyle(
fontSize: 12,
color: Colors.grey[400],
),
),
),
],
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CustomAppBar(title: 'Ajouter un produit'),
drawer: CustomDrawer(),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Ajouter un produit',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 16),
TextField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Nom du produit',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
TextField(
controller: _priceController,
decoration: const InputDecoration(
labelText: 'Prix',
border: OutlineInputBorder(),
),
keyboardType: const TextInputType.numberWithOptions(decimal: true),
),
const SizedBox(height: 16),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: TextField(
controller: _imageController,
decoration: const InputDecoration(
labelText: 'Image',
border: OutlineInputBorder(),
),
),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: _selectImage,
child: const Text('Sélectionner une image'),
),
],
),
const SizedBox(height: 16),
_displayImage(),
const SizedBox(height: 16),
DropdownButtonFormField<String>(
value: _selectedCategory,
onChanged: (newValue) {
setState(() {
_selectedCategory = newValue;
});
},
decoration: const InputDecoration(
labelText: 'Catégorie',
border: OutlineInputBorder(),
),
items: _categories.map((category) {
return DropdownMenuItem<String>(
value: category,
child: Text(category),
);
}).toList(),
),
const SizedBox(height: 16),
if (_qrData != null)
Column(
children: [
const SizedBox(height: 16),
const Text('Aperçu du QR Code :'),
QrImageView(
data: _qrData!,
version: QrVersions.auto,
size: 120.0,
),
],
),
const SizedBox(height: 16),
TextField(
controller: _descriptionController,
decoration: const InputDecoration(
labelText: 'Description',
border: OutlineInputBorder(),
),
maxLines: 3,
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _addProduct,
child: const Text('Ajouter le produit'),
),
],
),
),
);
}
}