return Stack(
children: <Widget>[
Image.asset(
'assets/logo/greenframe.png',
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
fit: BoxFit.fill,
),
...
I'm using this code for using a background image on my app. It is a rectangle frame. It works okay with my Redmi Note 8 screen but on another phone with a different size of the screen, it's not looking good. Not fitting the screen. Is there a way to fix this?
How can I set the size based on screen size?
You can use MediaQuery.of(context).size to get a responsive design for all screen sizes. For example, defining the size of the icon button will look like this:
IconButton(
iconSize: MediaQuery.of(context).size.height * 0.03, // Here!
icon: Icon(
Icons.arrow_back,
color: Colors.black,
),
onPressed: () => Navigator.pop(context),
),
Read more from here.
Related
I am using an image as a child of the floating action button on flutter. I don't need any background color.
and also I need to make the image size bigger. how can I do this? help, please.
Simply do this:
FloatingActionButton(
backgroundColor: Colors.transparent,
)
Edit: My answer is just a workaround. backgroundColor: Colors.transparent goes well.
I think for your case, you can also try workaround like this,
floatingActionButton: GestureDetector(
onTap: () {
// Do Something
},
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage("https://www.dartpad.dev/dart-192.png")),
borderRadius: BorderRadius.circular(0.50),
),
width: 50.0,
height: 50.0,
),
),
Play with height and width values for desired result. You can also add boxShadow to Container to look like the actual FAB.
Refer: https://api.flutter.dev/flutter/painting/BoxDecoration-class.html
and also for including your image from asset, refer https://api.flutter.dev/flutter/painting/DecorationImage-class.html
Hope that suits your case!
Colors.transparent
Will remove the color from any widget which accepts color parameter. Please use it in your FloatingActionButton like below:
FloatingActionButton(
elevation: 0.0,
child: new Icon(Icons.check),
backgroundColor: Colors.transparent,
onPressed: (){}
)
I used flutter_svg: ^0.18.0 to use svg images inside my project. But I cannot able to find a proper way to change the size of it.
Here's my code
body: SafeArea(
child: Center(
child: SvgPicture.asset('assets/images/morelights.svg'),
),
),
The key hint is to use fit attribute like this:
SvgPicture.asset(
'assets/svg/notification.svg',
height: 5, width: 5,
fit: BoxFit.scaleDown
)
I found a solution for this :)
Steps
Wrap svg icon inside a container. ( Container should be a child widget)
Set color of the container to transparent.
Change the size of the container. (Svg icon inhert the size of parent widget.)
Note: You can use IconButton widget as well. Container works better in my case :)
Example
You can wrap your SvgPicture widget with IconButton
IconButton(
onPressed: (){},
icon: SvgPicture.asset(
icDate,
height: 24,
width: 24,
),
),
you just have to use properties width and height of svg picture.
SvgPicture.asset(
'assets/images/bm-icon1.svg',
width: 18.0,
height: 18.0,
),
I found the solution by accident. Wrap SvgPicture with Container widget and add alignment.
That way you can freely change the width and height of the SvgPicture.
Container(
height: 100,
width: 100,
alignment: Alignment.center, // <---- The magic
padding: const EdgeInsets.all(12),
child: SvgPicture.asset(
'assets/icons/svg-image.svg',
semanticsLabel: 'Image',
height: 50,
width: 50,
),
),
Hope this works in your case
Wrap it in a Container widget as the SvgPicture.asset() will always take the full available height or width of the parent. you can size the Container widget height and width to get the desirable sizing you want for your SVG.
Container(
child: SvgPicture.asset('assets/images/morelights.svg'),
height: 100,
),
Edit:
You can use the SizedBox to easily achieve this:
SizedBox(
width: 100.0,
height: 150.0,
child: SvgPicture.asset('assets/images/morelights.svg',),
)
Original Answer:
You can specify the height and width in the asset constructor itself:
SvgPicture.asset('assets/images/morelights.svg', height: 100, width: 150)
Read all the available properties in this document: SvgPicture.asset
You can use the height and width parameters of SvgPicture.asset:
Center(child: SvgPicture.asset(icon, height: 20, width: 20))
You can Wrap the SvgPicture.asset widget with an IconButton, This property have the behavior to overwrite this problem. the sample code is given below.
IconButton(
icon: SvgPicture.asset('assets/svg_images/ic_plane.svg',
color: const Color(0xffFF5D2A),
height: 20,
width: 20,
), onPressed: () { },
),
This is from London App Brewery completed Flutter project "BMI Calculator": https://github.com/londonappbrewery/BMI-Calculator-Flutter-Completed.git
I'm not sure if caused by my outdated android phone or the android screen is too small, but I'm getting Bottom Overflow Pixels For example, the top two cards have an error message of Bottom Overflow by 19 Pixels, the center card has an error of Bottom Overflowed by 60 pixels, and bottom two cards have an error of Bottom Overflowed by 56 pixels.
Additionally, if I rotate my phone to 90 degrees, the card image size decreases dramatically, as you can see from the second image.
Please help me fix this.
Thank you
class _InputPageState extends State<InputPage {
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('BMI CALCULATOR'),
),
body: Column(
children: <Widget[
Expanded(
child: Row(
children: <Widget[
Expanded(
child: ReusableCard(
colour: colorCode,
cardChild: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget[
Icon(
FontAwesomeIcons.mars,
size: 20.0,
),
SizedBox(
height: 5.0,
),
Text(
'MALE',
style: TextStyle(
fontSize: 10.0,
color: Color(0xFF8D8E98),
),
)
],
),
),
)]),),
]));
}
}
This is happening because your UI takes up too much space for your phone! You have two options to solve this.
Either you wrap your overflowing widgets in a scrolling widget, like SingleChildScrollView, this will let you scroll the Widget if it is too large.
Or you calculate the size of the widget based on device size. You can do that by using the MediaQuery class:
MediaQuery.of(context).size.height Can be used to get device height for instance.
Have a look at this article if you want to go down that path. https://medium.com/tagmalogic/widgets-sizes-relative-to-screen-size-in-flutter-using-mediaquery-3f283afc64d6
Of course you could also just reduce the size of your Widgets so it fits on your device. Thats fine for Tutorial purposes, but if you actually want to publish an App, go with the MediaQueryapproach.
Good luck!
I want the lower path of Doctor image to get transparent so that I can see the tiles going underneath it.
How can I do that? I tried opacity but it is making the whole image fade.
Remember just the lower part not whole image
For different opacity in the same image, you can use a ShaderMask like this:
ShaderMask(
shaderCallback: (rect) {
return LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: <Color>[
Colors.black.withOpacity(1.0),
Colors.black.withOpacity(1.0),
Colors.black.withOpacity(0.3),
Colors.black.withOpacity(0.3),// <-- change this opacity
// Colors.transparent // <-- you might need this if you want full transparency at the edge
],
stops: [0.0, 0.5,0.55, 1.0], //<-- the gradient is interpolated, and these are where the colors above go into effect (that's why there are two colors repeated)
).createShader(Rect.fromLTRB(0, 0, rect.width, rect.height));
},
blendMode: BlendMode.dstIn,
child: FlutterLogo(size: 80, colors: Colors.red),
),
You'll have to play around with the LinearGradient stops in order to get the effect that you're looking for. Just for completeness sake, let me explain the colors and the stops that I chose. Since the gradient is interpolated, you need a really strong step from one color to the other. So, looking at the stops and colors, it reads like this:
start the first color (with opacity = 1.0) at 0% of the way down and go until you hit 50% of the way down, then interpolate from 50% to 55% from opacity 1.0 to opacity 0.3 (that's why those numbers need to be close together) Finally, end with opacity 0.3 at 100% of the image.
I explained that piece, because you will have to adjust the 0.5 and 0.55 piece to make it look how you want.
You will have to use Stack and positioned widget side by side.
Add your image as child of the Container. I hope this helps
Stack(
children: <Widget>[
Positioned(
right: 20,
child: Container(
height: 150,
width: 100,
color: Colors.black,
),
),
ListView(
padding: EdgeInsets.only(top: 100),
children: <Widget>[
Ink(
color: Colors.green,
child: ListTile(
title: Text("Tile 1"),
subtitle: Text("subtitle"),
),
),
Ink(
color: Colors.blue,
child: ListTile(
title: Text("Tile 2"),
subtitle: Text("subtitle"),
),
),
Ink(
color: Colors.green,
child: ListTile(
title: Text("Tile 3"),
subtitle: Text("subtitle"),
),
),
],
),
],
),
Currently the only way I could make this kind of curved effect would be through making 2 container, making 1 a child of another and making 1 container's colour into the background colour.
However I cannot replicate this effect if the background is an image. Could anyone assist?
Example of the code I use:
Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Container(
height: 100,
color: Color(0xFF0D6068),
),
Expanded(
child: Container(
color: Color(0xFF0D6068),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(50),
topRight: Radius.circular(50),
)),
),
))
],
)),
);
You can make use of a Stack widget to achieve this, such that the background color or image would be at the back then the curved container would be stacked on it. Here is an example code.
Stack(children <Widget> [
Image.asset(''),
Container()
]
)
Instead of using an image asset, you can make use of BoxDecoration with the decoration parameter of a container. The BoxDecoration has an image parameter where you can provide your image with a DecorationImage() widget.