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

86 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
import 'dart:io';
import '../classes/descriptionImage.dart';
import '../classes/alert.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
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,
),
);
}
}
// 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 {
@override
void initState() {
super.initState();
_getEventInfosFromImage();
}
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: "What is this picture ?", images: [file.readAsBytesSync()])
.then((value) => showDescImageAddDialog(
context, value?.content?.parts?.last.text ?? ''))
.catchError((e) => print("problem gemini : ${e}"));
}
@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: SingleChildScrollView(
child: Column(children: <Widget>[
Image.file(File(widget.imagePath)),
Text("Analyse en cours...")
])));
}
}