Open main-c opened 1 year ago
Hello, Great job,
I hope that all the coders will be ready with great system on Friday
thank you for this tutorial but for everyone who uses this tutorial don't forget to add these dependencies but before tape this on your command line
flutter pub add image_painter
and on pubspec.yaml add this
dependencies:
image_painter: latest
please in place of this
ImagePainter.file(
widget.image!,
key: _imageKey,
scalable: true,
textDelegate: TextDelegate(),
initialPaintMode: PaintMode.freeStyle,
),
add this code if else you will have this error No Material widget was found due to his forgetting to add a Scaffold widget and container widget on your phone when you'll build the application
Scaffold(
body:Container(
padding: EdgeInsets.all(20),
child:ImagePainter.file(
widget.image!,
key: _imageKey,
scalable: true,
textDelegate: TextDelegate(),
initialPaintMode: PaintMode.freeStyle,
)));
}
thank you for this tutorial but for everyone who uses this tutorial don't forget to add these dependencies but before tape this on your command line
flutter pub add image_painter and on pubspec.yaml add this dependencies: image_painter: latest
You don't have to install it
//Constructor for loading image from [File].
factory ImagePainter.file(
File file, {
...
AlertDialog? onDrawingEnd,
...
}) {...
directly after adding this will you find it after the constructor
ImagePainter._(
....
onDrawingEnd:onDrawingEnd,
);
add this line also if else you will never initialize the Alertdialogue
Food Annotation Image app with Flutter
This present guidelines of our coding party about build a food annotation image app with flutter.
Part 1 : Get a food image from gallery or camera
Step 1 : Setup flutter project
Create a new flutter projects name food_annotation using
flutter create food_annotation
Open created app in vs code and run it on your mobile devices or emulator with
flutter run
Step 2 : Install and use Image_picker package
[image_picker](https://pub.dev/packages/image_picker/) is a flutter package that use to pick image from gallery or camera.
flutter pub add image_picker
.main.dart
and remplace by the following:picker_image.dart
in the directorylib/ui/screens/
. This file contains a StatefulWidget class calledPickerImage
(Who is imported in the previous step)PickerImage
class, return aScaffold
without appBar and for body a Column.ImageSource
, who represent the type of action forcamera
orgallery
display_image.dart
in the directorylib/ui/screens/
who represents the screen where the picked image will be displayed. This file contains a StatefulWidget class namedDisplayImage
Part 2: Integration of image painter package
Image painter is a popular flutter package that uses to paint on an image, we will uses customize it according of our needs
Step 1 : Clone the package
Step 2 : Add the package in the project
src
in thelib
directory of your projectlib
directory present in image painter package and paste it in thesrc
directory that you have createdPart 3 : Adapt the package according to our wishes
At this level, you can already paint on an image picked from camera or from gallery, now we'll custom package by adding some form for naming a selection and save it.
Step 1 : Name a selection
The package give us some method like
onColorChanged
oronPaintModeChanged
that are functions call when color or paint mode changed resp. We want to add an another function that will can when drawing on image end. In our case we will show a dialog with a form to name the current selection.Open
src/_paint_over_image.dart
fileAdd a attribute
onDrawingEnd
with typeAlertDialog
onImagePainter
classUpdate
ImagePainter.file
factory by adding the new attributeIn the same file, use your IDE search tools to find
_scaleEndGesture
method. This method is fires when users stop interacting with the screen. Add to that method ashowDialog
method that will returnonDrawingEnd
as dialogGo back into your code in
display_image.dart
and changes the way you instantiatesImagePainter
class by addingonDrawingEnd
attribute, his value could be aAlertDialog
that you will defines.hot reload your app, if everything went well, a Alert dialog will display after an annotation
Step 2 : Get Coordinates of an annotation
Now that our app can register a food, we need to get coordinates of the current annotation
Image painter package defines the path of an annotation with the class
PaintInfo
define inlib/src/_image_painter.dart
add a
toJson
convert to that class. this method will help to get a json expression of the coordinates of an annotationThe package stores all the coordinates of the current image in a list of
PaintInfo
named_paintHistory
defined as private attributes inImpageState
class in filelib/src/_pain_over_image.dart
. Since it is a private class, we will define get and set methods to access its value directly from ourDisplayImage
classback in our file, we will use the
_imagekey
variable to access the attributes ofImageState
including our getter and setters. Modify the action of your dialog as folowsPath 4 : Save in Json file
Now to that we can get coordinates of an annotation, the next steps is saving in a json file
Step 1: use path_provider to create a file in local storage
path provider is a Flutter plugin for finding commonly used locations on the filesystem. install it by running this command
flutter pub add path_provider
in
lib
directory create a new directoryservices
and in that directory create a new dart filestorage.dart
. this will contains the methods for export data in a fileopen
lib/services/storage.dart
and paste this :Step 2 : Call our method after submit an annotation
In this coding party we consider that we save after each annotation so we modify
onPressed
of the finish button as follows :Your file has been saved, go check in your local storage of the app