flutter title automatically go to next line - flutter

the code below is in wrapped in a row. i need help in getting the text to automatically go to the next line when there's not enough space. ive been trying to wrap in expanded and stuff but it is not working and throwing me the wrong use of parent widget error. tia
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
Helpers.getChannelName(channel, context.currentUser!),
overflow: TextOverflow.visible,
style: GoogleFonts.lato(
fontSize: 21,
),
maxLines: null,
),
const SizedBox(height: 2),
],
)),

AppBar has limited height, you can have some(2/3) lines like
Text(
Helpers.getChannelName(channel, context.currentUser!),
style: GoogleFonts.lato(
fontSize: 21,
),
maxLines: 3,
softWrap: true,
),
Also, you can try changing text size by wrapping Text widget with FittedBox or using auto_size_text

Related

In Flutter, inside Row widget, I have taten two Text widget, I want space between them (like we do with "&nbsp" in Html),

In the above code I used Sized Box Widget but it didn't gives me anything?
How can I add space in it?
Android Emulator Images
Padding(padding: EdgeInsetsDirectional.fromSTEB(24, 4, 24, 0),
child:
Row(mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Discount',
style: AppTheme.of(context).subtitle2,
),
SizedBox(height: 20,),
Text(
'₹ ${Provider.of<Cart>(context,listen:false).discountPrice}',
style: AppTheme.of(context).subtitle1,
),
],
),
),
wrap with container/sizebox and add in row mainAxisAlignment: MainAxisAlignment.spaceAround
Using TextSpan() widget is better than instead of Row(). Like that.
RichText(
text: TextSpan(
children: [
TextSpan(text: 'Discount',style: AppTheme.of(context).subtitle2,),
WidgetSpan(
child: SizedBox(
width: 20, // your of space
),),
TextSpan(text: "${Provider.of<Cart(context,listen:false).discountPrice}",
style: AppTheme.of(context).subtitle1,),
],
),),
Edit: but I think in your question your code and your screen shoot is different. Please ask clearly. ** My answer is to space between horizontal text widget. **
That's a quite good approach to use SizedBox in this case, but you need to set width instead of height.

Spacer cause overflow issue

I have two texts in a row, and one of them is way longer than the other one. I use spacer to give more spaces to one of the texts. If the text gets really long, it causes an overflow. How do I fix this?
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Spacer(),
Text(
"Longer text",
style: Theme.of(context).textTheme.bodyText1?.copyWith(
color:
Theme.of(context).colorScheme.white.withOpacity(0.85),
),
overflow: TextOverflow.fade,
softWrap: false,
textAlign: TextAlign.right,
),
const SizedBox(width: 10),
Text(
"smaller text",
style: Theme.of(context).textTheme.bodyText1?.copyWith(
color:
Theme.of(context).colorScheme.white.withOpacity(0.85),
),
overflow: TextOverflow.fade,
softWrap: false,
textAlign: TextAlign.left,
),
Spacer(),
],
),
wrap the text widget with the Expanded widget and give them spacing whatever you want
Expanded(
flex: 1,// if you give them 1 flex value to Expanded widget both they take equal space and if you give one widget 2 flex value and one 1 flex value the one with 2 flex value will get double the space of the other
child:Text("Longer text")
)
You need to use RichText like this:
Center(
child: RichText(
textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.ellipsis,
text: TextSpan(
text: 'smaller text',
style: TextStyle(
color: Colors.black.withOpacity(0.85),
),
children: [TextSpan(text: 'Longer text ')],
),
),
),

I want to align the starting place of two texts

I have one "row" and two "text" widgets inside. I want to align the starting place of two texts. ie "m. name" and "m105" should line up.
Container(
child: Row(
children: [
Container(
child: Align(
alignment: Alignment.topLeft,
child: Text(
"M. Adı: ",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
),
// SizedBox(
// width: 20,
// ),
Expanded(
child: Text(
"$productName",
overflow: TextOverflow.ellipsis,
maxLines: 3,
softWrap: true,
),
),
],
),
),
Try
Row() widget attribute
crossAxisAlignment: CrossAxisAlignment.start
Text() widget attribute
textAlign: TextAlign.start,
/// Try something like that
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"M. Adı: ",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
SizedBox(width: 5),
Flexible(
child: Text(
"$productName",
textAlign: TextAlign.start,
),
),
],
),
Add crossAxisAlignment: CrossAxisAlignment.center, to your Row widget it should be aligned as you desired or still if it doesnt works just try changing center with other values check which one works for you...
and even after adding above property to your Row widget then you would have to remove alignment of topleft in your text of m.name

Can I use an Icon by passing a string in a text widget like Html? flutter

I am trying to make a text which contains an icon. I found out Icon has a kind of code inside. Is it possible to get an icon by passing the code in a text? like Html does.
Or should I use textspan and richtext?
static const IconData logout = IconData(0xe848, fontFamily: 'MaterialIcons');
this is icons.dart in flutter
<input type="submit" class="search-submit" value="" />
In Html.
I would like to get a material icon by using the code in icons.dart.
I think if there is a way to implement an icon in text widget. Its gonna be an easier way than to use richtext.
Iconbutton may work but my text is in already inkwell widget(the container widget is wrapped with an inkwell). I have some memos which contain tags. And I would like to show tags with an icon before getting in content screen.
So like this Text(icon + memo.tags.join(icon))
I am trying to add the text in the column.
Container(
width: MediaQuery.of(context).size.width,
height: 120,
child: Row(
children: [
setImage(memo.imageByte),
Expanded(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
memo.title == null ? "" : memo.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
fontSize: 32, fontWeight: FontWeight.bold),
),
Text(
memo.content == null ? "" : memo.content,
maxLines: 4,
overflow: TextOverflow.ellipsis,
),
Text(
memo.editTime.split('.')[0],
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
),
),
],
),
),
You can simply use named constructors for creating different types of buttons with icons. For instance
FlatButton.icon(
onPressed: null,
icon: icon: Icon(Icons.add),
label: Text("Submit")
);

Align text baseline with text inside a column, using Flutter

I'm having trouble aligning widgets. I have seen questions on here asking about similar things, however, none of them seem to work for my specific task. I'm trying to create a widget containing an icon and two text widgets. The icon is supposed to be centered over the first text widget, and the other text widget is supposed to be aligned with the baseline of the first.
The way I have gone about doing this is to place the icon and the first text widget in a column, and then place that column in a row together with the second text widget. I'm using crossAxisAlignment: CrossAxisAlignment.baseline and textBaseline: TextBaseline.ideographic in the row widget but for some reason the second text aligns with the icon. It looks like this:
If I instead use crossAxisAlignment: CrossAxisAlignment.end the text widgets do not align properly:
I have tried to put the text widgets in a row and place them in a column together with the icon, but then the icon is no longer centered over the first text widget:
Here's the code used for the first image, the other images are just rearrangements of the same code.
return Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.ideographic,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
widget.icon,
color: color_text_highlight,
size: 20,
),
Text(
widget.data,
style: TextStyle(
fontSize: 28,
fontFamily: 'Quicksand',
color: color_text_highlight,
),
),
],
),
SizedBox(width: 5),
Text(
widget.label,
style: TextStyle(
fontSize: 12,
fontFamily: 'Roboto',
color: color_text_dark,
)
),
],
);
Any help is appreciated!
Column defines its own baseline as a baseline of its first child*. That's why the label at right gets aligned to the bottom of icon:
A simple fix is to make "17:59" the first child of the Column (the icon will go below now) and then use VerticalDirection.up to swap them back:
Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Column(
mainAxisSize: MainAxisSize.min,
verticalDirection: VerticalDirection.up, // <-- reverse direction
children: [
Text('17:59', style: TextStyle(fontSize: 32)), // <-- first child
Icon(Icons.timer),
],
),
Text('mm:ss'),
],
)
* actually, of first child that has a baseline itself, anyway Icon does have it
There may be a solution like this, but the solution may cause problems because it depends on the font size. You will need to change the padding value accordingly.
Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Column(
children: [
Icon(
Icons.timer,
color: Colors.lightGreen,
size: 20,
),
Text(
"17:59",
style: TextStyle(
fontSize: 28,
fontFamily: 'Quicksand',
color: Colors.lightGreen,
),
),
],
),
SizedBox(width: 5),
Padding(
padding: EdgeInsets.only(bottom: 4),
child: Text("mm:ss",
style: TextStyle(
fontSize: 12,
fontFamily: 'Roboto',
color: Colors.white70,
)),
),
],
),
],
)
Google Nexus 9 Tablet
Google Pixel 3a XL
Google Pixel 2
Row allows for a crossAxisAlignment of baseline, set the text baseline too or it will throw an error
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,