Overflowing comment text not in alignment - flutter

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

Related

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

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

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 can I modify the size of my TextButton so that it is equal to it's child Text widget's height in Flutter?

I am creating an App Login page using Flutter framework and I want the the TextButton as well as the "Forgot password?" text to be horizontally aligned in the screen, but the following code displays the button below the "Forgot Password?" text. I tried modifying the button's height and width parameters as well as modifying the spacing in the Wrap widget but I still didn't get the expected outcome. Wrapping the TextButton with SizedBox widget too didn't worked as it hid the button from the screen. Is there any other way to get the desired outcome?
Part of the screen where the TextButton and "Forgot password?" text is displayed
Wrap(
children: [
Text(
"Forgot password?",
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 16.0,
),
),
TextButton(
onPressed: () {},
child: Text(
"Click here",
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 16.0,
decoration: TextDecoration.underline,
),
),
),
],
),
if you want to Align your two buttons horizontally, just wrap them with a Row widget, userSizedbox to adjust space between them, and if necessary wrap the Row with Padding to adjust the location of buttons like;
Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: Row(
children: [
Text(
"Forgot password?",
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 16.0,
),
),
SizedBox(height: 10,),
TextButton(
onPressed: () {},
child: Text(
"Click here",
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 16.0,
decoration: TextDecoration.underline,
),
),
),
],
),
),
Try to below code hope its helpful to you. Wrap your Text and TextButton widget inside Row().
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
"Forgot password?",
),
TextButton(
onPressed: () {
//Write here your onPressed function call
print('Button Pressed!');
},
child: Text(
"Click here",
),
),
],
),
Your Result Screen->
There is another approach to overcome this with RichText, TextSpan and TapGestureRecognizer.
To use TapGestureRecognizer, you will have to import 'package:flutter/gestures.dart'
RichText(
text: TextSpan(
text: "Forgot password? ",
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 16.0,
),
children: [
TextSpan(
text: "Click here",
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 16.0,
decoration: TextDecoration.underline,
),
recognizer: TapGestureRecognizer()
..onTap = () => {},
),
],
),
)
You can use this instead of using a TextButton.
Result is as below.

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

Flutter: Links in long text like Privacy policy and Tems of Service

I'm trying to make privacy policy and terms of service link.
Widget privacyPolicyLinkAndTermsOfService() {
return Container(
child: Center(
child: Text(
'By continuing, you agree to our Terms of Service and Privacy Policy.',
style: TextStyle(
fontSize: 14.0,
),
),
),
);
In this way the text doesn't overflow but I cannot make a part of text link. So I tried to use Row and separate the text and the link part as children.
However, the text overflows when I use Row and separate text as children.
How can I prevent them from overflowing while making a part of text link?
Use RichText class to link parts of text. It's constructor text.rich allows us to style different parts of the text. Below is the working code that only shows links for Terms of Service and Privacy Policy which user can tap on to open respective links and rest of the text is plain text:
Widget privacyPolicyLinkAndTermsOfService() {
return Container(
alignment: Alignment.center,
padding: EdgeInsets.all(10),
child: Center(
child: Text.rich(
TextSpan(
text: 'By continuing, you agree to our ', style: TextStyle(
fontSize: 16, color: Colors.black
),
children: <TextSpan>[
TextSpan(
text: 'Terms of Service', style: TextStyle(
fontSize: 16, color: Colors.black,
decoration: TextDecoration.underline,
),
recognizer: TapGestureRecognizer()
..onTap = () {
// code to open / launch terms of service link here
}
),
TextSpan(
text: ' and ', style: TextStyle(
fontSize: 18, color: Colors.black
),
children: <TextSpan>[
TextSpan(
text: 'Privacy Policy', style: TextStyle(
fontSize: 18, color: Colors.black,
decoration: TextDecoration.underline
),
recognizer: TapGestureRecognizer()
..onTap = () {
// code to open / launch privacy policy link here
}
)
]
)
]
)
)
),
);
}
Above code is rendered in UI as:
TapGestureRecognizer() method requires you to import below file:
import 'package:flutter/gestures.dart';
Hope this answers your question.