How to avoid Image Area showing Blue before Loading App - flutter

I am loading a rectangle logo saved as a .jpg into a CircleAvatar in my App.
When I am restarting my App, the area where the logo is loading appears blue for a few seconds. Then the real logo appears.
This Widget is found within a Stack.
This is how I transform my 1080x1073 image into a round logo within Flutter.
Container(
width: size.width * 0.5,
height: size.width * 0.5,
child: CircleAvatar(
backgroundImage: AssetImage('assets/images/logo.jpg'),
),
),
Does this happen because my image is too big? How should I handle this problem?
P.S. I am testing this on Visual Studio Code.

By default the background color is set to blue, you can modify the property and it will show whatever color you assign to it.
Container(
width: size.width * 0.5,
height: size.width * 0.5,
child: CircleAvatar(
backgroundColor: Colors.red, //here
backgroundImage: AssetImage('assets/images/logo.jpg'),
),
),

This has more to do with the delay it takes to load an image. You can change the background color like Yudhishthir suggested if the color is the actual problem or you can pre-fetch the image so that the image loads before anything is built.
This answer describes how to do the image pre-fetch using the precacheImage function.

This Cookbook solution is designed for this situation. Just replace the CircularProgressIndicator with whatever you want to show before the image is loaded.
https://flutter.dev/docs/cookbook/images/fading-in-images

Had similar blue background then changed to InkWell:
child: InkWell(
onTap: () async {
myFunc();
},
child: Container(
width: 50,
height: 50,
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(shape: BoxShape.circle),
child: Image.asset(
'assets/images/mybutton.png',
fit: BoxFit.cover,
),
),
),

Related

Draw a stripe with Box decoration

I want to decorate a container with a single stripe down the middle with white on each side like in the picture, I'm guessing I need to use Linear Gradient but I am struggling to get it right.Any Ideas?
Single Stripe Pic
Easy, You need to create a container and make a child of it as Stack and further create a container with aligned it to center. Give it some width and decorate it with linear gradient that's all
Here's some code I've written to help you. Feel free to customize on top of it and use within your app. Cheers!
Container(
width: 200,
height: 150,
color: Colors.white,
child: Stack(
children: [
Align(
alignment: Alignment.center,
child: Container(
width: 50,
color: Colors.red,
),
)
],
),
);

Customize the container shape

How to achieve following shape in flutter without using plugins? I want to implement the customized container using clippath. I'm trying to use lineTo() method. I did not get exactly this one.
try this package, clippy_flutter, using Point:
Point(
triangleHeight: 30.0,
edge: Edge.LEFT,
child: Container(
color: Colors.pink,
width: 100.0,
height: 100.0,
child: Center(child: Text('Point')),
),
),
and then stack them and overlap a little at the egde.

How to define the height of a container to default value or nothing

I have a Container and an image inside the container. I want to define the height for the container depending on the type of photo. If it is in portrait then the height should be of a size. If it is in Landscape then i want it to be in default height(the one automatically assigned).
In the Example below consider the height is False(Landscape Photo)
Container(
height: isPortrait
? MediaQuery.of(context).size.height / 1.4
: __________ , // How to define something like do nothing
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image.asset(image)
),
),
Since height is a double, there is no real way of setting it to "default" other than not assigning it. You could try something like this:
isPortrait
? Container(
height: MediaQuery.of(context).size.height / 2,
child: RestOfContent())
: Container(
child: RestOfContent(),
),
RestOfContent Would be a Builder/Widget with the rest of your code. This way you don't have to write duplicate code.

Label(text) at the bottom side of image in flutter

I have a custom build widget for grid view, in which i am either sending Icon or image to display along with Text (label). Now when i send icon i can display the test below icon, but when i am trying image i am not sure if the below approach is correct to display text or not, i am trying to put EdgeInsets in the container. I think it might fail due to pixel overload, but in my emulator, it seems to be fine.. Is there any better option for this scenario or the approach I am following is ok?
if (image != null) ...[
Container(
margin: EdgeInsets.only(top:150.0),
),
] else ...[
child ?? Container(),
],
Text('$label',
style: TextStyle(
color: AppColors.lightBlue,
fontSize: 20,
),),
If I am getting your problem well, then the solution is to use Alignment Class Flutter, in the Container. What this will do, is it allows the child of the Container to be aligned the way we want to align it. Since your requirement is to align the text at the bottom center, hence we would use the Alignment property, Alignment.bottomCenter.
Please Note: This will tell you how to align the content which will not be disturbed as per the device size, with a background image
SOLUTION 1 [EFFICIENT]
Container(
height: MediaQuery.of(context).size.height * 0.3, // ignore this, cos I am giving height to the container
width: MediaQuery.of(context).size.width * 0.5, // ignore this, cos I am giving width to the container
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage('https://i.pinimg.com/originals/0c/96/b1/0c96b19dc89ffdaa7ff737cfc04a095f.png')
)
),
alignment: Alignment.bottomCenter, // This aligns the child of the container
child: Padding(
padding: EdgeInsets.only(bottom: 10.0), //some spacing to the child from bottom
child: Text('Hello', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold))
)
)
The output would look like this now:
You can check this piece of code in any of the device, and it will show up just fine.
SOLUTION 2 [NOT RECOMMENDED]
Now, if you really want the margin, there is a cleaner way to do it, and that is using MediaQuery, as you can see in the above code also for giving out height and width.
Now, the question arises, what MediaQuery is, you can find it here, or in a simpler statement, it takes care of the size of the device. Now, whenever, we are concerned about the size of the device with the content alignment, we can use MediaQuery, and what it does, is it will adjust the margin/height/width/padding etc according to the Device size, and will look the same everywhere
How we can do that, I will just use your code only, and since you want the text to come at the bottom with the use of margin. Here, how you can do that
Container(
height: MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width * 0.5,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage('https://i.pinimg.com/originals/0c/96/b1/0c96b19dc89ffdaa7ff737cfc04a095f.png')
)
),
child: Container(
margin: EdgeInsets.only(top: MediaQuery.of(context).size.height * 0.25),
child: Text('Hello', textAlign: TextAlign.center, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold))
)
)
OUTPUT
MediaQuery.of(context).size.height = device height, and when we multiply, it does the math according to the device, and align the content. Similarly for MediaQuery.of(context).size.width= device width
I would suggest SOLUTION 1 only, since it is clean, and would not let you do a lot in nested child. I hope that clears your doubt. Let me know. Thanks :)

Fill a Card widget with a photo in Flutter

I have a circleavatar within a card widget but the image is not displaying as intended, how can I fill the card with the image rather than getting this eyeball effect?
Card with image
[Card(
shape: CircleBorder(),
elevation: 10.0,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 75.0,
child: ClipOval(
child: FadeInImage.assetNetwork(
placeholder:
'lib/screens/shared/defaultprofile.png',
image: userData.profilephoto,
fit: BoxFit.fill,
),
),
),
),
you are using ClipOval class A widget that clips its child using an oval.
By default, inscribes an axis-aligned oval into its layout dimensions and prevents its child from painting outside that oval, but the size and location of the clip oval can be customized using a custom clipper.You can remove it to have your desired view.
CircleAvatar(
Backgroundimage: NetworkImage(url)
...
Also see hw to add assets to you flutter app