mobile-flutter/covas_mobile/lib/pages/DisplayPictureScreen.dart

98 lines
3.1 KiB
Dart
Raw Normal View History

2024-07-07 10:53:33 +02:00
import 'package:flutter/material.dart';
import 'dart:io';
2024-07-17 23:39:51 +02:00
import '../classes/descriptionImage.dart';
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';
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>
with ShowDescImageAdd, ShowErrorDialog {
2024-07-10 23:31:25 +02:00
@override
void initState() {
super.initState();
_getEventInfosFromImage();
}
2024-07-22 23:11:45 +02:00
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");
}
2024-07-24 23:17:11 +02:00
Future<void> searchEvents(String json) async {
showDescImageAddDialog(context, json);
}
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:
"Peux-tu donner le nom, la date et le lieu de l'évènement sous format JSON avec les valeurs suivantes : name, place et date",
2024-07-20 20:22:57 +02:00
images: [file.readAsBytesSync()],
modelName: "models/gemini-1.5-flash-latest")
2024-07-24 23:17:11 +02:00
.then((value) => searchEvents(value?.content?.parts?.last.text ?? ''))
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.
body: SingleChildScrollView(
child: Column(children: <Widget>[
Image.file(File(widget.imagePath)),
Text("Analyse en cours...")
])));
2024-07-07 10:53:33 +02:00
}
}