I Have positioned the UI elements but have no idea how to achieve that corner clip effect. Any help is appreciated, Thank you.
class SplashScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Align(
alignment: Alignment.topRight,
child: Image.asset('assets/images/Lines.png'),
),
Spacer(),
Center(
child: Image.asset('assets/images/Logo.png'),
),
Spacer(),
Align(
alignment: Alignment.bottomLeft,
child: Image.asset('assets/images/Lines.png'),
),
],
),
);
}
}
What I have:
Desired effect:
I put the widgets in a stack and offset them.
class SplashScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Align(
alignment: Alignment.topRight,
child: Container(
height: 150,
width: 150,
child: Stack(
clipBehavior: Clip.hardEdge,
children: [
Positioned(
right: -35,
top: -35,
child: Image.asset('assets/images/Lines.png'),
),
],
),
),
),
Spacer(),
Center(
child: Image.asset('assets/images/Logo.png'),
),
Spacer(),
Align(
alignment: Alignment.bottomLeft,
child: Container(
height: 150,
width: 150,
child: Stack(
clipBehavior: Clip.hardEdge,
children: [
Positioned(
left: -35,
bottom: -35,
child: Image.asset('assets/images/Lines.png'),
),
],
),
),
)
],
),
);
}
}
Related
I have several images that I need to display on the screen, the first image is center screen and then 3 at the bottom, the layout of the 3 at the bottom is a single image, and 2 below that.
Using Stack and Align Widgets I have gotten the main image center, but the other images are aligning to the top of the screen and not the bottom even though I am specifying it in the Align Widget.
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Stack(
alignment: Alignment.center,
children: <Widget>[
const Align(
alignment: Alignment.center,
child: Image(
image: AssetImage("assets/imgs/logo.png"),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 40),
child: Column(
children: [
SizedBox(
width: MediaQuery.of(context).size.width / 2.5,
child: Image(
image: const AssetImage("assets/imgs/logo1.png"),
width: MediaQuery.of(context).size.width / 2.5,
),
),
Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SizedBox(
width: MediaQuery.of(context).size.width / 4,
child: const Image(
image: AssetImage("assets/imgs/square_logo.jpg"),
),
),
const Padding(
padding: EdgeInsets.all(10),
),
SizedBox(
width: MediaQuery.of(context).size.width / 4,
child: const Image(
image: AssetImage("assets/imgs/square_logo1.png"),
),
),
],
),
],
),
),
),
],
),
);
}
Checkout this one:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Stack(
children: <Widget>[
Align(
alignment: Alignment.center,
child: Image(
image: AssetImage("assets/imgs/logo.png"),
),
),
Positioned(
bottom: 0,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: MediaQuery.of(context).size.width / 4,
child: Image(
image: AssetImage("assets/imgs/square_logo.jpg"),
),
),
SizedBox(
width: MediaQuery.of(context).size.width / 4,
child: Image(
image: AssetImage("assets/imgs/square_logo1.png"),
),
),
],
),
SizedBox(
width: MediaQuery.of(context).size.width / 2.5,
child: Image(
image: AssetImage("assets/imgs/logo1.png"),
),
),
],
),
),
],
),
);
}
Default Column's mainAxisSize: MainAxisSize.max,,, you can use
mainAxisSize: MainAxisSize.min,
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Title Text'),
),
body: SafeArea(
child: Stack(children: [
Column(
children: const [
Text('short content'),
],
),
const Positioned(
top: 100,
width: 320,
child: SizedBox(
width: 320,
height: 50,
child: ColoredBox(color: Colors.red),
))
])),
);
I need the red box taking almost the entire screen with, but it flows the text width. How should I make this work?
Wrap your Stack with SizedBox:
SizedBox(
width: double.infinity,
child: Stack(
clipBehavior: Clip.none,
children: [
SizedBox(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Text('short content'),
],
),
),
Positioned(
top: 100,
right: 0,
left: 0,
child: Container(
height: 50,
color: Colors.red,
))
],
),
),
You can wrap all children of the stack with either Align or Positioned so that the Stack can properly lay them out.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Title Text'),
),
body: SafeArea(
child: Stack(
children: [
/// The column is now positioned.
Positioned(
top: .0,
left: .0,
child: Column(
children: const [
ColoredBox(color: Colors.amber, child: Text('short content'))
],
),
),
const Positioned(
top: 100.0,
width: 320.0,
height: 50.0,
child: ColoredBox(color: Colors.red),
)
],
),
),
);
}
Output:
I am using stuck to overlapping my widget. this is my widget:
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
controller: controller,
child: SizedBox(
height: double.maxFinite,
child: Column(
children: [
Container(
width: double.maxFinite,
constraints: BoxConstraints(maxHeight: 200),
color: Colors.blue,
child: Stack(
// clipBehavior: Clip.none,
children: [
Positioned(
bottom: -25,
left: 0,
right: 0,
child: Container(
width: 60,
height: 60,
padding: EdgeInsets.all(8),
color: Colors.blue,
child: CircleAvatar(
backgroundColor: Colors.white,
child: Icon(Icons.mail)),
),
),
],
),
)
],
),
),
);
}
this is an image :
as you can see my image was cut off.I added clipBehavior: Clip.none, to stack but it just increase the height of stack :
I want the button to come down from the blue container. Like all the pictures I took, without cut off.
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
controller: controller,
child: SizedBox(
height: double.maxFinite,
child: Column(
children: [
Container(
width: double.maxFinite,
constraints: BoxConstraints(maxHeight: 200),
color: Colors.blue,
child: Stack(
// clipBehavior: Clip.none,
children: [
Container(
width: MediaQuery of(context).size.width,
height : 300,
margin : EdgeInsets.only(bottom: 50),
color: Colors.blue,
),
Positioned(
bottom : 0,
left:0, right:0,
child: Container (height : 300, width: MediaQuery.of(context).width,
child: CircleAvatar())
)
],
),
)
],
),
),
);
}
I fix this issue :
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
controller: controller,
child: SizedBox(
height: double.maxFinite,
child: Column(
children: [
Container(
width: double.maxFinite,
height: 200,
color: Colors.blue,
child: Stack(
clipBehavior: Clip.none,
children: [
Align(
alignment: Alignment.center,
child: Container(
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("test",
style:
TextStyle(fontSize: 25, color: Colors.white)),
],
),
),
),
Positioned(
bottom: -25,
left: 0,
right: 0,
child: CircleAvatar(
backgroundColor: Colors.white,
child: Icon(Icons.campaign)),
),
],
),
)
],
),
),
);
}
}
result:
I have simple application, and I try to center a circular image in the screen with the following:
main.dart:
import 'package:flutter/material.dart';
import './widgets/mashoff.dart';
void main() => runApp(_MyApp());
class _MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
toolbarHeight: 0,
backgroundColor: Colors.black, // status bar color
),
body: WMashoff(),
),
);
}
}
WMashoff widget:
import 'package:flutter/material.dart';
import './mashoff-item.dart';
class WMashoff extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Stack(
children: <Widget>[
Column(
children: <Widget>[
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
WMashoffItem(
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkMKry-Vcp7oU4erjWxG4pXhfva0weO4n4_g&usqp=CAU',
Alignment.topLeft,
),
WMashoffItem(
'https://hotforsecurity.bitdefender.com/wp-content/uploads/2019/12/pexels-photo-1090393-1024x682.jpg',
Alignment.topRight,
),
],
),
),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
WMashoffItem(
'https://images.indianexpress.com/2019/07/tiktok.jpg',
Alignment.bottomLeft,
),
WMashoffItem(
'https://www.gannett-cdn.com/-mm-/b4f5f67b6112c6935b66ff0cbffc204774fdc87c/c=0-0-3022-1700/local/-/media/2020/08/18/PalmBeachPost/ghows-LK-200819292-73c48e6d.jpg?width=660&height=372&fit=crop&format=pjpg&auto=webp',
Alignment.bottomRight,
),
],
),
),
],
),
Container(
height: 70,
width: 70,
alignment: Alignment.center,
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
const BoxShadow(
color: Colors.black87,
spreadRadius: 5,
blurRadius: 3,
offset: const Offset(0, 3), // changes position of shadow
),
],
image: const DecorationImage(
image: const NetworkImage('http://i.imgur.com/QSev0hg.jpg'),
fit: BoxFit.cover,
),
borderRadius: const BorderRadius.all(const Radius.circular(50)),
border: Border.all(
color: Colors.white,
width: 4,
),
),
),
],
),
);
}
}
WMashoffItem widget:
import 'package:flutter/material.dart';
class WMashoffItem extends StatelessWidget {
final Alignment _favoriteIconAlignment;
final String _img;
WMashoffItem(
this._img,
this._favoriteIconAlignment,
);
#override
Widget build(BuildContext context) {
return Expanded(
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Image.network(
this._img,
fit: BoxFit.cover,
),
Container(
alignment: this._favoriteIconAlignment,
child: const IconButton(
iconSize: 50,
icon: Icon(
Icons.favorite_border,
color: Colors.red,
),
onPressed: null,
),
),
],
),
);
}
}
Please focus the 2nd stack child in the WMashoff widget code (The Container element).
Once I give this container height and width, it is no more centered.
This is the result:
As you can see the image is in the top left. But I want it in the center. Where am I wrong?
When you do:
Container(
height: 70,
width: 70,
alignment: Alignment.center,
child: MyWidget(),
),
You create a 70x70 Container and align its child in the center.
Now, what you want is to align your container at the center of the Stack:
Try this:
Stack(
children: <Widget>[
Column([...]),
Align(
alignment: Alignment.center,
child: Container(
height: 70,
width: 70,
alignment: Alignment.center,
child: MyWidget(),
),
),
],
),
What my try looked like:
What I want to do:
What I have tried is Stack widget. But the half of container became invisible when container moves outside.
Stack(
children: [
Align(
alignment: new Alignment(0.0, 0.0),
child: Container(
height: 150,
width: double.infinity,
decoration: BoxDecoration(color: Colors.red),
),
),
Positioned(
top: -15,
left: 140,
child: Container(
height: 50,
width: 100,
decoration: BoxDecoration(color: Colors.black),
)),
],
)
Any info would be welcome.
Answer:
Just adding Padding to main container
try use Transform
return Container(
width: 400,
height: 200,
child: Stack(
children: <Widget>[
Align(alignment: Alignment.topCenter,
child: Transform.translate(
child: Container(
width: 100,
height: 50,
color: Colors.green,
child: Text('some text'),
),
offset: Offset(0, -20),
),
),
],
),
color: Colors.red,
);
Use Stack along with Padding on one container to make it lower.
return Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.only(top: 10),
child: Container(
width: 400,
height: 100,
color: Colors.white
),
),
),
Align(
alignment: Alignment.topCenter,
child: Container(
width: 70,
height: 30,
color: Colors.red
),
),
]
);
Full code to see the result exactly as you want using dartpad.dev/flutter paste this:
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Builder(
builder: (context){
return Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: EdgeInsets.only(top: 10),
child: Container(
width: 400,
height: 100,
color: Colors.white
),
),
),
Align(
alignment: Alignment.topCenter,
child: Container(
width: 70,
height: 30,
color: Colors.red
),
),
]
);
}
);
}
}
You were correct in using a Stack widget. The code would look like the following:
Center(
child: Stack(
alignment: Alignment.topCenter,
children: [
Padding(
padding: const EdgeInsets.only(top: 25,),
child: Container(
color: Colors.green,
height: 200,
width: 200
),
),
Container(
color: Colors.red,
height: 50,
width: 100
),
]
),
),
By adding top padding to the green container you are pushing it down making room for the red container to sit on top.
The result looks like this: