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: () { },
),
Related
I need your help. I am trying to replicate a design I saw from Instagram for personal growth. I have an Image inside Stack, the Stack is a child to a Container with Border radius. However, the image inside the box doesn't take after the border radius. I have tried different methods but none of them worked.
What I am getting is
VS what I am trying to get
How can I make the image have a border radius even though is Positioned outside the box ??
Here is a snippet of my code
Container(
width: 300,
height: 500,
decoration: BoxDecoration(
color: boxColor,
borderRadius: BorderRadius.circular(12.0),
),
child: Stack(
children: [
Positioned(
right: -150,
top: -200,
child: ClipRRect(
clipBehavior: Clip.antiAlias,
// borderRadius: BorderRadius.circular(12.0),
child:Image.asset(
height: width*.35,
width: width*.35,
"assets/box.png",
),
),
),
],
),
)
Include clipBehavior: Clip.hardEdge, on container
Container(
width: 300,
height: 500,
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(12.0),
),
If you still like to use ClipRRect use it on top of container.
I have implemented a BottomNavigationBar. Each BottomNavigationBarItem has icon and label. I want to give margin or padding to each BottomNavigationBarItem separately.
BottomNavigationBarItem(
icon: Container(
padding: EdgeInsets.only(
bottom: 10,
),
margin: EdgeInsets.only(
left: 20,
),
child: Image.asset(
"assets/icons/icn_home_selected.png",
height: 16,
width: 16,
),
),
label: "Home",
)
Wrapping the icon with Container and using margin property I am able to move the icon but how can we move label?
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: (){}
)
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.
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"),
),
),
],
),
],
),