Flutter FOR loop inside widget - flutter

So essentially what I am trying to achieve is this:
But apparently its not allowed in flutter.
I'm trying to load a LIST of restaurant items and their quantity values using a FOR loop and display them unto the app with a text widget. But with the current logic that I used I need to iterate and update the variable of x and range after displaying each item name along with its quantity from their respective lists. The problem here is I can't use the FOR loop within widgets as you would a normal FOR loop where you can place multiple statements within the FOR loop.
The snippet of code below is the logic I'm trying to achieve. It is similar to the code I'm implementing in flutter. The FOR loop portion is the only portion I'm kind of stuck with.
Still kind of new to Flutter, so any help or guidance would be appreciated. Thank you!
void main()
{
int x =0;
int range =0;
List<String> restaurauntNames = ['Jollibee','Chowking','Mcdonalds'];
List<int> orderNumber = [3,2,1];
List<int> itemQuantity = [1,1,2,3,1,3];
List<String> itemName= ['Jolly Spag','Chicken Joy','Peach Mango Pie','Chao Fan','Lauriat','Big Mac'];
for(String i in restaurauntNames)
{
print(i);
for(int z =0; z!=orderNumber[x];z++)
{
print(itemName[z+range]+'---------------'+ itemQuantity[z+range].toString());
}
range = range + orderNumber[x];
x++;
}

You can't put brace in for loop widget, I've got the same problem for quite some time and one the solution was to map the iterated list.
children: [
...restaurantNames.map((i){
print(i);
for(int z = 0; z != loadedOrder.orderNumber[x];z++){
return Text(loadedOrder.itemNames[z + range] + 'X' +loadedOrder.itemQuantity[z + range].toString());
}
range = range + orderNumber[x];
x++;
}).toList(),
],

Related

I have Three Lists with user inputs how to create a table flutter [duplicate]

This question already has an answer here:
How can i create a table flutter?
(1 answer)
Closed 1 year ago.
Lists with user inputs
List1 =[1,2,3,.....];
List2 =[0.1,0.2,0.3,.....];
List3 =[0.5,0.10,6.0,....];
How Can I Create a table Like following image Please help Me I am stuck
Final Out Example Image
So you want a List of Lists. You could do something like this:
List<List<dynamic>> listMap = [];
listMap.add(List1);
listMap.add(List2);
listMap.add(List3);
Note I put dynamic because I don't know what types of variables you want.
My Lists
List1 =[1,2,3,.....];
List2 =[0.1,0.2,0.3,.....];
List3 =[0.5,0.10,6.0,....];
Table Widget
Widget createTable() {
List<TableRow> rows = [];
for (int i = 0; i < xList.length; i++) {
rows.add(TableRow(children: [
Text(List1[i].toString()),
Text(List2[i].toString()),
Text(List3[i].toString()),
]));
}
return Table(children: rows);}

How to replace text with image(is it possible in flutter)

var replaced = text.replaceAll(regExp, '*');
Instead of the asterisk mark can we replace this with actual image..
Ok. So you have bunch of emojis stored locally. and you have to replace a text with emoji.
I don't have a perfect answer. but how you can achieve a path that i can show.
Using a map. as you are suppose to change the text for an image there must be a pair. so you can define a map for it. a key will be a text and the value will be the path of the emoji. considering your folder as a images and format is png.
Map<String, String> myData = {"like" : "images/like.png", "time":
"images/time.png"};
and so on. Like this you can create a full map of it. if it's limited like 30 -40 records that you will do manually.
Now you can perform operations on that. Suppose you have String value = " I like your shirt!";
so we need to scan the string and then search every word inside tha map and replace if you found it.
the code looks like this
void main() {
//your value that you are going to scan
String str = " I like your shirt";
//Defining the map
Map<String, String> data = {"like" : "images/like.png", "shirt" :
"images/shirt.png", "happy" : "images/happy.png"};
//function that will do the changes and display
void replaceTextWithEmojis(String value, Map<String, String> data){
//splitting the value into the array or list of items so we can iterate easily
List<String> inputvalues = str.split(" ");
//looping through the text
for (int i =0; i<inputvalues.length; i++){
//checking where that text is present in the emojis map or not.
if(data.containsKey(inputvalues[i])){
//this will replace the text with the path for that key
inputvalues[i] = data[inputvalues[i]];
}
} //end of the for loop
print(inputvalues);
}
replaceTextWithEmojis(str, data);
}
This will replace with the desired values. But You want to display a image between the text. That's what something difficult. I don't think you can do it in flutter. Or it will be like you need to identify the position of the replaceable text and then break the string and store it in other variable. append the image at the breakpoint. so at the place where we are replacing with the map you can add a widget(child ImageProvider(inputvalue[i]));
I would suggest you to use unicode for emojis.
you can use the package for unicode emjois : https://pub.dev/packages/emojis
Also they will give you better performance.
/// Replace an emoticon or any string token with an image having a tooltip and Help cursor.
static List<Widget> replaceEmoji(final String text, final String fromToken, final String toImgTooltip, final String toImg) {
Widget _getImage(final String path, final String tooltip) => MouseRegion(
cursor: SystemMouseCursors.help,
child: Tooltip(message: tooltip, child: Image.asset(path)),
);
if (!text.contains(fromToken)) {
return [Text(text)];
}
final result = <Widget>[];
var buffer = text;
var n = -1;
while (true) {
n = buffer.indexOf(fromToken);
if (n != 0) {
result.add(Text(buffer.substring(0, n)));
}
result.add(_getImage(toImg, toImgTooltip));
buffer = buffer.substring(n + fromToken.length);
if (buffer.isEmpty) {
break;
}
}
return result;
}
You use it as follows:
...
Wrap(
children: replaceEmoji('my :) face - :):)', ':)', 'smile', 'assets/images/emoji/smile.png')
)
...

How to Jump to next Element in Dart Lists, (forLoops)

Hey Darting Developers, I want to Seek help. I am Currently Making a Emoji Translator, here i am Getting a Problem that,
List emojis = [
["Happy", "Smile", "Wink", "Tickle"],
["Silent", "Depressed", "Cry", "Sad"],
["lichi", "apple", "kiwi", "Banana"]];
I have a Long List
This List is under a List.
I want to Seek only 3 Items from a List.
And then Jump out to Other Item of the Big List.
How to Get Three Items and then Get out of loop and Get inside the another List. and so on.
This Question is Bit hard to Understand, but I need to Seek help.
An option is to map over the emojis 2D list and take the first 3 elements.
final emojis = [
["Happy", "Smile", "Wink", "Tickle"],
["Silent", "Depressed", "Cry", "Sad"],
["lichi", "apple", "kiwi", "Banana"]
];
final transformedList = emojis.map((sublist) => sublist.take(3).toList());
emojis.forEach((subList) {
for(int i=0; i<subList.length; i++){
if(i > 2) break;
//get your first 3 items
}
});

Adding and Removing Items From Flutter Lists Dynamically

I have these two functions that should add the item onTap to the list, and then another one to delete the item. The first one (adding the item) works fine, but the delete one doesn't seem to delete anything. I suspect the issue is that the counter variable is not global, but for some reason it doesn't work fine when I add it globally (and I would need it to be). Here's the full code:
List<int> _totalPrice = [];
List<int> get totalPrice => _totalPrice;
here is the add item function
Future getTotal(item) async {
int counter = 0;
_totalPrice.add(int.parse(item));
_totalPrice.forEach((element) => counter += element);
print('LIST: $_totalPrice');
print('SUM: $counter');
return counter;
}
here is the delete function that doesn't remove anything
deleteSumItem(String item) {
_totalPrice.remove(item);
}
I think the issue is that the counter variable isn't global, I am not sure how to add it globally to change dynamically.
So, here your code shows that you are putting int in your _totalPrice list, but you are trying to remove the String from the list. It will not be found to be deleted.
So, you can change the data type of your list into String or you can write the below function to delete an item where the function only takes an int
deleteSumItem(int item) {
_totalPrice.remove(item);
}
Also you can remove an item by below line of code (If you have a custom type of data in your list):
_totalPrice.removeWhere((item) => item.price == '520')

List<Chip> chip = []; how do I add more chips?

I have a list of chips widgets.
These were created as follows:
List<Chips> myChips = [];
myChips.add(... values);
So now myChips contains a ton of Chip objects.
How do I display this?? How do I display a collection of chip objects that exist in a list??
for example, I'm hoping for :
Wrap(children: ... [myChips])
or something of that nature. But the parent widget that encloses it must be able to take in the entire myChips list.
Use this code:
var chip= new Chip();
myChips.add(chip);
for (final element in myChips) {
print(element);
}