Container is not center aligned in stack - flutter

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(),
),
),
],
),

Related

How to overflow content from the top and clip it in flutter?

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'),
),
],
),
),
)
],
),
);
}
}

Flutter On Press Problem with Negative Y offset Positioned in a Stack

I have two widgets in a stack. Which is demonstrated below.
The second widget is a button and it is located in a positioned widget which has a negative Y axis.
The problem is overflowed are is not clickable. I there any way to fix this issue?
Stack(
fit: StackFit.expand,
overflow: Overflow.visible,
clipBehavior: Clip.none,
alignment: AlignmentDirectional.topCenter,
children: [
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(29)),
child: ClipPath(
clipper: NavbarClipper(),
child: Container(
color: Colors.white,
),
),
),
Positioned(
top: -30,
child: Container(
width: context.dynamicHeight(0.16),
height: context.dynamicWidth(0.16),
child: FittedBox(
child: FloatingActionButton(
onPressed: () {},
backgroundColor: Colors.orange,
child: Icon(Icons.ac_unit),
),
),
),
)
],
)
Thanks.
You can wrap rect with padding instead of negative margin to button
class MyHomePage extends StatefulWidget {
MyHomePage();
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Color color = Colors.orange;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: 200,
padding: EdgeInsets.all(32),
child: Stack(
fit: StackFit.expand,
clipBehavior: Clip.none,
alignment: AlignmentDirectional.topCenter,
children: [
Padding(
padding: EdgeInsets.only(top: 30),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20)),
child: ClipRRect(
child: Container(
color: Colors.blue,
)
),
),
),
Positioned(
top: 0,
child: Container(
width: 50,
height: 50,
child: FittedBox(
child: FloatingActionButton(
onPressed: () {
setState((){
color = (color == Colors.red) ? Colors.orange : Colors.red;
});
},
backgroundColor: color,
child: Icon(Icons.ac_unit),
),
),
),
)
],
),
),
);
}
}

Overflow visible in Expanded widget

My layout is a row with three expanded widgets.
I need overflow visible in one of the expanded widgets
This is the code
Row(
children: [
Expanded(
flex: 1,
child: Container(color: Colors.red,)
),
Expanded(
flex: 2,
child: Container(color: Colors.green,)
),
Expanded(
flex: 3,
child: Container(color: Colors.orange,)
),
],
),
Now I need to add circles like this, in of the Expanded widgets, that overflow the Expanded widget. I can't wrap the Expanded in an overflow box, so I am lost
Any help is appreciated.
Edit: I tried this with the third expanded, just to see if it will overflow, but it only overflows the SizedOverflowBox, no overflow over the Expanded
Expanded(
flex: 3,
child: SizedOverflowBox(
size: const Size(100.0, 100.0),
alignment: AlignmentDirectional.bottomCenter,
child: Container(height: 50.0, width: 1500.0, color: Colors.blue,),
),
),
It looks like it is not going to be possible
Can easily be achieved with a Stack. Please see the code below :
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 Stack(
children: [
Row(
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.red,
)),
Expanded(
flex: 2,
child: Container(
color: Colors.green,
)),
Expanded(
flex: 3,
child: Container(
color: Colors.orange,
)),
],
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
height: 25,
width: 25,
decoration:
BoxDecoration(color: Colors.black, shape: BoxShape.circle),
child: Center(
child: Container(
height: 15,
width: 15,
decoration: BoxDecoration(
color: Colors.white, shape: BoxShape.circle),
),
),
),
Container(
height: 25,
width: 25,
decoration:
BoxDecoration(color: Colors.black, shape: BoxShape.circle),
child: Center(
child: Container(
height: 15,
width: 15,
decoration: BoxDecoration(
color: Colors.white, shape: BoxShape.circle),
),
),
),
Container(
height: 25,
width: 25,
decoration:
BoxDecoration(color: Colors.black, shape: BoxShape.circle),
child: Center(
child: Container(
height: 15,
width: 15,
decoration: BoxDecoration(
color: Colors.white, shape: BoxShape.circle),
),
),
),
],
),
),
],
);
}
}
Please see the code for Text Bullet points overflowing.
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(
appBar: AppBar(),
body: MyWidget(),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.red.withAlpha(60),
child: OverflowBox(
alignment: Alignment.topLeft,
maxWidth: 400,
child: SizedBox(
width: 300,
child: Text(
'HelloHello 🅐',
style: TextStyle(color: Colors.white),
),
),
),
),
),
Expanded(
flex: 2,
child: Container(
color: Colors.green.withAlpha(60),
child: OverflowBox(
alignment: Alignment.topLeft,
maxWidth: 400,
child: SizedBox(
width: 300,
child: Container(
child: Text(
'\n\nHelloHello HelloHello 🅐\n\nHelloHello HelloHello 🅐\n\nHelloHello HelloHello 🅐\n\nHelloHello HelloHello 🅐',
style: TextStyle(color: Colors.white),
),
),
),
),
),
),
Expanded(
flex: 3,
child: Container(
color: Colors.purple.withAlpha(60),
child: OverflowBox(
alignment: Alignment.topLeft,
maxWidth: 400,
child: SizedBox(
width: 300,
child: Text(
'\n\n\n\n\n\n\n\n\n\n\n\Hello Hello Hello Hello Hello Hello ',
style: TextStyle(color: Colors.white),
),
),
),
),
),
],
);
}
}

How to make a container outside of parent container

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:

Flutter question: How to insert a column inside a row in Flutter

My aim is to reproduce this UI layout in Flutter:
So far I have produced a layout with the below code which is missing the green square below the yellow square in the middle of the screen. How can i include the green square below the yellow square and center them? Can this be done by inserting a column inside a row? Thanks.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 100.0,
color: Colors.red,
),
Container(
width: 100.0,
height: 100.0,
color: Colors.yellow,
),
Container(
width: 100.0,
color: Colors.blue,
),
],
),
),
),
);
}
}
Simply wrap the inner container with a Column widget.
Here you go
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
body: SafeArea(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 100.0,
color: Colors.red,
),
Column(
mainAxisAlignment:MainAxisAlignment.center ,
children: <Widget>[
Container(
width: 100.0,
height: 100.0,
color: Colors.yellow,
),
Container(
width: 100.0,
height: 100.0,
color: Colors.green,
),
],
),
Container(
width: 100.0,
color: Colors.blue,
),
],
),
),
),
);
}
}
import 'package:flutter/material.dart';
void main() => runApp(DiceApp());
class DiceApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.teal,
appBar: AppBar(
title: Text('Dice App'),
backgroundColor: Colors.teal,
centerTitle: true,
),
body: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
width: 100,
color: Colors.red,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 100,
width: 100,
child: Text('2'),
color: Colors.yellow,
),
Container(
height: 100,
width: 100,
child: Text('2'),
color: Colors.green,
),
],
),
Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Container(
width: 100,
// alignment: Alignment.center,
child: Text('1'),
color: Colors.blue,
),
])
],
),
),
);
}
}
This code gives you the same layout shown in this example
// Scroll Area
child: ListTile(
// Horizontal Row
title: Row(
children: <Widget>[
// First element in Row
ImageIcon(
AssetImage("assets/images/my_photo.png"),
),
// Second element in Row
Column(
children: [
child: Text('My name'),
Row(
children: [
child: Text('My title'),
],
Image.asset(
'assets/images/more.png',
),
),
], // End of Column children
), // End of Column
], // End of Row children
), // End of Row
), // End of Scroll
remember to add infinite height to the containers in red and blue
here is my flutter code that I made .
Create 04 containers and wrap the middle one in a column.
body: SafeArea(
child:Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Text(' Container 1'),
color: Colors.red,
width: 100.0,
height: double.infinity,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text(' Container 2'),
color: Colors.yellow,
width: 100.0,
height:100.0,
),
Container(
child: Text(' Container 3'),
color: Colors.green,
width: 100.0,
height:100.0,
),
],
),
Container(
child: Text('Container 4'),
color: Colors.blue,
width: 100.0,
height: double.infinity,
)
],
)),
),
);
}
}