FLUTTER: How to use text widget inside a string - android

I have a list of text widgets like this:
List<Widgets> widgets = [
Text('Theo', style: TextStyle(color: Colors.green)),
Text('Luca', style: TextStyle(color: Colors.blue))
]
and I have list of Strings like this:
'My name is #name',
'#name how old are you ?'
I want to replace the #name by a text widget in the list to print the right name with the right color.
Do you know a way to do it ?

you probably looking for rich text widget check this
RichText

It's not possible in Text Widget, use RichText Widget which takes TextSpan as children.
Change Text to Text Span
List<Widgets> widgets = [
TextSpan(text: 'Theo', style: TextStyle(color: Colors.green)),
TextSpan(text: 'Luca', style: TextStyle(color: Colors.blue))
]
Each line is a RichText. RichText takes TextSpan as children where the text and the corresponding style has to be specified.
widgets.map((textSpan) {
return Column(
children: [
RichText(
text: TextSpan(
text: 'My name is ',
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
textSpan // Name with color will appear here
],
),
RichText(
children: <TextSpan>[
textSpan // Name with color will appear here
TextSpan (
text: 'How old are you? ',
style: DefaultTextStyle.of(context).style,
),
],
),
],
)
}
Alternatively this can replicated by using Row and Text widgets but then it won't be a single line.

Related

Is there a widget in flutter that allows you to create such a view?

I need to implement such a widget in Flutter. Full version of the page, where this widget is going to be used is here Maybe you know build-in widgets, which could help me. But if there are none, what is the best way to implement this widget?
You can use a ListTile
ListTile(
leading: const Text("Lbikgabsb"),
trailing: Column(
children: [
const Text("900 P"),
const Text("2"), // add your decoration to the background
),
],
),
),
Alternatively you can use:
SizedBox(
height: your_height,
width:double.infinity,
child: Row(
children:[
Text("Lbikgabsb"),
Column(
children: [
const Text("900 P"),
const Text("2"), // add your decoration to the background
],
),
),
You can choose whichever approach works best for your app

Scrolling to a TextSpan inside a RichText

The problem is that I have a RichText inside a Scrollable Widget(SingleChildScrollView or ListView) and I need to Scroll to a specific TextSpan inside the RichText. Also, I cannot use ScrollablePositionedList because the texts should write in the end of the last one and if there was no space to continue the text should go to the next line so I have to use RichText.
Similar to
Sample Code:
ListView(
children:[
RichText(
children: AListOfTextSpansThatICreateWithIndexes(),
),
],
),
Sample Text That I want to show:
This is TextSpan1 and it is a bit
long. This is TextSpan2 and its ok.
Similar to Scrollable position of a TextSpan within a RichText
There is a way to make sure a key is visible on the screen using the Scrollable.ensureVisible method. (credits to the person who deleted their comment)
you should first generate keys for your list:
var keys = List.generate(LENGTH, (i) => GlobalKey());
Then use the keys behind each TextSpan:
Text.rich(
TextSpan(
children: Iterable.generate(LENGTH, (i) => i)
.expand((i) => [
WidgetSpan(
child: SizedBox.fromSize(
size: Size.zero,
key: keys[i],
),
),
TextSpan(
text: 'this is text number $i',
),
])
.toList(),
),
),
Finally, call this function(where there is access to the context) to scroll to the desired key:
Scrollable.ensureVisible(
keys[i].currentContext,
alignment: 0.2,
duration: Duration(milliseconds: 500),
),

Why is the text widget underlined?

Hey guys I have a question. I tried to ad a new text widget in my code (just as always), but it is red underlined. I don´t know why. In my opinion everything is right. I copied the same text widget one line below and in that line it isn´t underlined. Can anybody say my what the reason therefore is? (I added a link in which you should find the part of the code.
This is the code
pic of the widgets
You have to add , after RaisedButton.
Column(
children: <Widget>[
RaisedButton(
child: Text("Button"),
onPressed: (){
},
), // <--- here
Text("Simple"),
Text("simple"),
],
),
It's underline because it's missing a comma before your Text Widget :
RaisedButton(
child: Text(startStopButtonDesign),
onPressed: () {
startOrStop();
},
), //Missing commma
Text(_time.toString()),

How can i load an image in between a text in Flutter

I need to display content containing text and images from an API on my android app, when I save it in a variable of type String, the texts get displayed but the images shows as url. How do I load the get the links to load in between the texts?
I needed to insert an Image inline with a sentence of texts.
So for that I used this:
final String paragraph1 =
'Advanciti App is an app where you can study and make friends with people living near your home. Advanciti is an Edtech Startup based out of Pune, Maharashtra, India.\n';
RichText(
text: TextSpan(
children: [
TextSpan(text: "The "),
WidgetSpan(child: Container(width: 20.0, child: Image.asset('assets/app_icon.png', scale: 1))),
TextSpan(text: paragraph1),
],
),
),
if you want to show images you need to convert your data to HTML or Markdown. in MarkDown and HTML, you can show much more then the image. these are packages for HTML and MarkDown:
flutter_html
flutter_markdown
You can use the following to place an image between two texts vertically:
Column(
children: <Widget>[
Text("Hello"),
Image.network(
'REPLACE HERE WITH YOUR IMG URL'),
Text("World")
],
),
or you can use this to place an image between two texts horizontally:
Row(
children: <Widget>[
Text("Hello"),
Image.network(
'REPLACE HERE WITH YOUR IMG URL'),
Text("World")
],
),

HTML plugin with text right alignment flutter?

I'm using HTML widget from flutter_html library to show some articles fetched from some api and I want to align my text to the right (rtl language)
This my code
Html(
data: widget.article.postContent,
defaultTextStyle: TextStyle(
color: ThemeColors.white100,
),
),
I have tried to use Directionality widget as parent widget but it doesn't work.
add customTextAlign property with flutter TextAlign class properties to the widget like the below code
Html(
customTextAlign: (_) => TextAlign.right,
),
UPDATE:
after version 1.0.0
it should be like this
Html(
style: {
'html': Style(textAlign: TextAlign.right),
},
)
as mentioned by MSpeed
in this answer
It seems like the other answer might be out of date now. I had luck with this:
Html(
data: myData,
style: {
'html': Style(textAlign: TextAlign.center)
},
)

Categories

Resources