how to set limit max widget in row flutter - flutter

I have controller generated like this
final inputs = List.generate(100, (i) => TextEditingController());
and the input inside row
Row: (
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.only(right: 3),
child: TextField(
autocorrect: false,
controller: inputs[1], <= here is controller define
)),
]);
if I use for loop then row input become 100 in single row
I want max 5 input in one row
Here is screenshot
how to achieve this
thank you

Try with GridView
GridView.count(
crossAxisCount: 5,
crossAxisSpacing: 4.0,
mainAxisSpacing: 8.0,
children: List.generate(inputs.length, (index) {
return Center(
child: ,// your widget
);
}
)

Related

Limiting the number of ListViewBuilder item in a row and go to next/new row

How can limit the number of Listviewbuilder items in a row ( for example I want to show 5 items in a row)and then goes to the next row. There are more than 100 item in the list but I want just show 5 item in every line and then goes to the next line. Thank you.
Widget subViewProducts = Container(
height: 200,
margin: EdgeInsets.symmetric(horizontal: 5, vertical: 3),
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: cate.length,
itemBuilder: (context, i) {
return Container(
width: 150,
padding: EdgeInsets.all(3),
child:
ProducttabBar(
cate[i].name ,
cate[i].icon,
cate[i].price,
cate[i].destion,
cate[i].quantity,
5,
5,
i,
),);
},
),
);
if (cate.isEmpty) {
cate.addAll(_products);
}
You can use GridView instead of ListView.
GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 5,
),
More info here: https://api.flutter.dev/flutter/widgets/GridView/GridView.builder.html

Flutter: How to show a widget after every 3 rows in a gridview.count

I am trying to implement a banner ad widget like so
AdBanner()
in a GridView.count widget. I want the ad widget to show after every 3 rows within the grid (i.e, below every 6th item). I'm not able to get the logic. Can anyone here, please help me out? Thanks. Here is the code for my gridview
Expanded(
child: GridView.count(
shrinkWrap: true,
controller: _scrollController,
crossAxisCount: 2,
childAspectRatio: 0.68,
physics: const ClampingScrollPhysics(),
scrollDirection: Axis.vertical,
children: items.map((Product item) {
return Container(
color: Colors.black,
margin: const EdgeInsets.only(top: 10),
child: ProductCard(data: item));
}).toList(),
),
I have added an image below to further explain what I mean. Though this is a listview, but if I can get it in listview, I will implement the grid. In the image, we can see a banner ad showing up after every 6th item in the list
You can try a different approach with GridView.builder()
int ad_count = items.length ~/ 6; // divide by number of entries between every ad
int showed_ads = 0; // counter variable
GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemCount: items.length + ad_count,
itemBuilder: (BuildContext ctx, index) {
if((index+1)%ad_count == 0) // modulo defines the count between every ad
{
showed_ads += 1;
return AdBanner(); // your AdBanner class here
}
return Container(
color: Colors.black,
margin: const EdgeInsets.only(top: 10),
child: ProductCard(data: item[index-showed_ads]));
}),

How to make a dynamic list of elements appear horizontally up to each 6 index and then vertically in flutter

I have a dynamic list which will be increased every 10 seconds and I want this list to be displayed in flutter UI in such a way that the first 6 numbers should display on the first line and the next 6 numbers should display on the next line and so on.
Currently, the code I made just displays a list in a horizontally scrollable view.
Widget horizontalList1 = new Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 40.0,
child: new ListView(
scrollDirection: Axis.horizontal,
children: calledNumersArray(),
)
);
You could use a GridView and limit its CrossAxisCount
Widget horizontalList1 = new Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
height: 40.0,
child: GridView.count(
count: calledNumbersArraay().length
crossAxisCount: 6,
children: calledNumersArray(),
)
);
https://api.flutter.dev/flutter/widgets/GridView-class.html
If you're adding elements dynamically, I suggest you use a builder constructor to avoid performance issues. I think GridView can help you here. However, you'd need to refactor your code so that your number widgets are generated in the itemBuilder function.
Like so:
final calledNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; // generate the numbers somehow
Widget horizontalList1 = Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
child: GridView.builder(
itemCount: calledNumbers.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 8,
),
itemBuilder: (BuildContext context, int index) { // create the widgets here
final calledNumber = calledNumbers[index];
return Container(
height: 30,
width: 30,
margin: EdgeInsets.all(14.0),
key: ValueKey(index), // make sure to add a key
child: Center(child: Text(calledNumber.toString())),
decoration:
BoxDecoration(shape: BoxShape.circle, color: Colors.red));
},
));
The result:

Flutter GridView.builder is Generating Unwanted Extra Space

I am trying to display a row of buttons. Since the number of buttons depends on the number of elements in a List, I have to use GridView.builder to dynamically create the right amount of buttons. Unfortunately it seems that GridView.builder is taking up alot of unnecessary space. Anyone know what is wrong here?
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Dice buttons
Flexible(
child: GridView.builder(
itemCount: dices.length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: dices.length,
),
itemBuilder: (BuildContext context, int index) {
return new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ButtonTheme(
minWidth: 50.0,
height: 50.0,
child: RaisedButton(
child: Text(
dices[index].toString(),
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
color: usedDices[index] || !expectNum
? Colors.grey
: Colors.black,
onPressed: () {
if (!usedDices[index] && expectNum) {
setState(() {
numUsed[turn] = dices[index].toString();
numUsedIndex[turn] = index;
});
expectNum = false;
usedDices[index] = true;
}
},
),
),
]);
})),
Link to Pic: https://drive.google.com/file/d/1_Jr4rz9GJ-D8-Xjxs2w8Sn8lOdnBBqTc/view?usp=sharing
As you can see are lots of unnecessary space here and it seems to be the reuslt of GridView.builder
That space is the result of Flexible, which fills the available space. You will see that if you replace it with a Container and give it a height, it won't produce that much space below.
Just had this problem, top SliverPadding is set to 20.0 by default. Looking at docs I see:
/// By default, [ListView] will automatically pad the list's scrollable
/// extremities to avoid partial obstructions indicated by [MediaQuery]'s
/// padding. To avoid this behavior, override with a zero [padding] property.
So,
GridView.builder(
// shrinkWrap: true,
padding: EdgeInsets.zero,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 1,
mainAxisSpacing: 1,
crossAxisCount: 3,
),
itemBuilder: (context, index) {
return Container(
color: Colors.black,
);
},
itemCount: 10,
),
or
If you put a widget before the ListView, you should wrap the ListView with a
MediaQuery.removePadding widget (with removeTop: true)

How to set grid view column height?

I am new to flutter and don't have much experience.
I am trying to develop an android app using flutter and this is my previous app design.
And I'm also able to successfully make grid view in flutter but the column height is the problem. Is their anyone who can help me with my flutter code.
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
hi(){
}
return new Scaffold(
appBar: new AppBar(
actions: <Widget>[],
title: new Text("milla"),
),
body: new Container(
child: new Center(
child: new GridView.count(
shrinkWrap: true,
scrollDirection: Axis.vertical,
childAspectRatio:1.0,
crossAxisCount: MediaQuery.of(context).size.width <= 400.0 ? 3 : MediaQuery.of(context).size.width >= 1000.0 ? 5 : 4,
// Create a grid with 2 columns. If you change the scrollDirection to
// horizontal, this would produce 2 rows.
crossAxisSpacing: 2.0,
// Generate 100 Widgets that display their index in the List
children: new List.generate(100, (index) {
return new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
verticalDirection: VerticalDirection.down,
children: <Widget>[
new Center(
child: new Image(
image: new NetworkImage('https://github.com/flutter/website/blob/master/_includes/code/layout/lakes/images/lake.jpg?raw=true')
)
),
new Expanded(child: new Text("apple2")), new Expanded(child: new Text(
'Item $index',
style: Theme.of(context).textTheme.headline,
)),new Expanded(child: new Center(child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[new Text("+",style: new TextStyle(fontSize: 24.0),),new Text("1"),new Text("-")])))]
);
}),
),
),
)
);
}
}
And this is my output.
How to set column height?
When i'm trying to add new view, it's showing this error "Another exception was thrown: A RenderFlex overflowed by 21 pixels on the bottom."
Put this instead of
childAspectRatio:1.0 to childAspectRatio: (itemWidth / itemHeight)
var size = MediaQuery.of(context).size;
final double itemHeight = (size.height) / 2;
final double itemWidth = size.width / 2;
It works fine in my code to set height and width of Gridview
You might try this:
GridView.count(
crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.height / 400,
children: <Widget>[...]
);
Try this
childAspectRatio: mediaQueryData.size.height / 1000,
where
MediaQueryData mediaQueryData = MediaQuery.of(context);
I saw this code somewhere and looks ok to me.
Maintain
childAspectRatio: with MediaQuery.of(context).size.height/600
if this didint work change 600 with diffrent but less numbers.
[Screenshot][https://i.stack.imgur.com/h28C2.png]1
This is my code:
final screenWidth = MediaQuery.of(context).size.width/3;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: new Container(
color: Colors.white70,
padding: EdgeInsets.all(5.0),
child: new GridView.count(
childAspectRatio: screenWidth/180.0,
crossAxisCount: 3,
crossAxisSpacing: 5.0,
mainAxisSpacing: 5.0,
children: _buildFirdTitles(35),
),
),
);
If you use GridView.builder
Try this, use gridDelegate section to custom items height
Edit the parameter childAspectRatio
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
// width / height: fixed for *all* items
childAspectRatio: 0.75,
)
class ItemCardGridView extends StatelessWidget {
const ItemCardGridView(
{Key? key,
required this.crossAxisCount,
required this.padding,
required this.items})
// we plan to use this with 1 or 2 columns only
: assert(crossAxisCount == 1 || crossAxisCount == 2),
super(key: key);
final int crossAxisCount;
final EdgeInsets padding;
// list representing the data for all items
final List<ItemCardData> items;
#override
Widget build(BuildContext context) {
return GridView.builder(
padding: padding,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
// width / height: fixed for *all* items
childAspectRatio: 0.75,
),
// return a custom ItemCard
itemBuilder: (context, i) => ItemCard(data: items[i]),
itemCount: items.length,
);
}
}