In the turnary operator statement 5 == 5 ? print('Correct') : print('Wrong'), I want the statement to return nothing(You can see it's returning a print statement) if the evaluation of the initial expression turns false, how do I do that?
The actual code is given below, the return type cannot be null. So, that isn't an option
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(times[index]),
newMessagehasCome ? Icon(Icons.laptop) ?
],
)
You can do it like this if you project supports Dart 2.3.0 or later:
Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(times[index]),
if (newMessagehasCome)
Icon(Icons.laptop)
],
)
Related
This is my cardview code:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TitleText(text: "Item name mmmmmmmmm $index"),
SizedBox(height: 20.0),
Body1Text(text: "Discount mmmmmmmm",color: Colors.red,),
SizedBox(height: 5.0),
SubHeadText(text: "Price ,mmmmmmmmmmmmmmmmmm",color: Colors.red,)
],
),
Just Wrap you - Card with -Flexible Widget.
Row(
children: <Widget>[
Flexible(
child: Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Item name mmmasdaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaammmmmm"),
SizedBox(height: 15.0,),
Text(
"Discount mmmmmmmm",
),
SizedBox(height: 5.0,),
Text(
"Price ,mmmmmmmmmdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfdgfmmmmmmmmm",
)
],
),
),
),
],
),
You can use Wrap instead of Column.
For ex..
new Wrap(
spacing: 5.0,
runSpacing: 5.0,
direction: Axis.vertical, // main axis (rows or columns)
children: <Widget>[
TitleText(text: "Item name mmmmmmmmm $index"),
Body1Text(text: "Discount mmmmmmmm",color: Colors.red,),
SubHeadText(text: "Price ,mmmmmmmmmmmmmmmmmm",color: Colors.red,)
],
)
Simply use
isExpanded: true,
on DropdownButton.
Example
DropdownButton(
isExpanded: true,
items: data.map((item) {
return new DropdownMenuItem(
child: Text(item['name']),
value: item['id'].toString(),
);
}).toList(),
onChanged: (newVal) {
setState(() {
_mySelection = newVal;
});
},
value: _mySelection,
),
isExpanded: true, inside DropdownButtonFormField
You can use SizedBox instead of columns.
SizedBox(
child:new Wrap(
spacing: 5.0,
runSpacing: 5.0,
direction: Axis.vertical, // main axis (rows or columns)
children: <Widget>[
TitleText(text: "Item name mmmmmmmmm $index"),
Body1Text(text: "Discount mmmmmmmm",color: Colors.red,),
SubHeadText(text: "Price ,mmmmmmmmmmmmmmmmmm",color: Colors.red,)
],
),
),
Try this, it works for me:
Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child:Text('')
),
],
),
looks like your using static padding and sizing. This can be a problem when it comes to rendering your project on other devices aside from this one. I'd strongly recommend using one of the following:
SingleChildScrollView under a LayoutBuilder.
Instead of a column, try using a container including a custom height and width
In case you decided to set custom padding, alternatively, you can use MediaQueryData.
I'd recommend using the first one for starters, I would be easier. The second method is for calculating the width and height of the screen and adjusting.
I think there are other useful answers too. I am sharing my experience on this issue for those who are new and are trying to find multiple ways to fix this issue.
In my case Text() was inside Column() and I fix this issue by setting textDirection property as below:
textDirection: TextDirection.ltr
You can set the value depending on your needs.
In this directory your_project/android/src/main there is AndroidManifest.xml file, delete it and the error will be solved.
I saw some places a block of code assigned to a widget or to variable, what is the difference between creating both?
a sample code below:
//creating new widget
Widget ratingSection = Container(
padding: const EdgeInsets.all(20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ ..............
],
),
);
//creating new var
var ratingSectionVar = Container(
padding: const EdgeInsets.all(20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ ..............
],
),
);
return MaterialApp( ......
....
body: ListView(
children: <Widget>[
ratingSection,
ratingSectionVar,
],
),
),
);
Dart provide something called Type inference
The analyzer can infer types for fields, methods, local variables, and
most generic type arguments. When the analyzer doesn’t have enough
information to infer a specific type, it uses the dynamic type.
so when you create variables using the var keyword you get the type from the right side of the assignment statement
so var ratingSectionVar = Container()
means that the Type of ratingSectionVar is Container which is also derived from Widget class
I don't know why the dart compiler shows me an error in my code. What is that actually means? Thanks.
Source:
const SliverAppBar(
pinned: true,
expandedHeight: 300.0, // TODO: check out later
flexibleSpace: FlexibleSpaceBar(
title: new Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text('_SliverAppBar'),
Text('subtitle'),
],
),
background: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Info'),
],
)),
I have commented the original answer that lead to that problem. But here is why:
As #aziza pointed, you have instantiated your SliverAppBar with the const keyword. Therefore, all properties should be instantiated with const.
In your case, just changing from new Column to const Column would solve the problem, but, dart 2 can infer how it will instantiate the class. Just omit the new and const keywords, and let dart do the work.
I have a Stack widget with some children. One of those children is an Align which has a Container child which has a Column child. This Column has some children. Now, in my Stack, the Column is way bigger than it should be (it fills the complete screen) although a third of that space would be enough. I'd like to know how I can use something like Flexible so I can use the fit property to tighten up the space. Is this possible?
Here is a representation of my code:
new Stack(
children: <Widget>[
new Container(...),
new Align( // This takes up way more space than needed!
alignment: new Alignment(1.0, 1.0) // This widget should be displayed at the bottom
child: new Container(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget> [
new Container(child: new Center(child: new Text("text"))),
new Row(children: [...]),
new Row(children: [...]),
new Row(children: [...]),
new Row(children: [...]),
]
)
)
)
]
)
I tried playing around with the fit property but without any luck.
By default, Column has mainAxisSize defaulting to MainAxisSize.max.
Which means that Column will take all the available height.
In your case, you don't want that. So you can set that value to MainAxisSize.min, which will do the opposite : fit it's children.
The end result is :
child: new Stack(
children: <Widget>[
new Container(...),
new Align(
alignment: Alignment.bottomCenter,
child: new Container(
color: Colors.purple,
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[new Text("foo"), new Text("hello world")],
),
),
)
],
),
I am a complete newbie in Flutter, but I already like it.
The question is:
why is the column not expanding to the full height?
code is on gist.github
Using CrossAxisAlignment.stretch works for me:
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text("Some title"),
Text("Address"),
Text("Description")
],
),
A Column widget is flexible - it will try to take up as much space as it needs but no more. If you want it to take up all availible space, then wrap it in an Expanded widget.
createChildren() {
return [
Image.network(
testImg,
height: imageSize,
width: imageSize,
),
Padding(padding: EdgeInsets.only(left: 16.0)),
Expanded(child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
boldText("Some title"),
Text("Address"),
Text("Description")
],
),
),
];
}
Wrapping the column with a row with MainAxisSize.max
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Column(
children: <Widget>[
_someWidget(),
_someWidget(),
_someWidget(),
],
),
],
)
Don't use flex=1 inside Column, here you can see example
children: [
MyWidget(),
Expanded(
child: MyWidget(),
),
MyWidget(),
],
Based on your layout I assume that your Column is nested inside a Row. Wrap your Row with IntrinsicHeight and your Column will automatically expand its height to be the same as the Row's tallest child:
IntrinsicHeight(
child: Row(
children: [
Container(width: 40, height: 40, color: Colors.blue),
// tallest child
Container(width: 40, height: 60, color: Colors.blue),
// height will be 60
Column(
...
),
],
),
)
You can wrap the Column inside an infinite width SizedBox.
SizedBox(
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [ ... ]
)
)
The above code snippet will place the children at the center of the SizedBox. It'll work especially well if you want to use a Column as the body of your Scaffold. In that case children will show up at the center of the screen.
The code linked in the question is no longer available, so I can't give you a full working example for your case. However, one thing that will likely work is using mainAxisAlignment as follows:
Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
Text("Some title"),
Text("Address"),
Text("Description")
],
),
(mainAxisSize: MainAxisSize.max is actually the default value, butthis only works if you do not set it to MainAxisSize.min, so I included it here.)
Setting mainAxisAlignment to MainAxisAlignment.spaceAround or MainAxisAlignment.spaceEvenly will also expand the column, but spacing between/around children will be handled differently. Try it out and see which you like best!
This will cause the column to take all the space it can - if it still doesn't expand then that is due to its parent widget, and we can no longer access the code so I can't speak to that.