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?
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 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: () { },
),
Column widget is showing extra space between consecutive widgets even when using simple containers with their padding set to 0. I have tried using SizedBox, setting mainAxisSize to min but nothing seems to be working.
Moreover this issue is only appearing in Android OS and not on Web or iOS.
My Code:
Scaffold(
backgroundColor: Colors.yellow,
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
padding: EdgeInsets.zero,
height: 50,
color: Colors.black,
),
Container(
padding: EdgeInsets.zero,
height: 100,
color: Colors.black,
),
SizedBox(
height: 20,
),
Container(
height: 150,
color: Colors.black,
)
],
),
),
)
Output:
I have zoomed the image a bit to on the right to display the gap better. There is a line of space appearing between the 2 containers. I have placed another container of 150 height below the column to show the difference between complete opaque and space. I have recreated this same behaviour on Pixel 3 Emulator as well as OnePlus 6T device.
Can anybody help me with a fix to this. I know this can be solved using Stack and Positioned but I was hoping to find out if this is an issue in Flutter SDK or a fixable widget behaviour.
That's a pretty annoying effect indeed. And though I can't tell you why this is happening, playing around with it a bit, it seems that giving the Container a BoxDecoration with a Border instead of a color removes the artifacts:
Scaffold(
backgroundColor: Colors.yellow,
appBar: AppBar(
title: Text('title'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
color: Colors.black,
),
padding: EdgeInsets.zero,
height: 51,
//color: Colors.black,
),
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
color: Colors.black,
),
padding: EdgeInsets.zero,
height: 100,
),
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
color: Colors.black,
),
height: 150,
//color: Colors.black,
)
],
),
),
)
For me answer didn't work (probably because i had clipPath over container but i found similar solution)
decoration: BoxDecoration(
border: Border.all(width: 0, color: Colors.yourColor),
color: Colors.yourColor,
),
In all containers at your column
I am new to flutter and creating a project where the design contains rounded top navigation. In flutter we have tab bars. So I searched around but could not find the solution. There were solution for rounded tabs but not the tab bar as whole. Is it even possible or is there a work around it.
You can create custom tab bar using Container and Stack to positioned Container on top.
Something like this.
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Stack(
children: <Widget>[
Positioned(
top: 0,
child: Container(
width: MediaQuery.of(context).size.width,
height: 80,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(25.0))
),
child: Row(
children: <Widget>[
//icons buttons
],
),
),
)
],
)
),
Hi Everyone I am creating a profile page for a flutter app.
The SliverAppBar needs to show the below view when in the expanded state:
And below view when the user scrolls the silver list.
As you see this is a custom stack that I can create.
I don't know how to do it in slivers in a flutter.
Any links would also be highly appreciated to learn in-depth about flutter and slivers especially.
Code for the collapsed stack:
Stack(
alignment: Alignment.topRight,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 12),
width: 2000,
height: 80,
decoration: BoxDecoration(boxShadow: [
BoxShadow(blurRadius: 5.0, color: Colors.black87)
])),
Container(
margin: EdgeInsets.only(right: 20, top: 5),
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(blurRadius: 5.0, color: Colors.black87)
],
border: Border.all(color: Colors.cyan, width: 3.0)),
child: CircleAvatar(
backgroundImage: AssetImage(
"assets/images/food/avocado-f.jpg"),
radius: 50,
),
)
],
),
You should use SliverPersistentHeader and create your own SliverPersistentHeaderDelegate.
In the build method you add your Stack view and you can use Transform or SizedBox to scale and transform the circle depending on the height of the header.
There is a nice tutorial that explains how to use this widget
https://medium.com/flutter-community/how-to-code-a-dynamic-header-in-flutter-e171ec2231bf