Stack but one of the elements should use Expanded () [closed] - flutter

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am creating a container that should take up all available Space in width thats possible. However, I need to position an element on it that clips out of it, so I am using a Stack for it.
Using an Expanded on the lower stacked element causes an exception and also using a stack just with the positioned Item also causes an exception because the stacked widget has nowhere to anchor to.
Any Ideas how to solve this?
double.maxFinite as width does not work either
Using an expanded around the Containers looks like this:
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Stack(
overflow: Overflow.visible,
children: [
Container(
height: 100,
width: 100,
padding: EdgeInsets.only(bottom: 10, right: 5),
child: Container(
decoration: BoxDecoration(color: Colors.blue, borderRadius: BorderRadius.all(Radius.circular(10))),
padding: EdgeInsets.all(10), // Padding for content to Container Border
child: Text("Fruit"),
),
),
Positioned(
top: -8,
left: 10,
child: Container(
//padding: EdgeInsets.only(bottom: 10, right: 5),
child: Container(
decoration: BoxDecoration(color: Colors.green, borderRadius: BorderRadius.all(Radius.circular(10))),
padding: EdgeInsets.all(5), // Padding for content to Container Border
child: Text("Fruit"),
),
),
),
],
),
Stack(
overflow: Overflow.visible,
children: [
Expanded(
child: Container(
height: 100,
//width: MediaQuery.of(context).size.width/2,
padding: EdgeInsets.only(bottom: 10, right: 5),
child: Container(
decoration: BoxDecoration(color: Colors.blue, borderRadius: BorderRadius.all(Radius.circular(10))),
padding: EdgeInsets.all(10), // Padding for content to Container Border
child: Text("Fruit"),
),
),
),
Positioned(
top: -8,
left: 10,
child: Container(
//padding: EdgeInsets.only(bottom: 10, right: 5),
child: Container(
decoration: BoxDecoration(color: Colors.green, borderRadius: BorderRadius.all(Radius.circular(10))),
padding: EdgeInsets.all(5), // Padding for content to Container Border
child: Text("Fruit"),
),
),
),
],
),
],
);

Try fit: StackFit.expand,
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: SafeArea(
child: MyHomePage(),
),
),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Container(
height: 100,
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(child: _card()),
Expanded(child: _card()),
],
),
);
}
Widget _card() {
return Stack(
fit: StackFit.expand,
overflow: Overflow.visible,
children: [
Container(
padding: EdgeInsets.only(bottom: 10, right: 5),
child: Container(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
padding: EdgeInsets.all(10),
child: Text("Fruit"),
),
),
Positioned(
top: -8,
left: 10,
child: Container(
//padding: EdgeInsets.only(bottom: 10, right: 5),
child: Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
padding: EdgeInsets.all(5),
child: Text("Fruit"),
),
),
),
],
);
}
}

Related

how to add margin in multiple Container at one time in flutter

how to add margin in tow or more Container at one time
child: Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 100,bottom: 100),
child: Row(
children: [
Expanded(
child: Container(
margin: new EdgeInsets.all(10),
decoration: BoxDecoration(color: Colors.blueAccent),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(color: Colors.amber),
),
),
how to add margin in multiple container at one times or container warped in margin chilled
To do this you have to create a separate reusable widget like this:
class ReusableContainer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(10),
decoration: const BoxDecoration(color: Colors.blueAccent),
);
}
}
And then use that ReusableContainer widget like this:
Padding(
padding: const EdgeInsets.only(top: 100, bottom: 100),
child: Row(
children: [
Expanded(
child: ReusableContainer(),
),
Expanded(
child: ReusableContainer(),
),
],
),
),

Widgets not showing up in flutter

Im trying to do the layout below with flexible widgets so when the screen size changes the layout stays about the same but when I put a flexible widget around a column anything that I put in the column in not visible. What am I doing wrong?
Wanted Outcome
What I have
My code
class _HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.black,
),
body: SafeArea(
child: Column(
children: [
Flexible(
flex: 9,
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.grey,
),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(),
),
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(),
),
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(),
),
],
),
),
Flexible(
flex: 1,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.white),
),
)
],
),
),
);
}
}
Finish your work, you will get the thing you have asked
Provide child on container
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Colors.grey,
),
child: SizedBox(
height: 50,
width: double.infinity,
),
),
),

Flutter expand column inside row

I am trying to create this design.
.
My code
Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"${DateFormat("hh:mm aa").format(DateTime.parse(requestDetails.goTime))}",
style: TextStyle(fontSize: 12),
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 3),
width: 8,
height: 8,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(15.0)),
),
Container(
margin: EdgeInsets.only(top: 3),
width: 4,
height: 4,
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(15.0)),
),
Container(
margin: EdgeInsets.only(top: 3),
width: 4,
height: 4,
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(15.0)),
),
Container(
margin: EdgeInsets.only(top: 3),
width: 4,
height: 4,
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.circular(15.0)),
),
],
),
Flexible(
child: Text(
"${requestDetails.goAddress} ${requestDetails.goAddress}${requestDetails.goAddress}",
overflow: TextOverflow.clip,
style: TextStyle(fontSize: 14),
),
),
],
)
All I got is this. I want that dots to expand as address lines increases. Or is there a better approach in implementing the same. Is there a plugin to help this screen? This whole widget comes inside a ListView widget. Trying to create this for almost 4 hours from now. Please help.
Wrap your Row with IntrinsicHeight widget and then wrap your widget that makes this dots shape with the Expanded widget.
So your item will be like this:
Widget _buildItem(String text) {
return IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [Text('01:59 AM'), Text('07:30 PM')]),
Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 7,
height: 7,
decoration: BoxDecoration(
color: Colors.green,
shape: BoxShape.circle,
),
),
Expanded(
child: Container(width: 2, color: Colors.grey),
),
Container(
width: 7,
height: 7,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
),
],
),
Text(text)
],
),
);
}
And here is the output UI:
The full code example:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
padding: EdgeInsets.all(8),
children: [
_buildItem('''texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext'''),
Divider(
color: Colors.grey,
thickness: .5,
),
_buildItem('''texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext'''),
Divider(
color: Colors.grey,
thickness: .5,
),
_buildItem('''texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext
texttexttexttexttexttexttexttexttexttexttexttexttexttext'''),
Divider(
color: Colors.grey,
thickness: .5,
),
_buildItem(
'''texttexttexttexttexttexttexttexttexttexttexttexttexttext'''),
],
));
}
Widget _buildItem(String text) {
return IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [Text('01:59 AM'), Text('07:30 PM')]),
Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 7,
height: 7,
decoration: BoxDecoration(
color: Colors.green,
shape: BoxShape.circle,
),
),
Expanded(
child: Container(width: 2, color: Colors.grey),
),
Container(
width: 7,
height: 7,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
),
],
),
Text(text)
],
),
);
}
}
This code snipped with a little bit of styling should achieve the design from the image. Good luck. :)
import 'package:flutter/material.dart';
void main() {
runApp(
MyApp(),
);
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: true,
home: Scaffold(
body: SafeArea(
child: ListView(
children: <Widget>[
getCard(),
getCard(),
getCard(),
getCard(),
getCard(),
getCard(),
getCard(),
getCard(),
],
))));
}
getCard() {
return Container(
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Color.fromRGBO(
0, 64, 101, 0.15),
spreadRadius: 1,
blurRadius: 8,
offset: Offset(0,
2), // changes position of shadow
),
]),
padding: EdgeInsets.all(15),
height: 90,
width: double.infinity,
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(
left: 5, right: 5),
child: Column(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: <Widget>[
Text("01:53PM"),
Text("01:53PM"),
// Text(
// "7/1, 2nd block more adress etc."),
],
)),
Padding(
padding: EdgeInsets.only(
left: 5, right: 5),
child: Column(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: <Widget>[
Padding(
padding: EdgeInsets.only(
top: 3),
child: getDot(true)),
getDot(false),
getDot(false),
getDot(false),
getDot(false),
Padding(
padding: EdgeInsets.only(
bottom: 3),
child: getDot(true)),
],
)),
Padding(
padding: EdgeInsets.only(
left: 5, right: 5),
child: Column(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: <Widget>[
Text(
"333 Prospect St, Niagara Falls, NY 14303"),
Text(
"333 Prospect St, Niagara Falls, NY 14303"),
],
))
],
));
}
Widget getDot(bool isBig) {
return Container(
margin: EdgeInsets.only(top: 3),
width: isBig ? 8 : 4,
height: isBig ? 8 : 4,
decoration: BoxDecoration(
color: Colors.green,
borderRadius:
BorderRadius.circular(15.0)));
}
}
If you don't want to set a strictly fixed height (for example, a list can consist of either 1 element or 100 elements), you can do the following:
return ConstrainedBox(
constraints:
BoxConstraints(maxHeight: maxHeight),
child: ListView.builder(
shrinkWrap: true,
itemCount: count,
itemBuilder: (context, index) {
return Container(height: 20);
}));
In this case, you will only specify the maximum possible height, if your ListView is smaller, it will take its height, but it will not be greater than the height specified in constraints.

How to position elements independently using Stack, Row, and similar elements in Flutter

I have been trying to replicate these two designs in Flutter using Stack, Positioned, Row, and similar related widgets, however, I've been getting stuck in the same stages again and again. I either can't center the text or cannot position the back button correctly. Also, I am trying not to use fixed sizes/positions as this is supposed to be somewhat adaptable to different screen sizes.
Could someone point me in the right direction, of how to create this or similar layouts, which would be reused in other screens?
Example 1:
Example 2:
For your example numero 1, I came with this solution:
class DetailScreen extends StatefulWidget {
#override
_DetailScreenState createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
#override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: SafeArea(
child: Stack(
fit: StackFit.expand,
children: [
Container(
margin: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: Color(0xFF0B2746),
borderRadius: BorderRadius.circular(15),
),
child: Container(
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 18),
child: Text(
'Glossary',
style: TextStyle(
fontWeight: FontWeight.w800,
fontSize: 20,
),
),
)
],
),
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Card(
margin: const EdgeInsets.only(left: 0),
child: Padding(
padding: const EdgeInsets.all(12),
child: Icon(Icons.arrow_back_ios),
),
elevation: 6,
),
],
),
)
],
),
),
);
}
}
and the result is the following:
For your second example, I had to add extra logic:
class DetailScreen extends StatefulWidget {
#override
_DetailScreenState createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
#override
Widget build(BuildContext context) {
return Material(
color: Colors.white,
child: SafeArea(
child: Stack(
fit: StackFit.expand,
children: [
Container(
margin: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: Color(0xFF0B2746),
borderRadius: BorderRadius.circular(15),
),
child: Column(
children: [
Container(
height: 64.0,
width: double.infinity,
margin: const EdgeInsets.only(left: 54, right: 8, top: 8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: <Widget>[
SizedBox(width: 20),
Icon(Icons.train, size: 35),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 18),
child: Text(
'Metro',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.w800,
fontSize: 20,
),
),
),
),
Icon(Icons.arrow_drop_down, size: 35),
SizedBox(width: 20),
],
),
),
Expanded(
child: Container(
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
)),
)
],
),
),
Padding(
padding: const EdgeInsets.only(top: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Card(
margin: const EdgeInsets.only(left: 0),
child: Padding(
padding: const EdgeInsets.all(12),
child: Icon(Icons.arrow_back_ios),
),
elevation: 6,
),
],
),
)
],
),
),
);
}
}
and the result for this one is the follwing:

How to extend the full width of the child in Flutter?

I have a next markup:
The red block is ListView with a horizontal orientation.
The purple block is just Container for all content.
The white is Container as well but with paddings.
I want to expand the width of the red block on full width. How can I do it? It should look like this:
Code of the markup:
class FifaApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: Container(
padding: EdgeInsets.symmetric(
vertical: 60.0,
horizontal: 30.0,
),
color: Color(0xFFffffff),
alignment: Alignment.topLeft,
child: Container(
color: Colors.purple,
margin: EdgeInsets.symmetric(vertical: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Math Reports"),
Container(
color: Colors.red,
padding: EdgeInsets.symmetric(vertical: 10.0),
height: 170.0,
child: ListView(
scrollDirection: Axis.horizontal,
// children: renderItems(), // the example of code without green blocks
),
),
],
),
),
),
);
}
}
You can use Stack as below
class FifaApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.ltr,
child: Container(
padding: EdgeInsets.symmetric(
vertical: 60.0,
),
color: Color(0xFFffffff),
alignment: Alignment.topLeft,
child: Stack(
children: <Widget>[
Container(
color: Colors.purple,
margin: EdgeInsets.symmetric(horizontal: 30),
),
Padding(
padding: const EdgeInsets.only(left: 30.0),
child: Text(
"Math Reports",
style: TextStyle(color: Colors.white),
),
),
Container(
height: 170,
color: Colors.red,
margin: EdgeInsets.only(top: 30),
child: ListView(
scrollDirection: Axis.horizontal,
),
)
],
),
),
);
}
}