How to use icon as image - flutter

Is there any way to use Icon as image? I'm trying to display background image in CircleAvatar. If no image provided use icon as background image
CircleAvatar(
backgroundImage: _image != null
? AssetImage(_image.path)
: Icon(Icons.email), //<- here use icon as image
)
I know I can do something like this
CircleAvatar(
child: _image == null ? Icon(Icons.email) : null,
backgroundImage:
_image != null ? AssetImage(_image.path) : null,
)
I just found it a bit tedious adding 2 parameters for same purpose and also I want to use child parameter for something else

Related

Assignment of null to File variable in Flutter

How can I assign null to File variable. I got error when assigning null to File variable.
File myImage=null;
(myImage==null)?
CircleAvatar(
backgroundImage:AssetImage('assets/user_new.png'),
backgroundColor: Colors.cyanAccent,
radius: 70,
)
:
CircleAvatar(
backgroundImage:FileImage(myImage),
backgroundColor: Colors.cyanAccent,
radius: 70,
)
Sound null safety is available in Dart 2.12 and Flutter 2.
When using variables you declare are non-nullable by default and to make them nullable you have to use ? after datatype.
example:
int? i = null
In your case, it will be
File? myImage=null;
and you can use it like below:
(myImage==null)?
CircleAvatar(
backgroundImage:AssetImage('assets/user_new.png'),
backgroundColor: Colors.cyanAccent,
radius: 70,
)
:
CircleAvatar(
backgroundImage:FileImage(myImage!),
backgroundColor: Colors.cyanAccent,
radius: 70,
)
Here when using myImage you will use ! to tell a program that myImage will not be null.
Note: We should avoid using ! wherever possible as it can cause a run time
error but in your case, you are already checking for the null using a
ternary operator so you can safely use !.
I suppose you have nullsafety enabled. With nullsafety a variable declared without a ? cannot be null. To fix your issue do the following.
File? myImage = null;
In Flutter null safety is a thing.
With it, no variables can be null if they haven't got a ?. Add a question mark to your File declaration, as:
File? myImage = null;
Down in your code, you will need to check if myImage is null (since now it can be) if you want to assign it to the backgroundImage parameter. You have 2 choices for this:
backgroundImage:FileImage(myImage!) //Null check: Exception throw only if null
backgroundImage:FileImage(myImage ?? _anotherWorkingFile) //If: use another standard file if null

What is the default line style of a Flutter DataTable?

I'm trying to determine the default line style in a flutter data table
Looking at the source code is beyond my grasp -
https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/data_table.dart
I thought it might be Border(bottom: new BorderSide(width:1.0, color:Colors.grey.withOpacity(0.3))) but that's just eyeballing it imprecisely.
final BorderSide borderSide = Divider.createBorderSide(
context,
width: dividerThickness
?? theme.dataTableTheme.dividerThickness
?? _dividerThickness,
);
final Border? border = showBottomBorder
? Border(bottom: borderSide)
: index == 0 ? null : Border(top: borderSide);
According to this

Flutter: How can I set a background image for Circle Avatar

I have an image in my assets folder and want to set as background for circle Avatar how should I do this?
CircleAvatar(
backgroundImage: AssetImage('assets/image.png'),
);
This will work for you.
Make sure you have added an image in the asset folder and path inside pubspec.yaml file
like
assets:
- assets/image.png
Since you already have the Image in your Assets folder.
1) Add it to your pubspec.yaml file:
assets:
- assets/yourimage.png
2) Specify the image as the backgroundImage of your CircleAvatar:
CircleAvatar(
// the circle avatar has the background image property for specifying images
backgroundImage: AssetImage('assets/yourimage.png'),
);
Something like this ?
CircleAvatar(
child: Image.network(
"https://miro.medium.com/proxy/1*ilC2Aqp5sZd1wi0CopD1Hw.png",
fit: BoxFit.scaleDown,
))
Something like this will give you a avatar with a background that will show while loading a network image
CircleAvatar(
backgroundColor: Colors.pink,
backgroundImage: AssetImage('/path/to/asset.png'),
child: Image.network('https://www.example.com/with/online.png')
);
Don't forget to put the path to the asset folder in your pubspec.yml

Flutter Circular Avatar does not support Image from storage

I am looking for one widget which can handle both images(network image and storage image) in a circular shape. I have to select one image from storage if not available on network. I used many widgets and find CircleAvatar best among them. It makes circular shape if you put image in backgroundImage. It needs ImageProvider parameter. How I can to use Image.file(_file) in backgroundImage to pass it as a ImageProvider.
CircleAvatar(
radius: 80.0,
backgroundImage:
// NetworkImage("${imageNotfound}"), // NetowrkImage extends ImageProvider class, so it works
Image.file(_file), // How to convert my Image class to ImageProvider???
backgroundColor: Colors.transparent,
)
Note: My main concern is,use any single widget which have circular shape property for both(network image and storage image).
you can handle it as follows
local asset
CircleAvatar(
radius: 50.0,
backgroundImage: AssetImage("assets/images/image.jpg"),
)
network
CircleAvatar(
radius: 50.0,
backgroundImage: NetworkImage("https://images.unsplash.com/photo-1535083475219-aed59281b9aa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"),
)

"Unable to load asset" with image picker

I'm using Image Picker package in my Flutter project
I choose image from gallery then preview it in Image.asset widget
The problem here is if image name "example_name.png" (without spaces) the image is visible on the screen, but if image name "example name.png" (with spaces) the image is invisible like this Screenshot.
Error: Unable to load asset: /storage/emulated/0/Download/images (9).jpeg
File _image;
Image.asset(
_image != null
? "${_image.path}"
: getImage("icon.png"),
fit: BoxFit.cover,
width: 120,
height: 120,
);
...
Future chooseFile() async {
await ImagePicker.pickImage(source: ImageSource.gallery).then((image) {
setState(() {
_image = image;
});
});
}
You are using the wrong Image constructor. Use Image.file instead of Image.asset. Image.asset load files packaged in the application (assets section of pubspec.yaml) and ImagePicker does not have access to them.
in the image_picker (version 0.6.7 + 22) I was able to recover the image with this condition
if (photo == null) {
return Image (
image: AssetImage ('assets / no-image.png'),
height: 300.0,
fit: BoxFit.cover,
);
} else {
return Image.file (
Photo,
height: 300.0,
fit: BoxFit.cover,
);
}
Using Image.file is a good option but you like to display it in an effective way use
Image.file(_image).image this will help you to convert Image file to image provider