I was trying to create an app using the flutter desktop implementation that would read the path to an image from a JSON file and display the image, and then when an Inkwell is hovered, it would change the path to another image. The following was the declaration of variables:
characterName = 'Elise';
var skill_1_icon_path =
characterData[characterName]['ability 1']['icon path'];
I was changing the characterName variable to switch to other image paths.
The following widget was being used:
Inkwell(
child: Image(
image : AssetImage(
skill_1_icon_path),
),
onHover: (value){setState(){characterName = "Jhon";}}
),
)
However, the image does not change despite changing the path. I have tried printing skill_1_icon_path in both before hovering and after, the path changes to it's correct value, but the image itself doesn't.
In your onHover, use setState to also change skill_1_icon_path to the desired new image path.
Related
I pick a picture using this library image_picker: 0.8.4+4. Then the image saved into List..
After that i want to show the image in next page and make it ready to edit using this library image_painter: 0.4.4+1.
ImagePainter.file(
// Need file,
key: _imageKey,
scalable: true,
initialStrokeWidth: 2,
initialColor: Colors.green,
initialPaintMode: PaintMode.line,
),
Actually i just need how to convert List<XFile> to File
One way to do it:
XFile has a property called path so the you can:
XFile xfile = ...;
File file = File(xfile.path);
If you have a list, you can use map or other way you prefer to create File using the path of a XFile.
I want to do similar think like this ( How to generate QR CODE with logo in flutter ) but instead of using asset image, I want to user pick an image with image_picker package and then store that image in file and use it in QR code as an embedded image. Please anyone help me out.
Thank You
Well The Thing you want to achieve has some boilerplate code involved so I would break it down into 2 brief steps:
Step 1: Pick Image as a File using image picker plugin that is, simply store image as File you can achieve it using:
File image = await ImagePicker.pickImage(
source: ImageSource.camera, imageQuality: 50
);
You can follow this to perfectly get image
Step 2: Now if you see the type of embedded image is actually an ImageProvider so you can use FileImage() and do:
QrImage(
data: 'This QR code has an embedded image as well',
version: QrVersions.auto,
size: 320,
gapless: false,
//You can use image here, Note this image should be of type File
embeddedImage: FileImage(image),
embeddedImageStyle: QrEmbeddedImageStyle(
size: Size(80, 80),
),
),
Hope this is what you wanted to achieve.
You just have to make a variable of the image that you will use for QR :
var image = Image.asset('assets/ur_image.png',);
and then add it in here :
QrImage(
embeddedImage: image.image,
data: 'hello',
version: QrVersions.auto,
size: 100,
),
From an AssetImage object i'm trying to get the file name in Flutter.
Image photo = Image.asset('assets/images/image-not-found.jpg');
print(photo.image); // AssetImage(bundle: null, name: "assets/images/image-not-found.jpg")
print(photo.image.assetName); // assetName not defined
print(photo.image.name); // name not defined
How to get this assetName property?
Something nicer without doing some regex on photo.image.toString();
EDIT:
the purpose here was to retrieve the image name in order to compare it.
So i just go for direct comparaison.
if(photo.image == AssetImage('assets/images/image-not-found.jpg')){
print("image was not found");
}
Try AssetImage for this.
var photo = AssetImage('assets/images/image-not-found.jpg');
print(photo.assetName);
//You can use split method like
`String toSplit = 'assets/images/image-not-found.jpg';
var imageName = toSplit.split("/");
print(var[2]);`
//You can get your image name stored in var[2].
First, add an asset folder in pubspec file like:
assets:
- specify a folder where is your assets are stored.
and use like this
Image.asset(
'assets/images/profile.png',
fit: BoxFit.fill,
),
I need to add the overlay above the camera like rectangle box and need to save the image to gallery
Tried the basic flutter camera exampleenter image description here
If you are not too worried about limiting the image resolution to the phone's display resolution, you can opt to automatically take a screenshot of the display and save it.
For this to work, you'll need Flutter native_screenshot package.
Capturing the screenshot:
Future<void> takeScreenShot() async{
String path = await NativeScreenshot.takeScreenshot();
print(path);
}
The image will be saved in the folder indicated by the path.
Since the function captures the whole screen, you can use a Stack() widget to display your camera preview and the overlay on top of each other. A simple build would look like,
#override
Widget build(BuildContext context) {
if (controller == null || !controller.value.isInitialized) {
return Container();
}
return OverflowBox(
child: Stack(
children: <Widget>[
CameraPreview(controller),
Container(),
],
),
);
}
In the above code you'll need to initialize the camera controller in order to display the CamerarPreview(). You can edit the Container() child widget (or anything similar to that) to create your overlay box.
This is how I set up a fullscreen image
Image.network(
"https://cdn.pixabay.com/photo/2017/02/21/21/13/unicorn-2087450_1280.png",
fit: BoxFit.contain,
height: double.infinity,
width: double.infinity,
)
I want to implement resize animation to switch between image fit BoxFit.contain or BoxFit.cover(like a lot of video app did)
I am pretty new for the animation behavior, could anyone give a help, thanks.
If you just want to switch the fit value then you'll need to create a StatefulWidget and keep the the fit value as a member variable and then call setState() and change the value in there. There won't be animations but you can take care of that using an animated container. So the steps would be:
Switching fit value
Turn your view widget into a StatefulWidget
Store your BoxFit value as a memberVariable. BoxFit _imageFit
Change _imageFit inside a setState call.
setState(() {_imageFit = BoxFit.fitHeight;});
Animating your image size
Wrap your image in a AnimatedContainer() widget and set it's dimensions using local member variables
double _animatedContainerHeight = yourStartingValue;
double _animatedContainerWidth = yourStartingValue;
...
AnimatedContainer(height: _animatedContainerHeight,
width: _animatedContainerWidth, child: YourImage);
Supply the AnimatedContainer with the duration and interpolation and then Change the size of it in the setState function mentioned above.
setState((){
...
_animatedContainerWidth = newValue;
// same for height
});