Flutter infinite width exception - flutter

I want to align my transaction title and date to the left but when I give the parent container the full width of the row i.e. double.infinity, the following exception is thrown "BoxConstraints forces an infinite width". What could be the possible reason?
body: new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
new Card(child: new Text("CHART!")),
new Column(
children: [
...transactions.map((transaction) {
return new Card(
child: new Row(
children: [
new Container(
padding: EdgeInsets.all(10),
decoration: new BoxDecoration(
border: Border.all(
color: Colors.purple,
width: 2,
),
),
margin: EdgeInsets.symmetric(
vertical: 10,
horizontal: 15,
),
child: new Text(
transaction.amount.toString(),
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.purple),
),
),
new Column(
children: [
new Container(
width: double.infinity,
child: new Text(
transaction.title,
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 17),
),
),
new Container(
child: new Text(
transaction.date.toString(),
style: new TextStyle(color: Colors.grey),
),
),
],
)
],
),
);
})
],);

The Row() widget does not have a width property, meaning that you should put the Row() inside another Container(), so that the row knows what its constraints are, like this:
new Row(
children: [
new Container(
padding: EdgeInsets.all(10),
decoration: new BoxDecoration(
border: Border.all(
color: Colors.purple,
width: 2,
),
),
margin: EdgeInsets.symmetric(
vertical: 10,
horizontal: 15,
),
child: new Text(
transaction.amount.toString(),
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.purple),
),
),
new Column(
children: [
new Container(
width: MediaQuery.of(context).size.width,,
child: new Text(
transaction.title,
style: new TextStyle(
fontWeight: FontWeight.bold, fontSize: 17),
),
),
new Container(
child: new Text(
transaction.date.toString(),
style: new TextStyle(color: Colors.grey),
),
),
],
)
],
Note: the new keyword is optional. Furthermore, setting a width to infinity will only cause problems, since it will always go out of the screen, creating an overflow.

The problem is You are setting stretch for crossAxisAlignment, which gives infinite width to the column. You can provide constraint to the Container, through width property by setting
width: MediaQuery.of(context).size.width
This should work.

Related

Dart Flutter bears writing

I placed a post like this. When the text did not fit, it did not pass when it should have been on the bottom line.
Code:
body: Container(
padding: EdgeInsets.all(10),
child: Container(
height: 150,
decoration: BoxDecoration(
color: Colors.black
),
padding: EdgeInsets.all(10),
child: Container(
height: 180,
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 2),
),
child: Row(
children: [
Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Soru sor, kazan", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),),
Text("SorSor! uygulaması ile soru sor, para kazan! Tek amacın yaşadığın sorunları soru olarak açmak ve diğer kullanıcıların sorularını yanıtlamak.", style: TextStyle(fontSize: 16, color: Colors.white),),
],
),
),
],
),
),
)
)
I want it to go to the bottom line when there is no space left. How can I do it?
Just remove the Row widget that you have above the Padding and the issue will gone.
Container(
height: 180,
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 2),
),
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Soru sor, kazan",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white),
),
Text(
"SorSor! uygulaması ile soru sor, para kazan! Tek amacın yaşadığın sorunları soru olarak açmak ve diğer kullanıcıların sorularını yanıtlamak.",
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
),
),

how to implement textfield inside tile flutter

hi i need to design this page
but i'm new in flutter so i'm face difficult to manage to do it
here is my try
Container (
child: Column (
children: <Widget> [
Container(
margin: EdgeInsets.all(20),
child: Text(
"Source Account",
style: TextStyle(fontSize: 30,
color: Color(0xff8aa1ba)),
),
),
Container(
height: 100,
child: Card(
color: Color(0xff343b4b),
clipBehavior: Clip.antiAlias,
elevation: 10,
margin: EdgeInsets.all(7),
child: Column(
children: <Widget>[
ListTile(
title: Text("Main Account 123456",
style: TextStyle(
color: Colors.white,
fontSize: 15, fontWeight: FontWeight.bold),textAlign: TextAlign.left,),
subtitle: Text(" Available Balance \n 10.000.9 KWD" ,
style: TextStyle(
color: Colors.white,
fontSize: 15, fontWeight: FontWeight.bold),textAlign: TextAlign.right,),
),
],
),
),
),
],
),
)
so can any one help me to implement this or give me an idea of how to implement a textfield inside a list tile or card
Put a Row inside of your title. So replace the your code with something like this:
ListTile(
title: Row(
children: <Widget>[
Expanded(
child: TextField(
// your TextField's Content
),
),
],
),
Card(color: Colors.grey, child: Container(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 8), child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Loans", style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500,),),
Column(children: [
Icon(Icons.add),
Text("Amount", style: TextStyle(color: Colors.white,),)
])
]
),
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(2),
),
padding: EdgeInsets.symmetric(horizontal: 16),
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
),
style: TextStyle(color: Colors.white),
textAlign: TextAlign.end,
),
)
]),
),)

Container fill width of Column

I have a row inside this a column, column children are a two text, first text widget I put inside a container, I want the container background fill dull width of column. but the background only seen in where the text are present, left and right are empty,
here is my code
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
_bookingData.bookingData[index].paymentStatusFe =="Paid" ?
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.deepPurpleAccent)
),
child:
Column(
children: <Widget>[
Container(
child: Text("Paid"),
color: Colors.red,
),
Padding(
padding: const EdgeInsets.all(2.0),
child: Text(
'AED' +
' ' +
_bookingData
.bookingData[
index]
.amount
.toString(),
style: TextStyle(
fontSize:
12,
fontFamily:
'Roboto Condensed',
color: Colors.deepPurpleAccent,
fontWeight:
FontWeight
.w600),
),
)
],
),
): Container(
// width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.red)
),
child:
Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Not Paid",
style: TextStyle(
backgroundColor: Colors.red,
color: Colors.white
),
),
Padding(
padding: const EdgeInsets.all(2.0),
child: Text(
'AED' +
' ' +
_bookingData
.bookingData[
index]
.amount
.toString(),
style: TextStyle(
fontSize:
12,
fontFamily:
'Roboto Condensed',
color: Colors.red,
fontWeight:
FontWeight
.w600),
),
)
],
),
),
],
),
Row is put inside a Listview. So I want to have red in background for paid text, but red is only coming start of paid and till where it ends, not the entire column.
You have to do two changes in your code
1) Add your root container in within expanded widget
2) In your inner container set width:double.infinity
Row(mainAxisSize: MainAxisSize.max, children: <Widget>[
// first change
Expanded(
flex: 1,
child: Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.deepPurpleAccent)),
child: Column(
children: <Widget>[
Container(
//second change
width: double.infinity,
child: Text("Paid"),
color: Colors.red,
),
Padding(
padding: const EdgeInsets.all(2.0),
child: Text(
'AED TEST',
style: TextStyle(
fontSize: 12,
fontFamily: 'Roboto Condensed',
color: Colors.deepPurpleAccent,
fontWeight: FontWeight.w600),
),
)
],
)),
),
]);

How can i make my Scaffold widget scrollable so i would not have a "Bottom overflowed by xx Pixels" Error

I have a flutter app, and i am trying to make it scrollable, because i am getting a
"Bottom overflowed by 34 Pixels", which i am sure is caused when my widget scrolls below the apportioned height of the screen, how can i solved this issue:
i followed this question to solve the issue, but noting happened :
this is my code:
return ListView(
children: <Widget>[
new Container(
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
color: Colors.white,
image: DecorationImage(
colorFilter: new ColorFilter.mode(
Colors.black.withOpacity(0.3), BlendMode.dstATop),
image: AssetImage('images/space.gif'),
fit: BoxFit.cover,
),
),
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 200.0, left: 40.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Start",
style: TextStyle(
decoration: TextDecoration.none,
color: Colors.black,
fontSize: 30.0,
),
),
Text(
"Your",
style: TextStyle(
decoration: TextDecoration.none,
color: Colors.black,
fontSize: 30.0,
),
),
Text(
"Adventure",
style: TextStyle(
fontWeight: FontWeight.bold,
decoration: TextDecoration.none,
color: Colors.black,
fontSize: 30.0,
),
),
],
),
),
new Container(
width: MediaQuery.of(context).size.width,
margin:
const EdgeInsets.only(left: 30.0, right: 30.0, top: 100.0),
alignment: Alignment.center,
child: new Row(
children: <Widget>[
new Expanded(
child: new RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
color: Colors.blue,
onPressed: () => gotoSignup(),
child: new Container(
padding: const EdgeInsets.symmetric(
vertical: 20.0,
horizontal: 20.0,
),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Expanded(
child: Text(
"SIGN UP",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
],
),
),
),
),
],
),
),
new Container(
width: MediaQuery.of(context).size.width,
margin:
const EdgeInsets.only(left: 30.0, right: 30.0, top: 30.0),
alignment: Alignment.center,
child: new Row(
children: <Widget>[
new Expanded(
child: new RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
color: Colors.redAccent,
onPressed: () => gotoLogin(),
child: new Container(
padding: const EdgeInsets.symmetric(
vertical: 20.0,
horizontal: 20.0,
),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Expanded(
child: Text(
"LOGIN",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
],
),
),
),
),
],
),
),
],
),
)
],
);
Solved this by removing the height: MediaQuery.of(context).size.height, attribute in the first container widget

Flutter - Alignment and Wrapping issues

I'm trying to build a ListView, where the tiles look like this:
However, I'm facing some issues here.
So the item layout is
#override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: _onTap,
child: new Card(
elevation: 1.0,
color: Colors.white,
margin: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 15.0),
child: new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(
margin: const EdgeInsets.only(right: 15.0),
width: 100.0,
height: 130.0,
padding: const EdgeInsets.symmetric(
vertical: 10.0, horizontal: 5.0),
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
image: new DecorationImage(
fit: BoxFit.cover,
image: new NetworkImage(
"https://image.tmdb.org/t/p/w500" +
widget.movie.posterPath
)
)
),
),
new Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Expanded(
flex: 0,
child: new Container(
margin: const EdgeInsets.only(
top: 12.0, bottom: 10.0),
child: new Text(widget.movie.title,
style: new TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
),
new Expanded(
flex: 0,
child: new Text(
widget.movie.voteAverage.toString(),
style: new TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.grey[600]
),
),
),
],
),
new Container(
child: new Text(widget.movie.overview,
softWrap: true,
overflow: TextOverflow.ellipsis,
maxLines: 3,
),
),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: new Row(
children: <Widget>[
new Text("Released"),
new Container(
margin: const EdgeInsets.only(left: 5.0),
child: new Text(widget.movie.releaseDate)
),
],
),
),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: new Row(
children: <Widget>[
new Text("Rating"),
new Container(
margin: const EdgeInsets.only(left: 5.0),
child: new Text(widget.movie.voteAverage.toString())
),
],
),
),
],
)
],
),
),
),
);
}
The layout hierarchy, as you can see in the image above, is
Row -> Image, Column -> (Row -> (Title, Rating)), Description, Text,
Text
First Issue
The first issue is that I'm unable to align the Rating to the right within the Title, Rating row. So I want the title to be left aligned, and rating to be right aligned. For this, I tried to give a flex rating to both widgets, but as soon as I do that, both disappear. The only way I can think of is to give the Title a width, but that's a very hacky way and will break on most devices.
Second Issue
I want the Description to fit within the bounds of the Card. However, unless I define a specific width to the Container for Description, I'm unable to constrain it within the bounds of the screen. I tried encasing it within an Expanded, Flex and Flexible with various flex values, but no luck. I tried to use the softWrap property of Text widget, but again no luck. As soon as I wrap the Text in any of those widgets, it disappears. In the above code I've wrapped the Text in a Container, and set a width to it. This works, but again, it's very hacky, and will look different on different device, plus will break on a few.
Any help on these issues would be great.
After a lot of trial and error, I finally managed to get what I wanted.
My layout is like this:
#override
Widget build(BuildContext context) {
return new GestureDetector(
onTap: _onTap,
child: new Card(
elevation: 1.0,
color: Colors.white,
margin: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 14.0),
child: new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(
margin: const EdgeInsets.only(right: 15.0),
width: 100.0,
height: 150.0,
child: new Image.network(
"https://image.tmdb.org/t/p/w500" +
widget.movie.posterPath),
),
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Row(
children: <Widget>[
new Expanded(
child: new Container(
margin: const EdgeInsets.only(
top: 12.0, bottom: 10.0),
child: new Text(widget.movie.title,
style: new TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
),
new Container(
margin: const EdgeInsets.only(right: 10.0),
child: new Text(
widget.movie.voteAverage.toString(),
style: new TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.grey[600]
),
),
),
],
),
new Container(
margin: const EdgeInsets.only(right: 10.0),
child: new Text(widget.movie.overview,
style: new TextStyle(
fontSize: 16.0,
),
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
),
new Container(
margin: const EdgeInsets.only(top: 10.0),
child: new Row(
children: <Widget>[
new Text("RELEASED",
style: new TextStyle(
color: Colors.grey[500],
fontSize: 11.0,
),
),
new Container(
margin: const EdgeInsets.only(left: 5.0),
child: new Text(widget.movie.releaseDate)
),
],
),
),
],
),
),
],
),
),
);
}
What I had to do was make the second child of my top level row an Expanded widget. So as you can see in the code, I put my column (after the image) within an Expanded.
The problem that was happening before was that Row has infinite bounds, so making my text expanded within a row's column was not making any difference to it.
However, when I put the entire column into an expanded, it was bound by the screen width. That is what needs to be done.
The final output, with the above code, looks like this.
Here is the solution of your both the problem.
1) You just have to assign mainAxisAlighment.spaceBetween.
And you will good to go.
Below is the sample code which is solving your both the problems.
new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Container(
margin: const EdgeInsets.only(
top: 12.0, bottom: 10.0, left: 10.0),
child: new Text(
"Avengers: Infinity war",
style: new TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.bold,
),
),
),
new Container(
margin: const EdgeInsets.all(10.0),
child: new Text(
"8.6",
style: new TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.grey[600]),
),
)
],
),
new Container(
margin: const EdgeInsets.only(left: 10.0, right: 10.0),
child: new Text(
"Iron Man, Thor, the Hulk and the rest of the Avengers unite to battle their most powerful enemy yet -- the evil Thanos. On a mission to collect all six Infinity Stones, Thanos plans to use the artifacts to inflict his twisted will on reality.",
softWrap: true,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
)
],
)