Flutter set maxWidth on Stack with constrainedBox - flutter

in my simple implementation of Card don't have maxWidth and when i rotate application that fills the width and i'm trying to avoiding that and setting `maxWith:300.0', my code doesn't work
return Container(
color: Color(0xffeeeeee),
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 300),
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Container(
height: 200.0,
color: Colors.indigo,
),
Positioned(
width: MediaQuery.of(context).size.width - 20,
height: MediaQuery.of(context).size.height / 1.3,
left: 10.0,
top: 95.0,
child: Card(
elevation: 2.0,
color: Colors.white,
child: Container(
decoration: BoxDecoration(color: Colors.white12),
),
)),
],
),
));

Related

Floating picture outside its container in flutter

floating image
Container(
clipBehavior: Clip.none,
child: Stack(
fit: StackFit.passthrough,
children: <Widget>[
//Stack helps to overlap widgets
Positioned(
top: 80,
left: 50, //position of the widget
child: Container(
clipBehavior: Clip.none,
// height: 250,
// width: 250,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.orange
.withOpacity(0.5) //background color with opacity
),
child: Image.asset("assests/FT300.png"),
),
),
Positioned(
left: -80,
top: -50,
child: Container(
height: 180,
width: 220,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.redAccent.withOpacity(0.5),
),
),
),
Positioned(
//main content container postition.
child: Container(
height: 250,
alignment: Alignment.center,
child: Text(
"Stacked Containers Together",
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
textAlign: TextAlign.center,
),
),
)
],
),`
I would like to make the picture floating outside its container as shown in the picture I have tried to use a stack widget with
Clipbehaviour.none
but the picture still appears inside the container.
Is there anybody who has encountered this or have a solution to this?
Thanks
if you need the widget to be part of outside Stack, you need this:
Stack(
overflow: Overflow.visible,
children:
As the overflow is deprecated, the first solution to the given snippet is
Stack(
clipBehavior: Clip.none,
children:
Full snippet update:
Container(
clipBehavior: Clip.none,
color: AppRepoColors().appRedColor(),
child: Stack(
clipBehavior: Clip.none,
children: <Widget>[
//Stack helps to overlap widgets
Positioned(
top: 0,
left: 50, //position of the widget
child: Container(
clipBehavior: Clip.none,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.orange
.withOpacity(0.5) //background color with opacity
),
child: Image.asset("assests/FT300.png"),
),
),
Positioned(
left: -80,
top: -50,
child: Container(
height: 180,
width: 220,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.redAccent.withOpacity(0.5),
),
),
),
Positioned(
//main content container postition.
child: Container(
height: 250,
alignment: Alignment.center,
child: Text(
"Stacked Containers Together",
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
textAlign: TextAlign.center,
),
),
)
],
),
),

How to align widget under another widget in Flutter?

I have a widget meant to represent progress which goes
like this
I need to be able to put a name under a bubble, but when I do that like this:
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.green,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
child: Center(
child: Icon(
Icons.check,
color: Colors.green,
size: 30,
),
),
),
Text("Test label"),
],
),
this happens.
I also tried putting the bars in a row with the circle:
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
children: [
Container(
padding: EdgeInsets.symmetric(
vertical:
MediaQuery.of(context).size.height / 20,
),
height:
1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 16,
),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: mainColor,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
child: Center(
child: Icon(
Icons.check,
color: mainColor,
size: 30,
),
),
),
Container(
padding: EdgeInsets.symmetric(
vertical:
MediaQuery.of(context).size.height /20,
),
height:
1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 16,
child: Container(
color: grey,
),
),
],
),
Text("Test label"),
],
),
This is better, but still creates problems if the text is bigger
How can I align the text below the circle without causing problems with the lines?
The code for the entire widget (sorry that it's so long, don't know how to show it otherwise):
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: EdgeInsets.symmetric(
vertical:
MediaQuery.of(context).size.height / 20,
),
height:
1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 16,
),
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.green,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
child: Center(
child: Icon(
Icons.check,
color: Colors.green,
size: 30,
),
),
),
Text("Test label"),
],
),Container(
padding: EdgeInsets.symmetric(
vertical:
MediaQuery.of(context).size.height / 20,
),
height:
1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 16,
child: Container(
color: Colors.grey,
),
),
Container(
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height / 20,
),
height: 1 + MediaQuery.of(context).size.height / 10,
width: 3 / 2 * MediaQuery.of(context).size.width / 8,
child: Container(
color: Colors.grey,
),
),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.grey,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
),
Container(
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height / 20,
),
height: 1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 8,
child: Container(
color: Colors.grey,
),
),
Container(
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height / 20,
),
height: 1 + MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 8,
child: Container(
color: Colors.grey,,
),
),
Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
color: Colors.grey,,
width: 1.5,
),
),
height: MediaQuery.of(context).size.height / 10,
width: MediaQuery.of(context).size.width / 10,
)
],
),
One solution I would suggest is that try Wrapping Text with ConstrainedBox and Container and limit the max width.
For example -
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 50),
child: Container(
child: Text('Your Text'),
),
),
Thought that will move the text in the next line if it is too long to be placed in a single line.

How to Adjust container height according to the text height in flutter?

I am building a flutter application where I am trying to add a container and it's child text.
I want the container to take as much height as the text requires. But instead it takes the height specified.
Following is my code:
class ChangePassword extends StatefulWidget {
#override
_ChangePasswordState createState() =>
_ChangePasswordState();
}
class _ChangePasswordState extends State<ChangePassword> {
final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
key: _scaffoldkey,
drawer: Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
Container(
height: (MediaQuery.of(context).size.height * 0.23),
child: DrawerHeader(
child: Container(
padding:
EdgeInsets.symmetric(horizontal: 10, vertical: 10),
child: Row(
children: [
Container(
height: MediaQuery.of(context).size.width * 0.1,
width: MediaQuery.of(context).size.width * 0.1,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: Theme.of(context).primaryColor,
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(imageurl)),
boxShadow: [
BoxShadow(
blurRadius: 25,
color: Colors.black.withOpacity(0.2),
offset: Offset(0, 10),
)
],
),
),
Container(
height: MediaQuery.of(context).size.width * 0.15,
width: MediaQuery.of(context).size.width * 0.5,
padding: EdgeInsets.symmetric(horizontal: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
name,
style: TextStyle(
fontSize: 15,
color: Colors.lightBlue[900],
fontWeight: FontWeight.bold),
),
Text(email),
SizedBox(
height: 2,
),
Row(
children: [
Icon(
Icons.remove_red_eye_outlined,
size: 12,
),
Text(
'View Profile',
style: TextStyle(
fontSize: 12,
),
)
],
)
],
),
),
],
)),
decoration: BoxDecoration(
color: Colors.white,
),
),
),
],
),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.35,
child: Stack(children: [
Positioned(
height: MediaQuery.of(context).size.height * 0.23,
top: 0,
child: Padding(
padding: EdgeInsets.only(top: 50),
child: Container(
color: Color(0xff002060),
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.1,
child: Align(
alignment: Alignment.topCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
GestureDetector(
onTap: () {
_scaffoldkey.currentState.openDrawer();
},
child: new Container(
child: Icon(
Icons.menu,
color: Colors.white,
size: MediaQuery.of(context).size.height *
0.04,
),
padding: new EdgeInsets.all(7.0),
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.3,
),
Container(
height:
MediaQuery.of(context).size.height * 0.07,
child: Center(
child: Text(
'Profile',
style: TextStyle(
color: Colors.white,
),
),
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.3,
),
Icon(
Icons.search,
color: Colors.white,
size: MediaQuery.of(context).size.height * 0.04,
),
],
),
),
),
),
),
Positioned(
top: MediaQuery.of(context).size.height * 0.16,
left: MediaQuery.of(context).size.width * 0.03,
child: Row(
children: [
Container(
width: MediaQuery.of(context).size.height * 0.14,
height: MediaQuery.of(context).size.height * 0.14,
decoration: ShapeDecoration(
shape: CircleBorder(), color: Colors.white),
child: Padding(
padding: EdgeInsets.all(8.0),
child: DecoratedBox(
decoration: ShapeDecoration(
shape: CircleBorder(),
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://thumbor.forbes.com/thumbor/fit-in/416x416/filters%3Aformat%28jpg%29/https%3A%2F%2Fspecials-images.forbesimg.com%2Fimageserve%2F5f4ebe0c87612dab4f12a597%2F0x0.jpg%3Fbackground%3D000000%26cropX1%3D292%26cropX2%3D3684%26cropY1%3D592%26cropY2%3D3987',
))),
),
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.05,
),
Column(
children: [
Container(
height: MediaQuery.of(context).size.height * 0.08,
child: Text(
'Bill Gates',
style: TextStyle(
fontSize: 25,
color: Colors.white,
fontWeight: FontWeight.bold,
fontFamily: 'Calibri'),
),
),
],
)
],
),
),
Positioned(
top: MediaQuery.of(context).size.height * 0.24,
left: MediaQuery.of(context).size.height * 0.165,
child: FittedBox(
fit: BoxFit.fill,
child: Container(
height: MediaQuery.of(context).size.height * 0.09,
width: MediaQuery.of(context).size.width * 0.6,
child: Text(
'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
style: TextStyle(
color: Colors.black, fontFamily: 'Calibri'),
),
),
),
),
]),
),
],
)),
],
),
));
}
Following is the image:
I want the container that contains the text below 'Bill Gates' to be wrapped according to the information entered there.
The whole container should decrease as I am planning to add widgets below this customised appbar, so is there any way I can achieve this?
I tried to use flexible but it did not work. Can someone help me with this please?
Use fitted box:-
FittedBox(
fit:BoxFit.fitHeight,
child:Text("Your text"),
),
Do not provide the height of the container and it will adjust automatically.
#Ankit,
Culprit:
As you mentioned, the Container widget is intended to take the width and height you explicitly specify, so it does what you tell it to do.
What you should have done in the above case is avoid specifying the height for that Container which contains the Text widget as follows.
Change this code section from:
Positioned(
top: MediaQuery.of(context).size.height * 0.24,
left: MediaQuery.of(context).size.height * 0.165,
child: FittedBox(
fit: BoxFit.fill,
child: Container(
height: MediaQuery.of(context).size.height * 0.09,
width: MediaQuery.of(context).size.width * 0.6,
child: Text(
'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
style: TextStyle(
color: Colors.black, fontFamily: 'Calibri'),
),
),
),
),
To the following code, where I have removed specifying the height property of the container:
Positioned(
top: MediaQuery.of(context).size.height * 0.24,
left: MediaQuery.of(context).size.height * 0.165,
child: FittedBox(
fit: BoxFit.height,
child: Container(
width: MediaQuery.of(context).size.width * 0.6,
child: Text(
'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
style: TextStyle(
color: Colors.black, fontFamily: 'Calibri'),
),
),
),
),
By doing that, and wrapping the Container within a FittedBox, that varying text length Text widget will cause it to adjust its height accordingly.
Following part is important here:
child: FittedBox(
fit: BoxFit.height,

flutter: top of image deleted in stack widget

I am trying to use stack in my widget tree:
body: Center(
child: SingleChildScrollView(
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Container(
// card view
alignment: Alignment.center,
height: 200,
margin: EdgeInsets.only(
top: 20.0, bottom: 50.0, left: 50.0, right: 50.0),
decoration: BoxDecoration(
color: color_transparent_black,
borderRadius: BorderRadius.circular(14.0),
),
),
Positioned(
top: -60,
left: 0,
right: 0,
bottom: 0,
child: Align(
alignment: Alignment.topCenter,
child: Container(
width: width * 0.3,
height: height * 0.2,
child: CircleAvatar(
backgroundColor: Colors.transparent,
child: Image.asset("assets/images/ic_setting.png"),
),
),
),
),
],
),
),
),
);
And this result is this:
Why top of setting icon has been deleted?
I fixed the problem:
body: Center(
child: SingleChildScrollView(
child: Stack(
// overflow: Overflow.visible,
children: <Widget>[
Container(
// card view
alignment: Alignment.center,
height: 200,
margin: EdgeInsets.only(
top: 80.0, bottom: 50.0, left: 50.0, right: 50.0),
decoration: BoxDecoration(
color: color_transparent_black,
borderRadius: BorderRadius.circular(14.0),
),
),
FractionalTranslation(
translation: Offset(0.0, 0.0),
child: Align(
alignment: Alignment.topCenter,
child: Container(
width: width * 0.3,
height: height * 0.2,
child: CircleAvatar(
backgroundColor: Colors.transparent,
child: Image.asset("assets/images/ic_setting.png"),
),
),
),
),
],
),
),
),
);
and it is result:
I increases Top margin to top: 80.0 and replace Positioned with FractionalTranslation. Actually it works with Positioned too.
Its most likely because you set the top: -60 for the Positioned widget.
Edit 1:
Stop clipping by removing SingleChildScrollView
And you can center all children of Stack using alignment: Alignment.topCenter property of Stack widget itself.
child: Center(
child: Stack(
overflow: Overflow.visible,
alignment: Alignment.topCenter,
children: <Widget>[
Container(
height: 200,
...
),
Positioned(
top: -60,
...
child: Container(
...
),
),
),
]),

Flutter how can i round a container from middle

I have 2 containers I need to round 1 container from mid-bottom and other from mid-top so it will create a circle between 2 containers so I can add Text here. Can anyone please tell how it's possible
body: Container(
child: Column(
children: <Widget>[
SizedBox(height: (MediaQuery.of(context).size.height -
appBar.preferredSize.height - MediaQuery.of(context).padding.top) *
0.05,
),
Center(
child: Container(
width: MediaQuery.of(context).size.width * 0.9,
color: Colors.red,
height: (MediaQuery.of(context).size.height -
appBar.preferredSize.height - MediaQuery.of(context).padding.top) *
0.40,
child: Column(
children: <Widget>[
Text('10%'),
Text('Say goodbye')
],
),
),
),
Container(
height: (MediaQuery.of(context).size.height -
appBar.preferredSize.height - MediaQuery.of(context).padding.top) *
0.1,
child: Text('OR'),
),
Center(
child: Container(
width: MediaQuery.of(context).size.width * 0.9,
height: (MediaQuery.of(context).size.height -
appBar.preferredSize.height - MediaQuery.of(context).padding.top) *
0.40,
color: Colors.blue,
child: Column(
children: <Widget>[
Text('10%'),
Text('Say goodbye')
],
),
),
),
SizedBox(height: (MediaQuery.of(context).size.height -
appBar.preferredSize.height - MediaQuery.of(context).padding.top) *
0.05,
),
],
),
),
Like this
You can round a container by using a BoxDecoration and applying a BoxShape.circle to it:
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
AppBar appBar = AppBar(
title: Text('Demo'),
);
double stackHeight = MediaQuery.of(context).size.height - 105;
double stackWidth = MediaQuery.of(context).size.width;
return Scaffold(
appBar: appBar,
body: Stack(
children: [
Container(
height: stackHeight * 0.5,
width: stackWidth,
color: Colors.blue,
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: stackHeight * 0.5,
width: stackWidth,
color: Colors.red,
),
),
Align(
alignment: Alignment.center,
child: Container(
width: stackWidth,
height: stackHeight * 0.015,
color: Colors.black,
),
),
Align(
alignment: Alignment.center,
child: Container(
width: stackWidth,
height: stackHeight * 0.15,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black,
),
child: Align(
alignment: Alignment.center,
child: Text(
"OR",
style: TextStyle(color: Colors.white),
),
)
),
),
],
)
);
}
This produced the following screen:
you can use Stack Widget to place widgets
here is exactly what you need
body: Stack(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height,
color: Colors.amber,
child: Column(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.48,
color: Colors.blue,
),
Container(
height: MediaQuery.of(context).size.height * 0.04,
color: Colors.black,
),
Container(
height: MediaQuery.of(context).size.height * 0.48,
color: Colors.red,
)
],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Center(
child: Container(
height: MediaQuery.of(context).size.height * 0.20,
width: MediaQuery.of(context).size.width * 0.20,
decoration: BoxDecoration(
color: Colors.black, shape: BoxShape.circle),
),
),
],
),
Center(
child: Text(
"QR",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 24.0),
),
)
],
),
Hope it will help
As told before the solution may be the Stack Widget. Below a possible solution.
You can change the color of the middle part with the same color of the background in order to obtain the desired effect.
Stack(
alignment: Alignment.center,
children: <Widget>[
Column(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.9,
color: Colors.blue,
height: 200),
Container(
width: MediaQuery.of(context).size.width * 0.9,
height: 20,
color: Colors.black),
Container(
width: MediaQuery.of(context).size.width * 0.9,
color: Colors.red,
height: 200),
]),
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: Colors.black,shape: BoxShape.circle),
child: Center(child: Text("or")),
),
],
);