List of all rendered words' boxes in Flutter - flutter

How can I have list of boxes containing all words I render, I need it to determine which word is user long clicking?
In other words how can I find what maxRange so I can use getBoxesForRange in code below?
My code :
var pin = Offset(0, 0);
// To create a paragraph of text, we use ParagraphBuilder.
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(
ui.ParagraphStyle(
textDirection: ui.TextDirection.rtl,
textAlign: ebookTextAlign,
fontFamily: "Asan",
fontSize: ebookMainTextSize,
),
)..pushStyle(ui.TextStyle(color: const ui.Color(0xFF000000)));
spans.forEach((spn) {
if (spn.localName == "p") builder.addText("\n");
builder.addText(spn.text + " ");
});
builder.pop();
paragraph = builder.build()
..layout(ui.ParagraphConstraints(width: size.width));
var list = new List<ExtendedTextBox>();
var boxes = paragraph.getBoxesForRange(0, maxRange);

Related

flutter dropdown menu string puşş and take double

i prepare a dropdown menu with 2 or more items
İ want to take the double value and use it in a calculation
My entries are Aluminium 2.75 ----Steel 7.85 etc.
I define
String density_choose = "Steel 7.25";
DropdownButton<String>(
value: density_choose,
items: density.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text("malzeme:$value"),
);
}).toList(),
icon: Icon(Icons.arrow_drop_down),
onChanged: (var secilenVeri) {
setState(() {
density_choose = secilenVeri!;
});
},
),
My calculation
double sum = int.parse(kenar.text) *
int.parse(kenar.text) *
int.parse(yukseklik.text) *
int.parse(yukseklik.text) *double.parse(density_choose.split(" ")[2])/
1000000;
output = sum.toString();
I dont have any problem in syntax but I cant take the double
I think I found the issue. It's coming from this density_choose.split(" ")[2].
You should be getting the 1 not the [2].
density_choose.split(" ")[1]
Since:
density_choose = 'Steel 7.25';
Then:
density_choose.split(" ") = ['Steel', '7.25']
density_choose.split(" ")[0] = 'Steel';
density_choose.split(" ")[1] = '7.25';

Bold text in result set from the text

I am trying to find a solution to this problem.
I have a text input, and when the user types in the text input "trending". I return a result set of text containing the word trending. What I want to do is display the text that was entered bold (example: trendingitems).
The solution I have currently works, sort of.
String resultText = "trending items";
int x = resultText.toLowerCase().indexOf(queriedText.toLowerCase()); // Problem happens here, I get -1 as a result
List parts = [ // I use this list to display in a Rich Text to adjust the style of the text the user input
if (resultText.substring(0, x).isNotEmpty) resultText.substring(0, x).trim(),
queriedText,
if (resultText.substring(x + queriedText.length).isNotEmpty)
resultText.substring(x + queriedText.length).trim()
];
int idx = resultText.toLowerCase().indexOf(queriedText.toLowerCase());
while (idx > 0 && idx < resultText.length) {
String _subT = resultText.substring(idx + queriedText.length);
idx = _subT.toLowerCase().indexOf(queriedText.toLowerCase());
}
If the user enters trending items but the result set has trendingitems, int x is returned a value of -1 which is where the widget crashes (understandably).
What I would like to get is the following:
This scenario works:
User types: trending
Display: trending items in the list
This scenario crashes:
User types: trending items
Display: trending items in the list
TIA
I've made a package for this: text_chunk_styling
dependencies:
text_chunk_styling: ^2.0.1
Basically it will apply a custom TextStyle to some part of your text in your case the code would be something like this:
Sample 1
TextChunkStyling(
text: 'trending items',
highlightText: const ['trending'],
highlightTextStyle: const TextStyle(fontWeight: FontWeight.bold),
)
Sample 2
TextChunkStyling(
text: 'trending items',
highlightText: const ['trending items'],
highlightTextStyle: const TextStyle(fontWeight: FontWeight.bold),
)

Duplication of data in flutter

I have a list of data "percesc", which have a repetitive number of DS_DISCIPLINA_DIS (ordered by).
I'm using a class that I found on internet to make a Accordion style of Cards that contains a "Table" for each DS_DISCIPLINA_DIS different records.
So, for each number of DS_DISCIPLINA_DIS records I want a different "Table" to put on each Card.
PercEsc = await _iAlunoRepository.getPercEscolar(TurmaAtual);
accordionChildren.clear();
String discAtual = "";
List<TableRow> linhas = [];
for (PercEscModel percesc in PercEsc) {
if (percesc.DS_DISCIPLINA_DIS != discAtual) {
if (discAtual != "") {
Table tabelaNotas = Table(children: linhas);
linhas.clear();
var newItem = Accordion(discAtual, tabelaNotas);
accordionChildren.add(newItem);
}
discAtual = percesc.DS_DISCIPLINA_DIS;
linhas.add(const TableRow(children: [
Text("Módulo", style: TextStyle(fontSize: 20.0)),
Text("Designação", style: TextStyle(fontSize: 20.0)),
Text("Nota", style: TextStyle(fontSize: 20.0))
]));
}
linha.add(TableRow(children: [
Text(percesc.DS_ABREVUNID_ORG_UNO, style: TextStyle(fontSize: 11.0)),
Text(percesc.DS_UNID_ORG_UNO, style: TextStyle(fontSize: 11.0)),
Text(percesc.QT_NOTA_AVL.replaceAll(".00", ""),
style: TextStyle(fontSize: 11.0))
]));
}
if I made this way, all accordionChildren stays with the records of the last DS_DISCIPLINA_DIS.
if I delete "linhas.clear();" line, all accordionChildren stays with all records os all DS_DISCIPLINA_DIS.
I thought that when I write "Table tabelaNotas = Table(children: linha);" I was making a new "tabelaNotas" and the old one was discarded, but it seems that it stays the same.
How can I atribute the different data to each accordionChildren?
Sorry about my english
Let me simplify the question.
if I have this code:
Table tabelaNotas = Table(children: linhas);
var newItem = Accordion(discAtual, tabelaNotas);
accordionChildren.add(newItem);
linhas.clear();
"linhas" is a "List" of data to lines of Table
After executing this line ("accordionChildren.add(newItem);"), the first child of accordionChildren has 18 lines.
But after "linhas.clear();", accordionChildren will have no lines.
I understand that flutter works with reference to objects, so if I define "Table tabelaNotas = Table(children: linhas);", the content of table wil be somehow "linked" to "linhas", and if I clear linhas, I am also clearing the content of Table.
But I want to clear (delete) "linhas" without loosing that data on accordionCildren.
Probably is something very simple, but as before last week I never had worked with flutter, I'm a little bit confused with this situation.
If anyone could help me, I will be forever grateful.
I found the answer
List<TableRow> linhasAux = List.from(linhas);
tabelaNotas = Table(children: linhasAux);
var newItem = Accordion(discAtual, tabelaNotas);
accordionChildren.add(newItem);
linhas.clear();
this way, I made a copy (linhasAux) of List (linhas) and added the copy to table. Now I can clear "linhas" without loose the information that were already on Table.

Flutter: Unable to display correct word using hex value for QCF_BSML font

In my flutter application I'm using qcf_bsml font, I have to generate the hex values to display the corresponding words in the font file. For that, I'm using this equation:
(64396 + Id >= 64434) ? 64429 + chapterId : 64396 + Id
I convert the result from above to hex value using this function:
calculatehex(){
final myInteger = (64396 + 1 >= 64434) ? 64429 + 1 : 64396 + 1; //Id is 1: result 64397
final hexString = myInteger.toRadixString(16);
final paddedString = hexString.padLeft(4, '0');
uppercaseString = paddedString.toUpperCase();
print(uppercaseString); //displays correct hex value in console :FB8D
}
The problem is I am only able to hardcode the hex value in text field to display the correct word. For example:
new Text('\u{FB8D}',textDirection: TextDirection.rtl,
style:TextStyle(fontSize:30.0, fontWeight: FontWeight.w100,fontFamily: 'QCF'),
textAlign:TextAlign.center ), // works fine
But if I use the variable name to display text, it displays the wrong word.
new Text(uppercaseString,textDirection: TextDirection.rtl,
style:TextStyle(fontSize:30.0, fontWeight: FontWeight.w100,fontFamily: 'QCF'),
textAlign:TextAlign.center ), // not showing correct word
I tried to concatenate the string like this, but still not working fine
new Text(r'\u{'+uppercaseString+'}',textDirection: TextDirection.rtl,
style:TextStyle(fontSize:30.0, fontWeight: FontWeight.w100,fontFamily: 'QCF'),
textAlign:TextAlign.center ),
Please help me understand why is it display the correct word with hardcoded value with '\u' but not with variable. Thank you
I solved it by using String.fromCharCode(int.parse()) & making following changes in the code:
uppercaseString = paddedString.toUpperCase();
finalhexString ='0x$uppercaseString';
then:
new Text(String.fromCharCode(int.parse(finalhexString)),textDirection: TextDirection.rtl,
style:TextStyle(fontSize:30.0, fontWeight: FontWeight.w100,fontFamily: 'QCF'),
textAlign:TextAlign.center ),

taking items from map in flutter

I am tring to use a map to get words from a string and map them to a widget.
I have tried this but my problem is the key for the words doe and sister get the same keys so i end up getting only one of them
String theText = "my name is doe from http.doe.com, my sister is selly. doe and saqil are not sister friends of koiter.";
wordsMap = Map.fromIterable(text.split(' '),
key: (v) => v,
value: (v) => TextSpan(text: v));
so I tried the code below
Map mapMyWord = {};
// var wordsMap;
var splitForSize = text.split(' ').toList();
for(var t = 0;t<= splitForSize.length-1;t++){
mapMyWord[t] = {'$t':TextSpan(text: splitForSize[t])};
}
but In the second code when I tried to access mapMyWord.values.toList() it returns a list of map data again
[{0: TextSpan("my")}, {1: TextSpan("name")}, {2: TextSpan("is")}, {3: TextSpan("doe")}, {4: TextSpan("````http.codeish.com````,")}, ... ,{19: TextSpan("koiter")}]
so my main problem is how to get the values from here.
It returns maps because you're assigning maps with this line :
mapMyWord[t] = {'$t':TextSpan(text: splitForSize[t])};
So in the end you have a Map<Int, Map<String, TextSpan>>.
If you meant to turn the words of that sentence into a list of TextSpan, this would be the way :
var textSpanList = text.split(" ").map((word) => TextSpan(text: word)).toList();
If you want to do it directly in the widget tree, this would do it :
children: <Widget>[
for(var word in text.split(" "))
Text(word),
]
N.B: This last snippet requires a minimum SDK of 2.2.2 in the pubspec.yaml
In your second code, change the assignment part:
Map mapMyWord = {};
// var wordsMap;
var splitForSize = text.split(' ').toList();
for(var t = 0;t<= splitForSize.length-1;t++){
mapMyWord[t] = TextSpan(text: splitForSize[t]);
}
Then, mapMyWord.values.toList() will only return a list of TextSpan's. And if you want to get some specific value from the map:
int index = 1; //some number
print(mapMyWord[index]); //this will return one TextSpan