How can I implement number of textfields in vertical fashion - flutter

Example: https://i.stack.imgur.com/svQfv.jpg
And also the number of text fields are not fixed.
Any help will be appreciated !!
Edit:
I declared an array of controllers and an array for storing the textfield values.
List<StringBuffer> _gpas;
List<TextEditingController> _controllers;
And this is how I'm generating the fields
ListView.builder(
itemCount: widget.semestersDone,
itemBuilder: (BuildContext context, int index) {
return _createGPAInputField(
context,
'Semester ${index + 1}',
_controllers[index],
onChanged: _updateCGPA,
);
})
And this is how I'm trying to access their values in _updateCGPA function
void _updateCGPA(dynamic gpa) {
for (int i = 0; i < widget.semestersDone; ++i) {
_gpas[i].clear();
_gpas[i].write(_controllers[i].text);
}
...
}

Related

How to Pass value from first two index value to next index textfieldcontroller same listview?

First index value is 10. Second index value is 5
I am expecting the next index value to be 10 * 5 = 50, but I am getting the same value for all the index. Why does this happen?
void takeCostingValue(String text, String index) {
Map < String, int > quantities = {};
try {
int number = int.parse(text);
quantities[index] = number;
var totalObj = quantities.entries.map((e) {
return e.value;
}).toList();
var result = totalObj[0] * totalObj[1];
setState(() {
_costtextController = result as TextEditingController;
});
}
}
ListView.builder(
itemCount: costNameListData.length,
itemBuilder: (context, index) {
return TextField(
controller: _costtextController,
onChanged: (text) {
takeCostingValue(
text, index);
},
),
},
),

Display ads in every nth index using Listview.builder

I am trying to place an advertisement after every 3rd index. I got the required output but my problem in my case is i have a two type of List,
List One has 50 datas in it.
List two has only 5 datas.
When i tried with the below code.
ListView.separated(
physics: NeverScrollableScrollPhysics(),
padding: EdgeInsets.all(15),
shrinkWrap: true,
itemCount: articles.length,
separatorBuilder: (context, index) => SizedBox(
height: 15,
),
itemBuilder: (BuildContext context, int index) {
final Article article = articles[index];
final Sponsor sponsor = sponsors[index];
if(index %3 == 0 && index != 0) return SponsorCard(sponsor: sponsor, scaffoldKey: widget.scaffoldKey);
return Card4(article: article, heroTag: 'recent${article.id}', scaffoldKey: widget.scaffoldKey);
widget.scaffoldKey);
},
),
i am getting range error since sponsor list reached its last position.
error
The following RangeError was thrown building:
RangeError (index): Invalid value: Not in inclusive range 0..4: 49
What i need is that i need to make the sponsor list as loop so that if the List reached its last position it has to move again to the first position.
Can someone please help me on this.
You can create a new combined list as,
and make sure both Article & Sponsor class should be child class of ListItem.
OR
you need to wrap Article & Sponsor classes under ListItem Wrapper class
List items = []
Now use the items with ListView :
ListView.separated(
physics: NeverScrollableScrollPhysics(),
padding: EdgeInsets.all(15),
shrinkWrap: true,
itemCount: items.length,
separatorBuilder: (context, index) => SizedBox(
height: 15,
),
itemBuilder: (BuildContext context, int index) {
if (item[index] is Articles){
return SponsorCard(..);
}else{
return Card4(...);
}
},
);
Added Example on Request comment:
class _ListItem {
final bool isArticleType;
final dynamic value;
_ListItem(this.isArticleType, this.value);
}
class Article {}
class Sponsor {}
List<_ListItem> createList(List<Article> articles, List<Sponsor> sponsors) {
List<_ListItem> items = [];
items.addAll(articles.map((e) => _ListItem(true, e)).toList());
var index = 0;
var sIndex = 0;
for (var i = 0; i < articles.length && sIndex < sponsors.length; i++) {
if (i != 0 && i % 3 == 0) {
items.insert(index, _ListItem(false, sponsors[sIndex]));
sIndex++;
index++;
}
index++;
}
return items;
}
Thanks for your response guys.
I could achieve by the below code.
itemBuilder: (BuildContext context, int index) {
actualIndex = actualIndex +1;
if(sponsorIndex+1 >= sponsors.length){
sponsorIndex = -1;
}
if(index == 0 ){
articleIndex = -1;
}
if ((actualIndex-tempIndex) % 3 == 0 && actualIndex != 0 && (actualIndex-prevIndex) >=3 ) {
tempIndex = tempIndex +1;
prevIndex = actualIndex;
sponsorIndex = sponsorIndex + 1;
final Sponsor sponsor = sponsors[sponsorIndex];
return SponsorCard(sponsor: sponsor, scaffoldKey: widget.scaffoldKey);
}
articleIndex = articleIndex + 1;
final Article article = articles[articleIndex];
return Card4(article: article, heroTag: 'recent${article.id}', scaffoldKey: widget.scaffoldKey);
},

Attempting to run a simple widget inside my program but receive the "body_might_complete_normally" error. How can I correct this?

// Build the whole list of todo items
Widget _buildTodoList() {
return new ListView.builder(
itemBuilder: (context, index) {
// itemBuilder will be automatically be called as many times as it takes for the
// list to fill up its available space, which is most likely more than the
// number of todo items we have. So, we need to check the index is OK.
if (index < _todoItems.length) {
return _buildTodoItem(_todoItems[index], index);
}
},
);
}
Attempting to run a simple widget inside my program but receive the
"body_might_complete_normally" error.
How can I correct this?
itemBuilder only return on (index < _todoItems.length), you need to make sure return widget on every case.
Widget _buildTodoList() {
return ListView.builder(
itemCount:_todoItems.length, // your list length
itemBuilder: (context, index) {
if (index < _todoItems.length) {
return _buildTodoItem(_todoItems[index], index);
}
return Text("default case");// if above condtions dont meet, return this
},
);
}

How to add 1 fixed element in a list Flutter

Can you please tell me if it is possible to create a ListView but always have 1 fixed item at the end of the list, thus allowing a new card to be added. As shown in the screenshot below. There is a regular list of cards and at the end a card with an Add New Point button, when clicked, an action occurs (adding a new element to the end of the list).
If you know how to implement such an element as Add New Poynt, please tell me, I will be grateful.
Very simple, use [...oldList] to create a new List and add your fixed item after it like [...oldList, fixedItem].
Example:
Example with ListView:
You can increase itemLength by 1 and while check if the index is last one, provide your fixed widget.
ListView.builder(
itemCount: itemLength + 1,
itemBuilder: (context, index) {
if (index == itemLength) {
return Text("last Item");
}
return Text("item $index");
},
),
This is only logic please try it your own widget. Because I have not laptop.
**This code has syntax error please chaeck it because i have not laptop **
int howManyWidgetDraw = 5;
ListView.builder(
itemCount: howManyWidgetDraw,
itemBuilder: (BuildContext context,int
index){
if(howManyWidgetDraw==index.length){
return InkWell(
onTap(){
howManyWidgetDraw=howManyWidgetDraw+1;
//Call setstate method.
},
child: Text("last element print"));
}
else{
return Text("first four element");
}
}
),
);
Have any issue please ask me?
Try to use ListView.separated:
ListView.separated(
itemCount: _myList.length,
itemBuilder: (context, index){
return /* Your list item widget here */;
},
separatorBuilder: (context, index){
if(index == _myList.length-1) {return Text('Add New Poynt');}
else {return SizedBox();}
}
),

flutter widgets inside for loop

I have the following code to create Row widgets based on a returned items from Future Builder
for (int i = 0; i < snapshot.data.length; i++) Row( ) ,
the above will create Rows based on the number of items from snapshot.data
what I'm trying to achieve is create multiple widget based on the length of items returned
for (int i = 0; i < snapshot.data.length; i++) {
// for each item create two rows for example
Row( ) ,
Row( ) ,
}
but I get the following error !
the element Type 'set<Row>' can't be assigned to the list 'Widget'
Try this one:
List.generate(snapshot.data.length, (index) => Row( ),),
or if you want to insert deferent widget at deferent index use:
List.generate(snapshot.data.length, (index) {
if(index == 0){return Row();}
else if(index == 1){return Column();}
else{return Container();}
),
Use ListView.builder defining the length to itemCount property.
For Example..
body: ListView.builder
(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext ctxt, int index) {
return Row();
}
)
You either use a listView.builder or for in. When you're building a widget you can't use curly braces