How to set margin for children - flutter

I am new to flutter and I want to set top margin for image in the below code, how I can do that? Just designing a screen with text and image on the screen.
Here is my code:
Widget build(BuildContext context) {
SizeConfig().init(context);
return Container(
padding: new EdgeInsets.only(
top: 160,
left: SizeConfig.blockSizeHorizontal * 5,
right: SizeConfig.blockSizeHorizontal * 5),
decoration: BoxDecoration(
image: DecorationImage(
image: new AssetImage("images/bg_splash.png"),
fit: BoxFit.fill)),
child: Column(
children: [
Text(
"App is allowing users to learn & Grow their Brains",
textAlign: TextAlign.center,
style: TextStyle(
decoration: TextDecoration.none,
fontSize: 25.0,
color: Color(0xFFFDEA20),
fontFamily: "Calistoga",
),
),
Image(
image: AssetImage('images/logo.png'),
height: 175,
),
],
));
}

wrap your image widget with padding
Padding(
padding: const EdgeInsets.only(top:10),
child: Image(
image: AssetImage('images/logo.png'),
height: 175,
),
),

Add SizedBox(height: 10.0), with your requirement in column above Image() widget
return Container(
padding: new EdgeInsets.only(
top: 160,
left: SizeConfig.blockSizeHorizontal * 5,
right: SizeConfig.blockSizeHorizontal * 5),
decoration: BoxDecoration(
image:
DecorationImage(image: new AssetImage("images/bg_splash.png"), fit: BoxFit.fill)),
child: Column(
children: [
Text(
"App is allowing users to learn & Grow their Brains",
textAlign: TextAlign.center,
style: TextStyle(
decoration: TextDecoration.none,
fontSize: 25.0,
color: Color(0xFFFDEA20),
fontFamily: "Calistoga",
),
),
SizedBox(height: 10.0),
Image(
image: AssetImage('images/logo.png'),
height: 175,
),
],
));

Here are two solutions for this
wrap your image with padding
Padding(
padding: EdgeInsets.only(
bottom: 12,
top: 12,
left:10,
right:10),
child: Image(
image: const AssetImage("images/logo.png"),
width:125,
),
),
wrap your image with Container add margin property
Container(
margin: EdgeInsets.only(
left: 10,
right: 10,
bottom:10,
top:10),
child: Image(
image: const AssetImage("images/logo.png"),
width: MediaQuery.of(context).size.width * 0.25,
),
),

Related

how to create this with flutter?

how to create this with flutter ? please let me knw how to create like this category section using flutter because i want to create like this
i tried but i couldn't get the correct layout for that this is what i tried
Stack(children: [
Container(
margin: EdgeInsets.all(15),
decoration: BoxDecoration(
color: Color(0xffCBCACD),
borderRadius: BorderRadius.circular(12),
),
width: 150,
height: 100,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [
Text(
indexCatName,
style: TextStyle(
fontWeight: FontWeight.bold),
),
Positioned(
top: 70,
left: 110,
child: Image(
width: 80,
image: NetworkImage(
'https://pizzafactory.lk/wp-content/uploads/2017/08/Pizza-Pollo-Alla-Diavola-300x300.png'),
),
),
],
)),
]);
Maybe like this :
Widget _test() {
return Container(
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
color: Color(0xffCBCACD),
borderRadius: BorderRadius.circular(12),
),
width: 150,
height: 100,
child: Stack(children: [
Positioned(
top: 20,
left: 15,
child: Text(
'123',
style: TextStyle(fontWeight: FontWeight.bold),
),
),
Positioned(
bottom: -30,
right: -15,
child: Image(
width: 100,
image:
NetworkImage('https://pizzafactory.lk/wp-content/uploads/2017/08/Pizza-Pollo-Alla-Diavola-300x300.png'),
),
),
]),
);
}

Strange blur in FLutter BackdropFilter

I try on the Back drop Filter in the Flutter widget but get a strange effect. On the one hand there is a blur, on the other hand the objects retain clear boundaries. What am I doing wrong?
child: Stack(
children: <Widget>[
Positioned(
right: 10,
top: 10,
child: Container(
width: 100,
height: 100,
color: Color(0xFFbfeb91),
),
),
Positioned(
bottom: 10,
left: 50,
child: Container(
width: 90,
height: 90,
color: Colors.green,
),
),
Text('Hello world'),
Container(
width: 180,
height: 180,
child: ClipRRect(
borderRadius: BorderRadius.circular(24),
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
child: Container(
width: 100,
height: 100,
padding: EdgeInsets.all(24),
// color: Color(0xFF55aaff),
color: Colors.white.withOpacity(0.5),
child: Text(
"Flutter Dev's",
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: Colors.black38,
),
),
),
),
),
),
],
)
I here is what I've got. Look at the green square - its bounds are clear, like we have 2 layers^ one is blured, and the other one (at the top) is NOT blured).
Picture with blur effect
And the same effect with letters 'Hello world'
You should give blur container position by margin as you are using stack as base widget. As you set postioned in above for two widget from top and left. so above two postined overleaped by blur container layout. thats why you getting blur screen. Try below code it will serve your purpose.
Stack(
children: <Widget>[
Positioned(
left: 10,
top: 10,
child: Container(
width: 100,
height: 100,
color: Color(0xFFbfeb91),
),
),
Positioned(
left: 10,
top: 120,
child: Container(
width: 90,
height: 90,
color: Colors.green,
),
),
Text('Hello world'),
Container(
margin: EdgeInsets.only(top: 10, left: 120),
width: 180,
height: 180,
child: ClipRRect(
borderRadius: BorderRadius.circular(24),
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
child: Container(
width: 100,
height: 100,
padding: EdgeInsets.all(24),
// color: Color(0xFF55aaff),
color: Colors.white.withOpacity(0.5),
child: Text(
"Flutter Dev's",
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: Colors.black38,
),
),
),
),
),
),
],
);

Can i add radius button in singlechild scroll view?

I am using Stack widget in which I have 2 widgets one is just an image and the other is Singlechildscrollview.
I need to add a button on the footer of the screen and fix this (not moveable). Need to be fixed on screen when the Singlechildscrollview scroll or not I need to fix this button. I am not sure how can I do this because if I out the button in Singlechildscrollview then it will show only when I scroll. I need to fix when I scroll or not button should appear.
Here is my code:
Stack(
children: <Widget>[
Container(
padding: EdgeInsets.only(top: statusBarHeight * 0.8),
height: height * 0.4,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/place2.jpg',
),
fit: BoxFit.fill,
),
),
),
SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(top: height * 0.3),
child: SingleChildScrollView(
child: ClipRRect(
borderRadius: BorderRadius.only(
topRight: Radius.circular(30),
topLeft: Radius.circular(30)),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: height * 0.05,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'NYC Food Festival',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 30),
),
),
SizedBox(
height: height * 0.02,
),
Container(
margin: EdgeInsets.only(left: width * 0.03),
child: Row(
children: <Widget>[
Icon(
Icons.calendar_today,
size: 20,
color: Color(0xff808080),
),
SizedBox(width: width * 0.02), // give it width
Column(
children: <Widget>[
Text(
'Sat, May 25, 2020',
style: TextStyle(
color: Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
)
],
),
),
SizedBox(
height: height * 0.02,
),
Container(
margin: EdgeInsets.only(left: width * 0.03),
child: Row(
children: <Widget>[
Icon(
Icons.attach_money,
size: 20,
color: Color(0xff808080),
),
SizedBox(width: width * 0.02), // give it width
Column(
children: <Widget>[
Text(
'25,000 PKR',
style: TextStyle(
color: Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
)
],
),
),
SizedBox(
height: height * 0.02,
),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'Snaps',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Container(
child: Column(
children: <Widget>[
CarouselSlider(
options: CarouselOptions(
autoPlay: true,
aspectRatio: 2.0,
enlargeCenterPage: true,
),
items: imageSliders,
),
],
)),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'About',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Container(
padding: EdgeInsets.only(
right: width * 0.03, left: width * 0.03),
child: DescriptionTextWidget(
text:
"Flutter is Google’s mobile UI framework for crafting high-quality native interfaces on iOS and Android in record time. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.")
),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'Included',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Included(),
SizedBox(
height: height * 0.01,
),
Included(),
SizedBox(
height: height * 0.01,
),
Included(),
SizedBox(
height: height * 0.01,
),
],
),
),
),
),
),
)
],
)
Here is the output of current view:
This is what I want:
You can achieve this look with relative ease.
Here is an example:
class BottomFadeButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
title: Text("Bottom Fade Button"),
),
body: Container(
// height: 500,
color: Colors.amberAccent,
child: Stack(
children: <Widget>[
Container(
child: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text("Hello "),
);
}),
),
// Bottom Button
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
height: 100,
decoration: BoxDecoration(
// color: Color.fromARGB(110, 255, 255, 255),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
gradient: LinearGradient(
colors: [
Color.fromARGB(30, 255, 255, 255),
Color.fromARGB(255, 255, 255, 255),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
child: Center(
child: InkWell(
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 100,
vertical: 20,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.blueAccent,
),
child: Text(
"BUY TICKETS",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.white),
),
),
onTap: () {
return print("Tap");
},
),
),
),
),
],
),
),
),
);
}
}
Output:
yes you can add the button in stack and then wrap with align widget with alignment.bottom_center

Flutter gesturedetector not working in stack widget

I am using the stack widget to show the back arrow button on an image. And its showing but the problem is it's not tappable mean Gesturededector is not working on Stack.
My code
Stack(
children: <Widget>[
Container(
height: height * 0.4,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/place2.jpg',
),
fit: BoxFit.fill,
),
),
),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () => print("first container"),
child: Align(
alignment: Alignment.topLeft,
child: Container(
margin: EdgeInsets.only(left: width * 0.03),
padding: EdgeInsets.only(top: statusBarHeight * 2),
child: Icon(
Icons.arrow_back_ios,
size: 25,
color: Colors.white,
),
),
),
),
SingleChildScrollView(
child: Padding(
padding: EdgeInsets.only(top: height * 0.3),
child: SingleChildScrollView(
child: ClipRRect(
borderRadius: BorderRadius.only(
topRight: Radius.circular(30),
topLeft: Radius.circular(30)),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: height * 0.05,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'NYC Food Festival',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 30),
),
),
SizedBox(
height: height * 0.02,
),
Container(
margin: EdgeInsets.only(left: width * 0.03),
child: Row(
children: <Widget>[
Icon(
Icons.calendar_today,
size: 20,
color: Color(0xff808080),
),
SizedBox(width: width * 0.02), // give it width
Column(
children: <Widget>[
Text(
'Sat, May 25, 2020',
style: TextStyle(
color: Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
)
],
),
),
SizedBox(
height: height * 0.02,
),
Container(
margin: EdgeInsets.only(left: width * 0.03),
child: Row(
children: <Widget>[
Icon(
Icons.attach_money,
size: 20,
color: Color(0xff808080),
),
SizedBox(width: width * 0.02), // give it width
Column(
children: <Widget>[
Text(
'25,000 PKR',
style: TextStyle(
color: Color(0xff000000),
fontWeight: FontWeight.bold,
fontSize: 15),
),
],
)
],
),
),
SizedBox(
height: height * 0.02,
),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'Snaps',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Container(
child: Column(
children: <Widget>[
CarouselSlider(
options: CarouselOptions(
autoPlay: true,
aspectRatio: 2.0,
enlargeCenterPage: true,
),
items: imageSliders,
),
],
)),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'About',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Container(
padding: EdgeInsets.only(
right: width * 0.03, left: width * 0.03),
child: DescriptionTextWidget(
text:
"Flutter is Google’s mobile UI framework for crafting high-quality native interfaces on iOS and Android in record time. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.")
),
SizedBox(
height: height * 0.02,
),
Container(
width: width,
margin: EdgeInsets.only(left: width * 0.03),
child: Text(
'Included',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 25),
),
),
SizedBox(
height: height * 0.02,
),
Included(),
SizedBox(
height: height * 0.01,
),
Included(),
SizedBox(
height: height * 0.01,
),
Included(),
SizedBox(
height: height * 0.01,
),
],
),
),
),
),
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
height: 100,
decoration: BoxDecoration(
// color: Color.fromARGB(110, 255, 255, 255),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
gradient: LinearGradient(
colors: [
Color.fromARGB(30, 255, 255, 255),
Color.fromARGB(255, 255, 255, 255),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
child: Center(
child: InkWell(
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 80,
vertical: 20,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Colors.blueAccent,
),
child: Text(
"BOOK NOW",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.white),
),
),
onTap: () {
return print("Tap");
},
),
),
),
),
],
)
I need to use it as a back button when I navigate but it's not working then for testing I just print a value onTap but its also not working I try to add behaviour also.
Can you try putting GestureDectector after SingleChildScrollView in a stack.
I guess SingleChildScrollView is lying above GestureDetector since it is a stack.
SingleChildScrollView(),
GestureDetector(),

how to add clipper to navigation drawer in flutter

I have created a navigation drawer.But I don't know how to add clipper using custom_clippers package
I want to add clipper image in the header section of my drawer.
here is the code
Widget _createHeader() {
return DrawerHeader(
// margin: EdgeInsets.zero,
// padding: EdgeInsets.zero,
child:ClipPath(
clipper: SideCutClipper(),
child: Container(
height: 600,
width: 500 ,
color: Colors.pink))
,
// decoration: BoxDecoration(
// image: DecorationImage(
// fit: BoxFit.fill,
// image: AssetImage('path/to/header_background.png'))),
child: Stack(children: <Widget>[
Positioned(
bottom: 12.0,
left: 16.0,
child: Text("Flutter Step-by-Step",
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w500))),
]));
}
Just Few Edits to brackets and parentheses:
Widget _createHeader() {
return DrawerHeader(
child: ClipPath(
clipper: SideCutClipper(),
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage('images/Rectangle-image.png'))),
child: Stack(
children: <Widget>[
Positioned(
bottom: 12.0,
left: 16.0,
child: Text(
"Flutter Step-by-Step",
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w500),
),
),
],
),
),
),
);
}
Tip
Use , (comma) after parentheses and brackets and reformat your code (Alt + Shift + F in VSCode). This will make debugging a lot easier, helps with documentation, makes code pretty and ... .