HTML plugin with text right alignment flutter? - android

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

Related

How to add whatsapp image in floating action button in flutter

How to add whatsapp image in floating action button in flutter because whatsapp icon is not in flutter icon list. Tell me a simple method.
Row(
children: [
FloatingActionButton(
child: const Icon(Icons.chat),
backgroundColor: Colors.green.shade800,
onPressed: () {
String url =
"https://wa.me/+923045873730/?text=Hello";
launch(url);
},
),
],
),
Font Awesome has a flutter package that helps with most icons that are not in flutter default icon set, https://pub.dev/packages/font_awesome_flutter. It also includes all icons listed in font Awesome Offical website https://fontawesome.com/icons
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
Row(
children: [
FloatingActionButton(
child: FaIcon(FontAwesomeIcons.whatsapp),
backgroundColor: Colors.green.shade800,
onPressed: () {
String url =
"https://wa.me/+923045873730/?text=Hello";
launch(url);
},
),
],
),
The easiest way that comes to my mind is that downloading the icon image of Whatsapp (from flaticon or other website), adding it to the project's assets and adding it as a child( for example asset image).

auto detect link flutter

i have an issue about the text widget not being able to detect that there is a link.
later I will get a text response from the server as shown. the response is in the form of text as well as a link.
the problem is the text widget on flutter can't detect it. How do I make the link in the text detectable and clickable?
I have read and searched about this but could not find it. because most of the link text must be typed by yourself. while my problem the text is automatically there because it is the response from the server
can anyone help me find an answer?
Thank you in advance
Text widget does not automatically recognise links.
You can, however insert actions to text using the TextSpan widget and url_launcher, somewhat like this:
RichText(
text: TextSpan(
children: [
TextSpan(text: 'some text'),
TextSpan(
text: url,
style: new TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline
),
recognizer: TapGestureRecognizer()
..onTap = () async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
},
),
]
),
)
You can wrap this into a class and make a small LinkText widget for yourself.
What flutter_linkify does is that it automatically detects URLs & email IDs in text and links them.
You can also do this using regex. For eg. for identifying url, you could have something like:
bool _isLink(String input) {
final matcher = new RegExp(
r"(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&//=]*)");
return matcher.hasMatch(input);
}
You will have to, modify this a bit to get the substring and use that as url in text span!

FLUTTER: How to use text widget inside a string

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.

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

Categories

Resources