How to align the icon relative to the text in RichText? - flutter

I have a text located in RichText, n how much later I want to make it clickable, there is an icon next to the text. The problem is that the icon does not align correctly relative to the text, this is especially noticeable when its size increases. How do I make the icon and text to be on the same line in the center ? Align centered doesn't help
RichText(
textAlign: TextAlign.center,
text: TextSpan(
children: [
TextSpan(text: 'Next ', style: richBotton),
WidgetSpan(
child: Icon(Icons.arrow_forward_ios, color: Colors.grey, size: 11,)
),
],
),
),
for styles
final richBotton = const TextStyle(color: Color.fromARGB(255, 117, 117, 117), fontSize: 11,);
final text = const TextStyle(color: Colors.grey, fontSize: 11);
Screen
My code
With alignment: PlaceholderAlignment.Middle
With alignment: PlaceholderAlignment.Top
With alignment: PlaceholderAlignment.Bottom

You can include alignment: PlaceholderAlignment.middle on WidgetSpan.
RichText(
textAlign: TextAlign.center,
text: TextSpan(
children: [
TextSpan(
text: 'Next ',
),
WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Icon(
Icons.arrow_forward_ios,
color: Colors.grey,
size: 11,
)),
],
),
),

Related

Add text field with text paragraph in Flutter

I am trying to add a text field inside the paragraph in flutter but not getting success below screen shot what I want to achive.
I am trying the below code
RichText(
text: TextSpan(
text: "“",
style: TextStyle(
color:
const Color(0xff56A1D5),
fontSize: 20.sp),
children: <InlineSpan>[
WidgetSpan(
child: Text(
"I want to talk with you about ",
textAlign: TextAlign.center,
style: TextStyle(
color: const Color(
0xff506274),
fontSize: 20.sp),
),
),
const WidgetSpan(
child: SizedBox(
width: 120,
child: TextField(
decoration:
InputDecoration(
isDense: true,
contentPadding:
EdgeInsets.all(0),
),
),
),
),
WidgetSpan(
child: Text(
"(the actions, attitude, or behavior)",
textAlign: TextAlign.center,
style: TextStyle(
color: const Color(
0xff506274),
fontSize: 20.sp),
),
),
],
),
),
But My output is look like below.
Can anyone show me where I am going to wrong or How can I achieve the same as the above image without leaving extra space?
Thanks in advance.
Your code is perfectly right. as (the actions, attitude, or behavior) this text does not fit in the screen it came into a new line. For checking this reduce your text then you may understand the issue.
Your code output when I run your code:
Desktop:
Mobile:

How to center title in flutter obx

I am using get's obx() like below code.
But I want the title to be centered.
But I can't even use "align", so I'm curious how to align it.
title:
Obx(
() => Text.rich(
TextSpan(
text:
"${_questionController.questionNumber.value}. ",
style: TextStyle(fontSize: 15, color: Colors.blue),
children: [
TextSpan(
text: '${Questiontitle[widget.index]}',
style: TextStyle(fontSize: 15, color: Colors.blue),
),
],
),
),
),
Wrap title with Center or with Align and set alignment to Alignment.center.

How to add iconbutton at the end of the text in Flutter?

How can I add the iconbutton at the end of the text like this ?
Pic
I use Row widget but the iconbutton is not in the right place, it is not at the end of the text
Pic
This is my code:
Row(
children: [
Expanded(
child: Text(
'All about marketing, lead generation, content marketing and growth hacks',
style: const TextStyle(
fontSize: 12.0, fontWeight: FontWeight.w500),
),
),
IconButton(
icon: Icon(Icons.info),
iconSize: 12.0,
color: Palette.appbar,
onPressed: () {},
),
],
),
Use RichText with TextSpan and WidgetSpan.
WidgetSpan allow to add your own widget as a part of text.
like this :
RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: _text, style: _style),
WidgetSpan(
child: _child
)
],
),

Overflowing comment text not in alignment

I want to achieve following layout
There is profile picture, followed by username and followed by comment.But if the comment is too long it should starting coming below the username and not from the profile picture.
Following is my code
Row(
children: [
const CircleAvatar(
backgroundImage: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTTul_FjIE5w2p_VarQyzwaZ1mkOYjQRXcNCA&usqp=CAU'),
radius: 16,
),
const SizedBox(
width: 10,
),
Text(
'UserName',
style: GoogleFonts.tenorSans(
color: Colors.black),
),
Text(
'The Dram Club is a platform that endeavours to bring whisky enthusiassts & aficionados together for people to enjoy their whiskes',
style: GoogleFonts.tenorSans(
color: Colors.black,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic),
)
],
)
For the above code the comment text gets cuts off if its large so instead of using Row I decided to use Wrap but that is moving my enter comment in a new Row. I just tried wrapping the username and comment text in a Wrap but nothing happens. I tried using Flexible and Expanded widget as well
You can use this code.
Row(
children: [
const CircleAvatar(
backgroundImage: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTTul_FjIE5w2p_VarQyzwaZ1mkOYjQRXcNCA&usqp=CAU'),
radius: 16,
),
const SizedBox(
width: 10,
),
Flexible(
child: RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(text: 'Username ', style: TextStyle(fontSize: 18, color: Colors.black)),
TextSpan(text: 'The Dram Club is a platform that endeavours to bring whisky enthusiassts & aficionados together for people to enjoy their whiskes', style: TextStyle( color: Colors.black, fontSize: 16)),
],
),
),
)
],
),
Use RichText class, in this you can add multiple text with different styles. So the idea is
To have a single text, with different styles, one for UserName and other for Comment
children: [
CircleAvatar(
backgroundImage: NetworkImage('https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTTul_FjIE5w2p_VarQyzwaZ1mkOYjQRXcNCA&usqp=CAU'),
radius: 16,
),
SizedBox(
width: 10,
),
// here for User name and Comment
RichText(
text: TextSpan(
text: 'UserName ', // <-- Username text
style: GoogleFonts.tenorSans(color: Colors.black), // <-- User name style
children: <TextSpan>[
// your comment code
TextSpan(text: 'The Dram Club is a platform that endeavours to bring whisky enthusiassts & aficionados together for people to enjoy their whiskes',
style: GoogleFonts.tenorSans(color: Colors.black,fontWeight: FontWeight.bold, fontStyle: FontStyle.italic)))
]
)
)
]

How to show Icon in Text widget in flutter?

I want to show an icon in text widget. How do I do this ?
The following code only shows the IconData
Text("Click ${Icons.add} to add");
Flutter has WidgetSpan() to add a widget inside the RichText().
Example use:
RichText(
text: TextSpan(
children: [
TextSpan(
text: "Click ",
),
WidgetSpan(
child: Icon(Icons.add, size: 14),
),
TextSpan(
text: " to add",
),
],
),
)
Above code will produce:
You can treat the child of WidgetSpan like the usual widget.
Screenshot:
Using Wrap:
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
Text('Click'),
Icon(Icons.add),
Text('to add'),
],
)
Using Row:
Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('Click'),
Icon(Icons.add),
Text('to add'),
],
)
Using Text.rich:
Text.rich(
TextSpan(
children: [
TextSpan(text: 'Click'),
WidgetSpan(child: Icon(Icons.add)),
TextSpan(text: 'to add'),
],
),
)
An alternative might be to use emoji.
In Dart, strings supports escape sequences for special characters. For unicode emoji, you can use \u and curly braces with emoji hex code inside. Like this:
Text('Click \u{2795} to add')
The result is:
You can find a complete unicode emoji list here: http://unicode.org/Public/emoji/1.0/emoji-data.txt
Row Widget can be one solution for this issue, but you have to use different widgets to make it happen.
Follow below example.
Row(
children: <Widget>[
Text("Hi"),
Icon(Icons.add),
Text("Hello")
]
)
Another option is String.fromCharCode(int charCode)
This is icons.dart created by flutter team and charCodes are can found here.
RichText(
text: TextSpan(
style: TextStyle(
color: Colors.black,
fontSize: 24.0,
),
text: 'This is alarm icon : ',
children: <TextSpan>[
TextSpan(
text: String.fromCharCode(0xe190), //<-- charCode
style: TextStyle(
fontFamily: 'MaterialIcons', //<-- fontFamily
fontSize: 24.0,
color: Colors.red,
),
)
],
),
)
And result :
I think it's better to use the Row widget. here is an example
Row(
children: [
Icon(Icons.download_rounded),
Text(" Download")
],
)
You can use WidgetSpan() on Text.rich().
Here's a link for InlineSpan-class.
You can use this Package to achieve this.
I am not able to find the pub of this package.
Here is the Implementation.
RealRichText(
[
TextSpan(
text: "A Text Link",
style: TextStyle(color: Colors.red, fontSize: 14),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked.");
},
),
ImageSpan(
AssetImage("packages/real_rich_text/images/emoji_9.png"),
imageWidth: 24,
imageHeight: 24,
),
ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
imageWidth: 24,
imageHeight: 24,
margin: EdgeInsets.symmetric(horizontal: 10)),
TextSpan(
text: "哈哈哈",
style: TextStyle(color: Colors.yellow, fontSize: 14),
),
TextSpan(
text: "#Somebody",
style: TextStyle(
color: Colors.black, fontSize: 14, fontWeight: FontWeight.bold),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked");
},
),
TextSpan(
text: " #RealRichText# ",
style: TextStyle(color: Colors.blue, fontSize: 14),
recognizer: TapGestureRecognizer()
..onTap = () {
debugPrint("Link Clicked");
},
),
TextSpan(
text: "showing a bigger image",
style: TextStyle(color: Colors.black, fontSize: 14),
),
ImageSpan(AssetImage("packages/real_rich_text/images/emoji_10.png"),
imageWidth: 24,
imageHeight: 24,
margin: EdgeInsets.symmetric(horizontal: 5)),
TextSpan(
text: "and seems working perfect……",
style: TextStyle(color: Colors.black, fontSize: 14),
),
],
);
You can also check out below issue for more:
Flutter Issue #2022
For simple icons, You can use a Unicode character as regular text, just copy and paste.
You can search and copy Unicodes from this site
Text("Enjoy! 🔎❌💖🔥✅💜😍")
This method will work with any Text widget and package.
You can use this function:
Widget _spanIcon(String text, IconData icon, TextStyle st) {
return Row(
children: [
Text(text,style: st,),
Text(
String.fromCharCode(icon.codePoint), //<-- charCode from icon data
style: TextStyle(
fontFamily: icon.fontFamily, //<-- fontFamily from icon data
fontSize: st.fontSize! + 4.0,//you can increase font size for icon here
color: st.color,
),)
],
);}
and then use it :
_spanIcon('11',Icons.access_alarm, TextStyle(fontSize: 14.0, color: Color.fromARGB(113, 0, 0, 0))),