Trouble drawing rectangle on a canvas using local coordinates- Flutter - flutter

I have 2 screens.
One the first screen I load up an image from the web and have my user draw a rectangle on the image. I am using local position from onPanStart and onPanUpdate callbacks in the GestureDetector. I am saving the start and end Offset values.
final taggableImage = GestureDetector(
onPanStart: (DragStartDetails details){
provider.updateRectangleStart(details.localPosition);
},
onPanUpdate: (DragUpdateDetails details ){
provider.updateRectangleEnd(details.localPosition);
},
child: CustomPaint(
foregroundPainter: provider.drawRect,
child: image,
),
);
Screenshot of drawing the rectangle on the first screen:
Now I would like to load up the images again in a new screen and draw the rectangle back onto the image from the Offset values I have saved earlier. But the rectangle always shows up in the wrong spot and sometimes even outside the image.
Here is how I am redrawing the rectangle from the saved Offset values.
final image = CustomPaint(
foregroundPainter: DrawRectangleService(provider.selectedDetection?.detectionRect ?? Rect.zero),
child: FittedBox(
fit: BoxFit.fill,
child: CachedNetworkImage(
placeholder: (context, url) => loadingWidget("Loading image"),
imageUrl: imageURL,
),
),
);
Screenshot of how it looks like when I redraw the rectangle
Question: How do I use the Offset values I saved on my first screen to redraw the rectangle on the image in my second screen.

convert your coordinates to double, in the range of 0 to 1, both the width and height by dividing them with original width and height of the Image widget,
on the new screen multiply them back with the new width and height of the Image widget.

Related

How can i make image in full screen without to lose some parts of image on screen

to display image according to it's original size i do the following
Image.file(File(fileCreated!.path)),
to display image to fit height and width (full screen) i do the following
Image.file(File(fileCreated!.path),fit: BoxFit.cover,height: double.infinity,width: double.infinity,),
ok it is now in full screen but i noticed there is some missing parts of the image contents is no longer visible on screen like 20 pixel from width and height , i know flutter do it for keeping the image quality the same. but How i ignore that behavior
How could i display image in full screen so the whole image parts is visible on screen ?
i tried with following
height:MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
whatever i change the height. the width will be auto changing too, even if i did not change the width !!!!
but same result
edit :
full parent
#override
Widget build(BuildContext context) {
return Scaffold(
body: Image.file(File(widget.imageFile,),fit: BoxFit.cover,height: MediaQuery.of(context).size.height,),
);
}
}
You can use MediaQuery, but I will prefer using LayoutBuilder on top widget. Also check the parent widget if it is having padding/margin.
Image.file(
File(fileCreated!.path),
fit: BoxFit.fill,
height:MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
),

FutureBuilder with Icon placeholder flickers

I'm using a FutureBuilder to load album thumbnails into a ListTile, as follows:
ListTile _albumTile(Album album, BuildContext context) {
return ListTile(
leading: FutureBuilder<Uint8List>(
future: AndroidContentResolver.instance.loadThumbnail(
uri: album.thumbnailUri,
width: 56,
height: 56,
),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Image.memory(snapshot.data!);
} else {
return const FittedBox(
fit: BoxFit.contain, child: Icon(Icons.album, size: 56));
}
}),
title: Text(album.album),
subtitle: Text(album.artist),
);
}
But, as the image loads, the placeholder icon is replaced with a blank image, and then with the final thumbnail. The blank image has the wrong width, which results in the ListTile title and subtitle jumping around, causing a flicker.
The following sequence of screenshots shows three consecutive frames:
Image placeholders.
Some of the futures have completed, and are showing thumbnails. The other tiles have a blank image (and the text jumps to the left).
All of the futures have completed.
Even when I fix the text jumping around -- by specifying fit: BoxFit.contain, width: 56, height: 56 for the image -- I still get a flash of white before the thumbnail appears.
What am I doing wrong?
Use precacheImage function to prefetch an image into the image cache.
If the image is later used by an Image or BoxDecoration or FadeInImage, it will probably be loaded faster.
I solved it by using frameBuilder. The documentation says:
If this is null, this widget will display an image that is painted as soon as the first image frame is available (and will appear to "pop" in if it becomes available asynchronously).
This describes exactly what I'm seeing.
It continues with the following:
Callers might use this builder to add effects to the image (such as fading the image in when it becomes available) or to display a placeholder widget while the image is loading.
...and the attached example shows how to implement a fade-in effect. For simplicity, however, I opted for the following:
return Image.memory(
snapshot.data!,
frameBuilder: (context, child, frame, wsl) {
if (frame != null) {
return child;
} else {
return _albumThumbnailPlaceholder();
}
},
Widget _albumThumbnailPlaceholder() {
return FittedBox(fit: BoxFit.contain, child: Icon(Icons.album, size: 56));
}
This seems to completely get rid of the blank frame.

Is it possible to have a listview maintain its position as images of variable height load in flutter?

Lets say you have a ListView of variable height:
List items are a Container with a mix of text and images. As such, the list items are of variable height. Sometimes no images. The text renders immediately as expected, but the images may take time to retrieve and render on screen
The images are retrieved from the network using CachedNetworkImage
Images are of variable height
When the Screen is opened the ListView automatically scrolls to item#11 (using ensureVisible technique)
So there are items both above and below your current position
At this point, when one of the network images above your position load up, the entire ListView will be pushed and you will no longer be looking at Item #11, rather somewhere randomly higher up
I considered initiating a new scroll in a callback after each image loads, however, due to network speeds, usually the listview scroll will finish before all the images load. If there are a lot of images, the images could take time to load, so it would be unreasonable to initiate a new scroll each time a new image is loaded, the screen just keeps scrolling forward every few seconds. It becomes dizzying and annoying.
Alternatively, the scrollview could jumpTo a new position as soon as the image loads, but I'm imagining there would be a slight delay between the two events and the user perceive a small "glitch" as the image loads and the listview immediately jumps to offset the image load. Even using a Future.microtask there is a very small perceptible 'glitch' as the image loads and the jumpto fires
It would be most preferable to have the listview expand the content upward somehow, so that the users current scroll position is maintained, as far as they are concerned.
Is it possible to have the ListView keep its position as the images load?
Assuming you have a predefined size for your images, you can wrap the image in a SizedBox(). This way your list will always have the same height and your items won't get pushed around.
EDIT:
Since your images are of variable size, I would probably animate to the desired location on every image load.
CachedNetworkImage has a callback
imageBuilder: (context, imageProvider) {
/// Animate to desired index
return Image(image: imageProvider);
}
Animated container, might help you. It can adjust the height automatically, depending on the height u provide in builder.
Also you can use this answer to determnin image height and width in rnutime.
Images are of variable height
To overcome this, Either we take the image size or aspect ratio of the image while storing the image along with other data.
While retrieving data, along with other text data we will receive the aspect ratio or height for the image.
I would use the same height or ratio and show placeholder image till images are loaded.
CachedNetworkImage(
imageUrl: countryList[index].flagUrl,
height: 60, // Set your height according to aspect ratio or fixed height
width: 60,
fit: BoxFit.cover,
placeholder: (_, __) => Container(
alignment: Alignment.center,
height: 60, // Set your height
width: 60,
color: Colors.red.withAlpha(80),
child: Text(
countryList[index].name[0],
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
)
Image must be of specific aspect ratio I believe. You can define height according to aspect ratio.
Try as follows:
ListView.builder(
shrinkWrap: true,
itemCount: images.length,
itemBuilder: (ctx, i) {
return Column(children: [
ButtonItems(i),
const SizedBox(height: 10),
]);
}));
Button Items class
class ButtonItems extends StatefulWidget {
final int i;
ButtonItems(this.i);
#override
_ButtonItems createState() => _ButtonItems();
}
class _ButtonItems extends State<ButtonItems> {
var images = [
"https://opengraph.githubassets.com/2ddb0ff05ef9ccfce35abb56e30d9c5068e01d1d10995484cfd07becee9accf7/dartpad/dartpad.github.io",
null,
"https://opengraph.githubassets.com/2ddb0ff05ef9ccfce35abb56e30d9c5068e01d1d10995484cfd07becee9accf7/dartpad/dartpad.github.io"
];
#override
Widget build(BuildContext context) {
print(images[widget.i]);
return Container(
height: 50,
color: Colors.grey,
child: Row(children: [
AspectRatio(
aspectRatio: 3 / 2,
child: images[widget.i] == null
? Container()
: Image.network(images[widget.i]!, fit: BoxFit.cover),
),
Text("Title " + widget.i.toString()),
]));
}
}

Flutter. Set parent's height based on child canvas drawing's

Is there any way to set parent container's height based on height of what is drawn in the child canvas?
I am using a custom painter like this:
double width = MediaQuery.of(context).size.width;
return Container(
color: Colors.yellow,
height: 240,
width: width,
child: CustomPaint(
painter: ShapePainter(),
),
);
Then ShapePainter() draws different shapes (1 shape for each canvas in the list).
But as you can see, some shapes like 2nd rectangle, take twice the space they actually need.
I can calculate height of a shape inside ShapePainter() easily,
but I have no idea how to apply it to its parent's height. (Except calling an insta-setState() from child, but there should be a better way, because this may flicker for one frame)
Thanks for any help!
Flutter has several phases that it goes through when creating a frame, including building, layout, and painting. The size of widgets is determined during the layout phase. So you can't set the height based it what was painted. (Except, as you've said, by using something like setState to generate a new frame.) You'll have to determine the the size you want before any painting has happened. For example, you can give your CustomPainter a getter to provide the shape:
class ShapePainter extends CustomPainter {
ShapePainter({#required this.shape});
final Shape shape;
void paint(Canvas canvas, Size size) {
// the painting
}
Size get size {
// determine size based on shape
}
}
...
Widget build(BuildContext context) {
final shapePainter = ShapePainter();
return Container(
color: Colors.yellow,
height: shapePainter.size.height,
align: Alignment.center,
child: CustomPaint(
painter: shapePainter,
),
);
}

Gesture Detector doesn't want to size itself to a child container (unless you give it a color)

So this problem is driving me mad so any help would be greatly appreciated :)
I currently have a custom Gesture Detector which I want to size to the width and height parameters I've given to the child container. Though it will only do this if the child container is given a colour (transparent in this case). I'll post some code and screenshots down below to explain a bit further.
Things that I've tried include using SizedBox instead of a container but that didn't work. I also tried changing the values of behaviour but that didn't seem to change anything either.
//The params width and height are given values up here
...
return Container(
color: Color(0XFF0000FF), //Blue container which shows the actual size I want the GD to be.
child: ColorFiltered( //Red shows how big the GD actually is.
colorFilter: ColorFilter.mode(Color(0XFFFF0000), BlendMode.hue),
child: RawGestureDetector(
key: gestureKey,
behavior: HitTestBehavior.translucent,
gestures: <Type, GestureRecognizerFactory>{
_SingleDeviceGestureDetector: GestureRecognizerFactoryWithHandlers<
_SingleDeviceGestureDetector>(
() => _SingleDeviceGestureDetector(
//When the pointer touches down on the screen
onHorizontalDragDown: (details) { ... }
onHorizontalDragUpdate: (details) { ... }
onHorizontalDragUp: (details) { ...}
),
(_SingleDeviceGestureDetector instance) {},
),
},
child: Container( //The child container with a transparent colour.
//color: Color(0X00000000),
height: height,
width: width,
child: ...
),
),
),
);
});
}
1 Shows the widget when color is uncommented
2 Shows the widget when color is commented
When you uncomment the color field in the child container (1) the gesture detector will be the same size as the first parent container (which is what I want), and when you comment out the color field (2) the gesture detector is smaller.
Even though I have the fix, it feels very 'hacky' and it would still be good to figure out why it does this.
Cheers