Prefer const literals as parameters of constructors on #immutable classes - flutter

I am following the example of this tutorial.
If I want to add two text components to a children array and I get the title warning.
For example:
Widget titleSection = Container(
child: Row(
//need to expand the rows on the column
children: [
Expanded(
child: Column(
children: [
const Text(
'Hello1',
),
const Text(
'Hello2',
),
],
)
],
),
);
What am I doing wrong?

In your case it should be
Widget titleSection = Container(
child: Row(
//need to expand the rows on the column
children: [
Expanded(
child: Column(
children: const [
Text(
'Hello1',
),
Text(
'Hello2',
),
],
),
)
],
),
);

Related

Align text based on bottom line like textAlign: TextAlign.bottom feature

Hi community, as you can see I would like to align text based on this red line, so if there is more text, they should go up, now i tried `textAlign: TextAlign.end // no align bottom? , but it seems not work for my case, plz help,Thank you!
return MaterialApp(
home:
Column(
children: [
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: const [
Text('asd asdk\n alksd\n alksd '),
Divider(
color: Colors.red,
)
],
),
),
const Expanded(child: SizedBox())
],
),
);
try this code:
Column(
children: [
Flexible(
child: SizedBox(height: MediaQuery.of(context).size.height,),
),
Text(...);
],
);

Flutter: how to make this part of the code scrollable?

I am working on a flutter app and I need to make a part of the code scrollable. The full code:
return Scaffold(
appBar: AppBar(
title: Text(_title),
),
body: Column(
children: [
Padding(
padding: EdgeInsets.only(bottom: 30),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
...
],
),
for (var day in data)
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
day.getDate(),
style: getDataLabelStyle(),
),
Text(
day.getStarting(),
style: getDataLabelStyle(),
),
Text(
day.getEnding(),
style: getDataLabelStyle(),
),
],
),
],
),
);
And this part should be scrollable:
for (var day in data)
Row(
...
],
),
Also you can see what part should be scrollable:
Thanks in advance
Use ColumnBuilder and then Wrap it with singleChildScrollView like this:
Expanded(
child: SingleChildScrollView(
child: Column(children: List.generate(data.lenght, (index){
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
day.getDate(),
style: getDataLabelStyle(),
),
Text(
day.getStarting(),
style: getDataLabelStyle(),
),
Text(
day.getEnding(),
style: getDataLabelStyle(),
),
],
);
}),),
),
)
You can achieve your task using the SingleChildScrollView Class.
For your given code you can achieve this by wrapping your row into Column and again wrap it with SingleChileScrollView.
The full solution is:
....
SingleChildScrollView(
child: Column(
children: [
for (var day in data)
Row(
children: [
.......
],
),
],
),
),
Wrap the part with SingleChildScrollView. and if you are using SingleChildScrollView inside a Column, do not forget to wrap that with Expanded.
example,
Expanded(
child: SingleChildScrollView(
child: Container(
child: yourCustomWidget()
),
),
),

How can i return a list of widgets using map function in flutter

I created a list of widgets...
`List<Widget> commWidgets = [
Card(
color: Colors.blueGrey[200],
child: Column(
children: <Widget>[
Text('Commodity 1'),
Row(
children: <Widget>[
Text('Opening Stock'),
Text('Bought/Sold'),
Text('Closing Stock')
],
)
],
),
),
Card(
color: Colors.blueGrey[300],
child: Column(
children: <Widget>[
Text('Commodity 2'),
Row(
children: <Widget>[
Text('Opening Stock'),
Text('Bought/Sold'),
Text('Closing Stock')
],
)
],
),
),
Card(
color: Colors.blueGrey[200],
child: Column(
children: <Widget>[
Text('Commodity 3'),
Row(
children: <Widget>[
Text('Opening Stock'),
Text('Bought/Sold'),
Text('Closing Stock')
],
)
],
),
),
];
`
and then i tried returning it using this line of code in my body property
Column(
children: commWidgets.map((commWidget) => Widget(commWidget)).toList(),
),
but it fails with the error:
Compiler message:
lib/main.dart:70:51: Error: The class 'Widget' is abstract and can't be instantiated.
children: commWidgets.map((commWidget) => Widget(commWidget)).toList(),
^^^^^^
Don't create a widget list like this by adding the same widget. instead, you can follow the steps below.
Create a model class for your data.
class DataClass {
final String labelOne;
final String labelTwo;
final String labelThree;
DataClass(
{required this.labelOne, required this.labelTwo, required this.labelThree});
}
Create a list using above class
List<DataClass> dataList = [
DataClass(
labelOne: 'AAA',
labelTwo: 'aaa',
labelThree: '1111',
),
DataClass(
labelOne: 'BBB',
labelTwo: 'bbb',
labelThree: '222',
),
DataClass(
labelOne: 'CCC',
labelTwo: 'ccc',
labelThree: '333',
),
];
Then You can map those objects to a Column, a Row, or even ListView / GridView
Sample Column
Column(
children: dataList.map((e) => Card(
color: Colors.blueGrey[300],
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Text(e.labelOne),
Text(e.labelTwo),
Text(e.labelThree)
],
)
],
),
)).toList(),
),
Sample GridView
GridView.count(
childAspectRatio: 1.2,
primary: false,
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 3,
children: dataList
.map((e) => Card(
color: Colors.blueGrey[300],
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Text(e.labelOne),
Text(e.labelTwo),
Text(e.labelThree)
],
)
],
),
))
.toList()),
You already have a list of widgets, the map function isn't applicable here
EDIT - Fixed the problem
The Column widget will automatically render the widgets from the list
Column (
children: commWidgets
)

How to fill contents inside wrap widget?

I want to move widgets to the new line automatically if it's overflow and Wrap widget is a good way for this.
so I have some wrap widgets which have width based on text length and some icons like:
Current actual:
I want to align 2 icons on the left and right side of the parent.
but as you can see the red-colored area of Row, I don't know how to make Row width match to the parent when Text widget has longer than Row (in the above case of the attached image, something?)
This is what I expect:
and my current code is:
Wrap(
direction: Axis.horizontal,
crossAxisAlignment: WrapCrossAlignment.start,
children: [
Container(
child: Column(
children: [
Text(
"hoge",
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.edit),
Icon(Icons.more_vert),
],
)
],
),
),
Container(
child: Column(
children: [
Text(
"too long text...............",
),
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Icon(Icons.edit),
Icon(Icons.more_vert),
],
)
],
),
),
Container(
child: Column(
children: [
Text(
"hoge",
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.edit),
Icon(Icons.more_vert),
],
)
],
),
),
],
),
How to achieve this?
IntrinsicWidth(
child: Column(
children: [
Text(
"too long text...............",
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Icon(Icons.edit),
Icon(Icons.more_vert),
],
)
],
),
),
Wrap your long text Column with IntrinsicWidth instead of normal Container Widget.moreover, remove the mainAxisSize constraint on the Row widget to make it's width same as Text widget.
https://api.flutter.dev/flutter/widgets/IntrinsicWidth-class.html
I think this is what you're looking for List of Containers side by side
I made it dynamic so that you can add new items if you want base on the list of Item.
class Item {
String text;
IconData icon1;
IconData icon2;
Color color;
Item({ this.text, this.icon1, this.icon2,this.color,});
}
We can make this dynamic by creating a List of Item
final List<Item> item = [
Item(
text: 'Item 3',
icon1: Icons.edit,
icon2: Icons.more_vert,
color: Colors.blue,
),
Item(
text: 'Item 2 This text is too long for the screen width..... screen width.....screen width.....screen width.....',
icon1: Icons.edit,
icon2: Icons.more_vert,
color: Colors.greenAccent,
),
Item(
text: 'Item 3',
icon1: Icons.edit,
icon2: Icons.more_vert,
color: Colors.amberAccent[100],
),
];
You might also want to look at Wrap Class
Wrap(
spacing: 8.0, // gap between adjacent chips
runSpacing: 4.0, // gap between lines
children: item.map((Item) {
return GestureDetector(
child: Container(
decoration: BoxDecoration(
color: Item.color,
),
margin: const EdgeInsets.all(15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(Item.text),
Wrap(
children: <Widget>[
Icon(Item.icon1),
Icon(Item.icon2),
],
)
],
),
),
onTap: () => {},
);
}).toList(),
),

Flutter invalid refrence to 'this' expression

i am trying to add item in list when i click on add button, all code is ok, but i am getting error invalid reference to this expression. i am using stateful widget.
List<Widget> _listSection = [];
body: Container(
child: Stack(
children: [
FloatingActionButton(
onPressed: () {
_listSection.add(
listSectionMethod(
"title three", "hello from click", Icons.forward),
);
setState(() {});
},
),
],
),
),
),
);
}
}
Widget listSection = Container(
margin: EdgeInsets.only(top: 210),
child: ListView(
children: [
Column(
children: [
Column(
children: this._listSection, // ----> ERROR HERE
),
],
),
],
),
);
List Section Method:
Card listSectionMethod(String title, String subtitle, IconData icon) {
return new Card(
child: ListTile(
title: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(subtitle),
trailing: Icon(
icon,
color: Colors.blue,
),
),
);
}
change this:
Widget listSection = Container(
margin: EdgeInsets.only(top: 210),
child: ListView(
children: [
Column(
children: [
Column(
children: this._listSection,
),
],
),
],
),
);
for this:
Widget listSection() {
return Container(
margin: EdgeInsets.only(top: 210),
child: ListView(
children: [
Column(
children: this._listSection,
),
],
),
);
}