165 lines
5.4 KiB
Dart
165 lines
5.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'dart:io';
|
|
import '../classes/addEventImage.dart';
|
|
import '../classes/alert.dart';
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
import 'package:flutter_gemini/flutter_gemini.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import "ItemMenu.dart";
|
|
import 'UpdateEventImage.dart';
|
|
|
|
import 'dart:convert';
|
|
|
|
import '../variable/globals.dart' as globals;
|
|
|
|
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,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
// A screen that allows users to take a picture using a given camera.
|
|
class DisplayPictureScreen extends StatefulWidget {
|
|
final String imagePath;
|
|
|
|
const DisplayPictureScreen({super.key, required this.imagePath});
|
|
|
|
@override
|
|
DisplayPictureScreenState createState() => DisplayPictureScreenState();
|
|
}
|
|
|
|
// A widget that displays the picture taken by the user.
|
|
class DisplayPictureScreenState extends State<DisplayPictureScreen>
|
|
with ShowDescImageAdd, ShowErrorDialog, TickerProviderStateMixin {
|
|
late AnimationController controller;
|
|
@override
|
|
void initState() {
|
|
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);
|
|
super.initState();
|
|
|
|
_getEventInfosFromImage();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> displayError(String e) async {
|
|
print("problem gemini : ${e}");
|
|
showErrorDialog(context,
|
|
"L'IA de Google n'a pas su analyser l'image. Recommecer avec une autre");
|
|
}
|
|
|
|
Future<void> searchEvents(String json, String imagePath) async {
|
|
print(json);
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
Map<String, dynamic> jsonData = jsonDecode(json);
|
|
var name = jsonData["name"];
|
|
var place = jsonData["place"];
|
|
var accessToken = prefs.getString("access_token") ?? "";
|
|
|
|
if (accessToken.isNotEmpty) {
|
|
var urlGet = Uri.parse("${globals.api}/events?name=${name}");
|
|
|
|
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"])));
|
|
}
|
|
}
|
|
} else {
|
|
showErrorDialog(context, "Erreur de token");
|
|
}
|
|
|
|
//showDescImageAddDialog(context, message);
|
|
}
|
|
|
|
Future<void> _getEventInfosFromImage() async {
|
|
await dotenv.load();
|
|
|
|
final gemini = Gemini.init(
|
|
apiKey: dotenv.env['GEMINI_API_KEY']!, enableDebugging: true);
|
|
|
|
final file = File(widget.imagePath);
|
|
|
|
gemini
|
|
.textAndImage(
|
|
text:
|
|
"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 avec les valeurs suivantes : name, address, city, zip_code, country, date sous le format en YYYY-MM-DD HH:mm:ssZ, et sans la présence du mot json dans la chaîne de caractère",
|
|
images: [file.readAsBytesSync()],
|
|
modelName: "models/gemini-1.5-pro-latest")
|
|
.then((value) => searchEvents(
|
|
value?.content?.parts?.last.text ?? '', widget.imagePath))
|
|
.catchError((e) => displayError);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
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.
|
|
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',
|
|
),
|
|
])));
|
|
}
|
|
}
|