How to extract digits and string from a combined string and put them seperately into different list [duplicate] - flutter

I have a List that contains lottery numbers
List image
How can I separate number and text, and after that list them in the below list: for example:
List<String> n = ["ABC23", "21A23", "12A312","32141A"];
Thank you!

You can do it by using forEach like below:
List<String> n = ["23", "2123", "12312","32141"];
n.forEach((element) =>
print(element)
);
And to separate number and text you can use the following code:
const text = '''
Lorem Ipsum is simply dummy text of the 123.456 printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an 12:30 unknown printer took a galley of type and scrambled it to make a
23.4567
type specimen book. It has 445566 survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
''';
final intRegex = RegExp(r'\s+(\d+)\s+', multiLine: true);
final doubleRegex = RegExp(r'\s+(\d+\.\d+)\s+', multiLine: true);
final timeRegex = RegExp(r'\s+(\d{1,2}:\d{2})\s+', multiLine: true);
void main() {
print(intRegex.allMatches(text).map((m) => m.group(0)));
print(doubleRegex.allMatches(text).map((m) => m.group(0)));
print(timeRegex.allMatches(text).map((m) => m.group(0)));
}
Please refer to this link for more information.

To do something with each element:
List<String> n = ["23", "2123", "12312","32141"];
n.forEach((element) {
int number = int.parse(element);
...
}
);
To do create list of ints:
List<String> n = ["23", "2123", "12312","32141"];
final intList = n.map((element) => int.parse(element)).toList();

Related

Can iterate through an array with forEach, but not with List

I have an array of notes (viewModel.list) that I would like to display in a List. All notes conform to Identifiable.
When I use ForEach, all notes appear just fine:
ForEach(viewModel.list){ note in
NoteView(namespace:namespace, note: note)
}
But if I change this to a List no objects appear:
List(viewModel.list){ note in
NoteView(namespace:namespace, note: note)
}
The same thing happens if I embed the ForEach in a List
List{
ForEach(viewModel.list){ note in
NoteView(namespace:namespace, note: note)
}
}
I want to use a List because I would like to add a swipeAction to every NoteView which can only be used in Lists. Is there any reason this behavior is happening?
UPDATE: I have uploaded a reproducible example below and believe it has something to do with embedding the List in a ScrollView. If I remove the ScrollView, this code works fine, but nothing shows up with it.
import SwiftUI
struct Note: Identifiable {
var id = UUID()
var content: String = ""
}
var notes = [
Note(content: "Lorem ipsum dolor sit amet, consectet adipiscing elit. Condimentum quisque id vitae convallis dignissim pharetra nisl est creatus"),
Note(content: "Mauris ac tempor libero, non eleifend lectus. Mauris eu hendrerit nunc. Donec at ante mauris. Duis ac elit purus. Mauris ullamcorper mi."),
Note(content: "Lorem ipsum dolor sit amet, consectet adipiscing elit. Condimentum quisque id vitae convallis dignissim pharetra nisl est creatus"),
Note(content: "Mauris ac tempor libero, non eleifend lectus. Mauris eu hendrerit nunc. Donec at ante mauris. Duis ac elit purus. Mauris ullamcorper mi.")
]
struct ContentView: View {
var body: some View {
ScrollView {
List(notes){ note in
NoteView(note: note)
.padding(.bottom)
}
}
}
}
struct NoteView: View {
var note: Note = notes[0]
var body: some View {
Text(note.content)
}
}
List is not showing because List infer it size from its parent View. So when you embed it inside something like Scrollview that have dynamic height, it can't infer it size correctly.
Also List behaves like a UIScrollView (an arbitrarily long scrollable view of views).
So no need to wrap List inside scrollview.
struct ContentView: View {
var body: some View {
// ScrollView {
List(notes){ note in
NoteView(note: note)
.padding(.bottom)
}
// }
}
}
Just remove List from ScrollView - it is scrollable itself.
struct ContentView: View {
var body: some View {
List(notes){ note in
NoteView(note: note)
.padding(.bottom)
}
}
}
*the reason of the issue is that List wants to take parent size to expand, but ScrollView does not have own size and expects from content a size that needs to be scrolled, result is obvious - cycling conflict - to do not crash/hang in cycling SwiftUI breaks layout - output is empty.

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),
)

How do I make a column of 3 cards match in height in bootstrapVue?

I have a row with 3 columns containing cards, the b-card-text (text in the cards) is not equal in length thus making the cards differ in height. Question is how do I make the cards match in height?
Tried adding h-100 d-inline-block classes to the cards but no luck.
<b-row>
<b-col class md="4">
<b-card>
<b-card-text>Lorem Ipsum is simply dummy text of </b-card-text>
</b-card>
</b-col>
<b-col class md="4">
<b-card class="h-75 d-inline-block">
<b-card-text>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make
</b-card-text>
</b-card>
</b-col>
<b-col class md="4">
<b-card>
<b-card-text>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard
</b-card-text>
</b-card>
</b-col>
</b-row>
I expect the cards to be the same in height.
On the <b-row> add the class class="align-self-stretch", or to make it applicable to only md and up breakpoints: class="align-self-md-stretch". And then use the utility class h-100 on each <b-card>
Example: https://jsfiddle.net/ofcxrquz/
See:
https://bootstrap-vue.js.org/docs/reference/utility-classes
https://getbootstrap.com/docs/4.3/utilities/flex/#align-self

How to combine unicode characters in Flutter?

I need to display the combining overline character (unicode U+0305) over some other characters, like '2' or 'x'.
https://www.fileformat.info/info/unicode/char/0305/index.htm
Is there a way to accomplish this in Dart?
Thanks in advance.
You can combine by placing the unicode right after the letter:
String overlined = 'O\u{0305}V\u{0305}E\u{0305}R\u{0305}';
print(overlined); // Output: O̅V̅E̅R̅
A more dynamic version (with simplistic logic) would be:
void main() {
String overlined = overline('I AM AN OVERLINED TEXT');
print(overlined); // Output: I̅ A̅M̅ A̅N̅ O̅V̅E̅R̅L̅I̅N̅E̅D̅ T̅E̅X̅T̅
}
String overline(String text) {
return text.split('').map((String char) {
if (char.trim().isEmpty)
return char;
else
return '$char\u{0305}';
}).join();
}
However, this is pretty much limited. A better approach would be using the style property of Flutter's Text to do so:
const Text(
'OVER',
style: TextStyle(decoration: TextDecoration.overline),
);

How to set line height in Clutter.Text using Pango

I want to have a Clutter.Text displaying text with double line spacing using Gtk.Pango.
note : in css, we would use line-height: 200% I think.
I tried this code and it didn't work:
var text_actor = new Clutter.Text.with_text ("Roboto 10", "Long long text") ;
text_actor.width= SIDE_PANE_WIDTH ;
text_actor.get_layout ().set_spacing (2*Pango.SCALE) ;
Apparently, Clutter.Text keeps a cached version of the Pango.Layout
Is there a way to achieve this?
One way working around this by using CoglPango directly:
var t = new Clutter.Actor ();
t.set_size (300, 300);
// add some more text to see it
var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, ...";
var layout = t.create_pango_layout (text);
layout.set_width (200);
layout.set_spacing (24 * Pango.SCALE);
t.paint.connect (() => {
Cogl.pango_render_layout (layout, 0, 0,
Cogl.Color.from_4ub(0, 0, 0, 255), 0);
});
edit this is indeed the solution. Based on this, here's the code that I used:
actor = new Text () ;
actor.width= SIDE_PANE_WIDTH ;
var text = "Some long text<b>Bold</b>" ;
var pango_layout = bio_actor.create_pango_layout ("");
pango_layout.set_markup (text, text.length) ;
pango_layout.set_spacing (2 * Pango.SCALE);
pango_layout.set_font_description (Pango.FontDescription.from_string ("Roboto 10")) ;
pango_layout.set_width (SIDE_PANE_WIDTH * Pango.SCALE);
bio_actor.paint.connect (() => {
Cogl.pango_render_layout (pango_layout, 0, 0, Cogl.Color.from_4ub(255, 255, 255, 255), 0);
});
int width;
int height ;
pango_layout.get_size (out width, out height) ;
actor.height = height / Pango.SCALE;
Yes, ClutterText caches the PangoLayout instance to avoid having to re-measure text every time it has to retrieve the preferred size. It actually caches the latest three instances of PangoLayout, because of the width-for-height/height-for-width geometry requests, but that's just an implementation detail.
Changing properties on the PangoLayout instance of ClutterText is also not going to result in updates to the ClutterText: you'd have to call clutter_actor_queue_redraw() afterwards, as the PangoLayout sits at a lower level than the actor in the scene graph.
In general, the ClutterText actor does not allow you to tweak the PangoLayout it creates; it's expected that the actor is in charge of creating an modifying the layout, and attempts at overriding that will result in either poor performance or undefined behaviour.
If you want to introduce a line spacing property then you'll have to file a bug against ClutterText — though that would mean that you'd have to wait until September for the next stable release of Clutter.
If you don't need all that ClutterText does, and you just want to display text, you could subclass ClutterActor and use clutter_actor_create_pango_layout() to create a PangoLayout that you can measure inside the overridden get_preferred_width(), get_preferred_height(), and allocate() virtual functions, and paint inside the overridden paint() virtual function.