2024-07-07 10:53:33 +02:00
import ' package:flutter/material.dart ' ;
import ' dart:io ' ;
2024-07-27 18:53:02 +02:00
import ' ../classes/addEventImage.dart ' ;
2024-07-17 23:39:51 +02:00
import ' ../classes/alert.dart ' ;
2024-07-17 22:49:47 +02:00
import ' package:flutter_dotenv/flutter_dotenv.dart ' ;
2024-07-07 10:53:33 +02:00
import ' package:flutter_gemini/flutter_gemini.dart ' ;
2024-07-25 21:49:52 +02:00
import ' package:shared_preferences/shared_preferences.dart ' ;
import ' package:http/http.dart ' as http ;
2024-07-27 19:40:02 +02:00
import " ItemMenu.dart " ;
2024-07-30 23:19:37 +02:00
import ' UpdateEventImage.dart ' ;
2024-07-07 10:53:33 +02:00
2024-07-25 00:18:00 +02:00
import ' dart:convert ' ;
2024-07-25 21:49:52 +02:00
import ' ../variable/globals.dart ' as globals ;
2025-01-10 21:06:41 +01:00
import ' ../classes/MyDrawer.dart ' ;
2024-07-25 21:49:52 +02:00
2024-07-07 10:53:33 +02:00
void main ( ) {
runApp ( MyApp ( ) ) ;
}
class MyApp extends StatelessWidget {
const MyApp ( { Key ? key } ) : super ( key: key ) ;
// This widget is the root of your application.
@ override
Widget build ( BuildContext context ) {
return MaterialApp (
title: ' Flutter Demo ' ,
theme: ThemeData (
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors . blue ,
) ,
) ;
}
}
2024-07-08 23:01:07 +02:00
// A screen that allows users to take a picture using a given camera.
class DisplayPictureScreen extends StatefulWidget {
2024-07-07 10:53:33 +02:00
final String imagePath ;
const DisplayPictureScreen ( { super . key , required this . imagePath } ) ;
2024-07-08 23:01:07 +02:00
@ override
DisplayPictureScreenState createState ( ) = > DisplayPictureScreenState ( ) ;
}
// A widget that displays the picture taken by the user.
2024-07-17 23:39:51 +02:00
class DisplayPictureScreenState extends State < DisplayPictureScreen >
2024-12-30 22:34:17 +01:00
with ShowDescImageAdd , ShowAlertDialog , TickerProviderStateMixin {
2024-08-14 22:11:38 +02:00
late AnimationController controller ;
2024-07-10 23:31:25 +02:00
@ override
void initState ( ) {
2024-08-14 22:11:38 +02:00
controller = AnimationController (
/// [AnimationController]s can be created with `vsync: this` because of
/// [TickerProviderStateMixin].
vsync: this ,
duration: const Duration ( seconds: 5 ) ,
) . . addListener ( ( ) {
setState ( ( ) { } ) ;
} ) ;
controller . repeat ( reverse: false ) ;
2024-07-10 23:31:25 +02:00
super . initState ( ) ;
_getEventInfosFromImage ( ) ;
}
2024-08-14 22:11:38 +02:00
@ override
void dispose ( ) {
controller . dispose ( ) ;
super . dispose ( ) ;
}
2024-07-22 23:11:45 +02:00
Future < void > displayError ( String e ) async {
print ( " problem gemini : ${ e } " ) ;
2024-12-30 22:34:17 +01:00
showAlertDialog ( context , ' Error IA ' ,
2024-07-22 23:11:45 +02:00
" L'IA de Google n'a pas su analyser l'image. Recommecer avec une autre " ) ;
}
2024-09-01 22:27:18 +02:00
Future < void > searchEvents ( String json , String imagePath ) async {
2024-11-07 23:10:03 +01:00
print ( json . replaceAll ( " '''json " , ' ' ) . replaceAll ( " ''' " , " " ) ) ;
2024-07-25 21:49:52 +02:00
SharedPreferences prefs = await SharedPreferences . getInstance ( ) ;
2024-11-08 16:57:07 +01:00
try {
Map < String , dynamic > jsonData =
jsonDecode ( json . replaceAll ( " ```json " , ' ' ) . replaceAll ( " ``` " , " " ) ) ;
print ( " json : ${ jsonData } " ) ;
var name = jsonData [ " name " ] ;
print ( " name : ${ name } " ) ;
var place = jsonData [ " place " ] ;
2024-12-01 21:23:22 +01:00
var date = jsonData [ " start_date " ] ;
2024-11-08 16:57:07 +01:00
var accessToken = prefs . getString ( " access_token " ) ? ? " " ;
if ( accessToken . isNotEmpty ) {
2024-12-01 21:23:22 +01:00
var urlGet = Uri . parse (
" ${ globals . api } /events/search?item= ${ name } &date_event= ${ date } " ) ;
2024-11-08 16:57:07 +01:00
var responseGet = await http . get ( urlGet ,
headers: { HttpHeaders . cookieHeader: ' access_token= ${ accessToken } ' } ) ;
if ( responseGet . statusCode = = 200 ) {
var events = jsonDecode ( utf8 . decode ( responseGet . bodyBytes ) ) ;
print ( " reponse http : ${ events . length } " ) ;
if ( events . length = = 0 ) {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( _ ) = > UpdateeventImage (
events: jsonData , imagePath: imagePath ) ) ) ;
} else {
Navigator . push (
context ,
MaterialPageRoute (
builder: ( _ ) = > ItemMenu ( title: events [ 0 ] [ " id " ] ) ) ) ;
}
2024-12-01 21:23:22 +01:00
} else {
2024-12-30 22:34:17 +01:00
showAlertDialog ( context , ' Erreur de reponse ' ,
2024-12-01 21:23:22 +01:00
" response status code update : ${ responseGet . statusCode } " ) ;
2024-07-25 21:49:52 +02:00
}
2024-11-08 16:57:07 +01:00
} else {
2024-12-30 22:34:17 +01:00
showAlertDialog ( context , " Erreur de reponse " , " Erreur de token " ) ;
2024-07-25 21:49:52 +02:00
}
2024-11-08 16:57:07 +01:00
} catch ( e ) {
2024-12-30 22:34:17 +01:00
showAlertDialog (
context , " Erreur IA " , " Erreur de format de donnée fourni par l'IA " ) ;
2024-07-25 21:49:52 +02:00
}
//showDescImageAddDialog(context, message);
2024-07-24 23:17:11 +02:00
}
2024-07-10 23:31:25 +02:00
Future < void > _getEventInfosFromImage ( ) async {
2024-07-17 22:49:47 +02:00
await dotenv . load ( ) ;
final gemini = Gemini . init (
apiKey: dotenv . env [ ' GEMINI_API_KEY ' ] ! , enableDebugging: true ) ;
2024-07-10 23:31:25 +02:00
final file = File ( widget . imagePath ) ;
gemini
. textAndImage (
2024-07-22 23:11:45 +02:00
text:
2024-11-08 17:07:40 +01:00
" Peux-tu donner le nom, la date avec l'année actuelle ou d'une année future proche et le lieu de l'évènement sous format JSON (sans le caratère json au début de la chaine de caractère) avec les valeurs suivantes : name, place, description, tags (tableau sans espace), organizers (tableau), start_date et end_date (si le end_date est vide, alors donnez une valeur de six de plus par rapport à start_date) sous le format en YYYY-MM-DD HH:mm:ssZ " ,
2024-07-20 20:22:57 +02:00
images: [ file . readAsBytesSync ( ) ] ,
2024-08-18 19:14:01 +02:00
modelName: " models/gemini-1.5-pro-latest " )
2024-09-01 22:27:18 +02:00
. then ( ( value ) = > searchEvents (
value ? . content ? . parts ? . last . text ? ? ' ' , widget . imagePath ) )
2024-07-22 23:11:45 +02:00
. catchError ( ( e ) = > displayError ) ;
2024-07-10 23:31:25 +02:00
}
2024-07-07 10:53:33 +02:00
@ override
Widget build ( BuildContext context ) {
return Scaffold (
2024-07-08 23:37:27 +02:00
appBar: AppBar ( title: const Text ( ' Display the Picture ' ) ) ,
// The image is stored as a file on the device. Use the `Image.file`
// constructor with the given path to display the image.
2025-01-10 21:06:41 +01:00
drawer: MyDrawer ( ) ,
2024-08-14 22:11:38 +02:00
body: Padding (
padding: const EdgeInsets . all ( 20.0 ) ,
child: Column (
mainAxisAlignment: MainAxisAlignment . spaceEvenly ,
children: < Widget > [
Text (
' Analyse de l \' image en cours ' ,
style: Theme . of ( context ) . textTheme . titleLarge ,
) ,
CircularProgressIndicator (
value: controller . value ,
semanticsLabel: ' Loading progress ' ,
) ,
] ) ) ) ;
2024-07-07 10:53:33 +02:00
}
}