I want fixed the Right Text , Left Text using remaining space - flutter

I have two widget : Text A, Text B, Text A is very long text,
B is right , I want to fix B position and full visible, and let A widget using remaining space and new line to show B text.
Here is the code:
Row(
children: <Widget>[
Text('AAAAAAAAAAAA--------------------------------------------------------'),
//Spacer(),
Text(
"BBBBBBBBB",
style: TextStyle(fontSize: 13, color: Color(0xFF666666)),
)
],
)

Row(
children: <Widget>[
Expanded(child: Text(
'AAAAAAAAAAAA---------------------------------------------------------------------------------------------------')),
//Spacer(),
Text(
"BBBBBBBBB",
style: TextStyle(fontSize: 13, color: Color(0xFF666666)),
)
],
)

Related

Flutter reverse order of Children inside Column

Is there a way to reverse the order of Children inside Column?
Because I'm using a custom Widget and the widget appears two times: first time the order is good, the second time order is reversed.
Need to reverse the order of Text elements in Column:
Column(
verticalDirection: VerticalDirection.up,
children: [
Text(
dTDInfoValue,
style: const TextStyle(
fontSize: AppFontSizes.aboutMountainDTDInfoText,
color: AppColors.aboutMountainDTDInfoColor,
fontWeight: FontWeight.w400),
),
const SizedBox(height: AppConstants.aboutMountainDTDSizedBoxHeight),
Text(
dTDInfoName,
style: const TextStyle(
fontSize: AppFontSizes.aboutMountainDTDNameText,
color: AppColors.aboutMountainDTDInfoNameColor),
)
],
)
Use the List<Widget> to store the Widget you need.
List<Widget> _widgetList = [/* your Widgets */];
Column(
verticalDirection: VerticalDirection.up,
children: [
for(int i=0; i<_widgetList.length; i++)
_widgetList[i];
],
)
Use the reversed method of List to reverse the order.
_widgetList.reversed;
You are trying to change the order of item, It will be easier to make a list and then reverse if needed.
You can do something like
body: Column(
// verticalDirection: VerticalDirection.up,
children: [
...[
Text(
"XdTDInfoValue",
style: const TextStyle(fontWeight: FontWeight.w400),
),
const SizedBox(height: 100),
Text(
"dTDInfoName",
),
].reversed.map((e) => e).toList()
],
),
Create a state variable of type List
say
List<Widget>myList=[Widget1, Widget2, Widget3...],
and to reverse use method myList.reversed.
Assign this variable to Column children and use myList.reversed within setState whenever you need to reverse the list.
List<String> randomWords = ['abc', 'xyz', '123'];
List<String> reversedRandomWords = randomWords.reversed.toList();
print(reversedRandomWords); // [ '123','xyz', 'abc'];

Flexible widget didn't work on text widget flutter

I'm trying to use flexible on my text cause it's overflow but for some reason expanded or neither flexible didn't work. But it work on other text widget on different screen. Anyone know why ? How can I fix this ?
return Row(
children: [
/// Ticket Details
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
/// Ticket Title
Flexible(
child: Text(
ticketData['title'],
style: primaryColor700Style.copyWith(
fontSize: fontSize18,
),
),
),
SizedBox(height: 8),
/// Date Created
Text(
'Created : ' +
DateFormat('d MMM y').format(
DateTime.parse(
ticketData['date_created'].toDate().toString(),
),
),
style: primaryColor400Style.copyWith(
fontSize: fontSize12,
),
),
],
),
/// Urgent Icon
if (ticketData['is_urgent'])
Icon(
Icons.warning_rounded,
size: 35,
color: warningColor,
),
],
);
Wrap the column with flexible
Row(
children: [
Flexible(
child: Column(
children:[
Text(),
]
)
)
]
)
Row takes infinite width, To get available width row for next children you can wrap Expanded/ Flexibile/ fixed width widget. You can check this doc more about.
You can find this from Layout algorithm on Row
Expanded, to indicate children that should take all the remaining room.
Flexible, to indicate children that should share the remaining room but that may by sized smaller (leaving some remaining room unused).

Make Container fill TableCell in Flutter

I'm trying to make a Table with two cells of the same width and height, so that the height depends on the size of the content. However, the smaller TableCell always shrinks:
This is what I'm trying to implement:
Here's the code:
Table(
children: [
TableRow(
children: [
TableCell(
child: Container(
color: Colors.green,
child: Text(
'long text long text long text long text long text long text long text'),
),
),
TableCell(
child: Container(
color: Colors.orange,
child: Text('short text'),
),
),
],
)
],
),
P.S. I could solve it by adding verticalAlignment: TableCellVerticalAlignment.fill, to the smaller cell, but any cell can be the smaller one, depending on the content. When I add this line to both cells, the whole table disappears. The only bypass I could imagine is to calculate the length of the content and find the smaller cell, but I wonder if there is a way to implement this UI directly with Flutter.
Would appreciate any help.
1. Row with IntrinsicHeight
IntrinsicHeight limits the height of the Row to the content size, which however is considered a 'relatively expensive' approach and is not recommended.
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Container(
color: Colors.green,
child: Text(
'long text long text long text long text long text',
))),
Expanded(
child: Container(
color: Colors.orange,
child: Text(
'short text',
))),
],
),
),
2. Table with TableCellVerticalAlignment.fill
As mentioned in the question, the .fill option must not be used in the largest TableCell, because in this case the TableRow will have zero height. This is the preferred solution, because it doesn't have the 'expensiveness' issue of the previous one.
final texts = ['long text long text long text long text long text', 'short text'];
final colors = [Colors.green, Colors.orange];
// find the longest text and its index
final max = texts.asMap().entries.reduce(
(a, b) => (a.value.length > b.value.length) ? a : b,
);
return Table(children: [
TableRow(
children: texts
.asMap()
.entries
.map((e) => TableCell(
// use .fill in all cells except the largest
verticalAlignment: (e.key != max.key)
? TableCellVerticalAlignment.fill
: TableCellVerticalAlignment.top,
child: Container(
color: colors[e.key],
child: Text(e.value),
)))
.toList(),
),
]);

How to use overflow with conditional statement in flutter

I get a lengthy result (location address) in my text box leading it to an error RIGHT OVERFLOW BY 226 PIXEL. I tried using overflow but the result is still the same.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(Provider
.of<AppData>(context)
.pickUpLocation != null
? Provider
.of<AppData>(context)
.pickUpLocation
.placeName
: "Add Home",overflow: TextOverflow.ellipsis),
SizedBox(
height: 4.0,
),
Text("Your home Address",
style: TextStyle(color: Colors.black54, fontSize: 12.0)),
],
)
Next, I tried using Flexible but the text is cut into half
Wrap Text widget into FittedBox with fit: BoxFit.scaleDown

How to make text responsive in Flutter?

I'm struggling to make my text responsive in my application I'm re-writing from Kotlin to Flutter.
The point is that I have a text widget, which needs to be responsive. When I open it on a phone with 16:9 screen ratio, it's quite ok, but when I open my application on a phone with 18:9 screen ratio, the text doesn't fill the remaining space.
In Kotlin I had Contraint layout with guidelines which made the job very easy, but i don't know how to do it in Flutter.
I'm using AutoTextSize package, but it doesn't work which I intend it to work.
In my Kotlin app, it looks like this
In Flutter on my Samsung Note 9 with screen ratio 18:9 looks like this:
My code:
import 'package:flutter/material.dart';
import 'package:auto_size_text/auto_size_text.dart';
import '../../helpers/makdolan.dart';
class GeneratedCouponScreen extends StatelessWidget {
final String couponImage;
GeneratedCouponScreen({Key key, #required this.couponImage}) : super(key: key);
#override
Widget build(BuildContext context) {
var _makdolan = Makdolan();
var now = _makdolan.calculateDate();
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: Container(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('DATA WYDANIA:', style: TextStyle(color: Colors.black, fontSize: 16.0, fontWeight: FontWeight.bold)),
Text(now, style: TextStyle(color: Colors.black, fontSize: 16.0))
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('UNIKALNY KOD:', style: TextStyle(color: Colors.black, fontSize: 16.0, fontWeight: FontWeight.bold)),
Text(_makdolan.calculateUniqueCode(), style: TextStyle(color: Colors.black, fontSize: 16.0))
],
)
],
),
SizedBox(height: 8.0),
Image.asset(couponImage),
SizedBox(height: 8.0),
AutoSizeText.rich(
TextSpan(
text: 'Kupon ten upoważnia do jednokrotnego odbioru produktu gratis przy kolejnym dowolnym zakupie z oferty klasycznej. Kupon ten ważny jest przez 7 dni od czasu jego wygenerowania i może być zrealizowany w dowolnej restauracji McDonald\'s w Polsce z wyłączeniem restauracji znajdyujących się na terenie Portu Lotniczego im. Fryderyka Chopina w Warszawie oraz Portu Lotniczego im. Lecha Wałęsy w Gdańsku. Szczegółowy regulamin ankiety „Opinia Gości" znajduje się na stronie ',
style: TextStyle(color: Colors.black),
children: [
TextSpan(
text: 'www.mcdonalds.pl ',
style: TextStyle(color: Color(0xffffc300), decoration: TextDecoration.underline)
),
TextSpan(
text: 'w sekcji ',
style: TextStyle(color: Colors.black)
),
TextSpan(
text: 'Regulaminy',
style: TextStyle(color: Color(0xffffc300), decoration: TextDecoration.underline)
)
]
),
maxLines: 12,
),
Spacer(),
Card(
child: Container(
height: 95.0,
color: Color(0xffffc300),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('DRUKUJ /', style: TextStyle(fontSize: 28.0)),
Text('ZAPISZ JAKO PDF', style: TextStyle(fontSize: 28.0),)
],
),
)
),
),
Card(
child: Container(
height: 95.0,
color: Color(0xffffc300),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('PRZEŚLIJ KUPON', style: TextStyle(fontSize: 28.0)),
Text('(WYSYŁKA W CIĄGU 24 GODZIN)', style: TextStyle(fontSize: 17.0),)
],
),
)
),
)
],
),
),
)
);
}
}
You might try screenutil package.
The approach is basically getting a ratio based on screen width/height/dpi. So you can adjust any UI element (like fontsize) accordingly. So the idea is more or less to match the size of fonts on ur original tested device (where you have the design) and adapt it to other resolutions.
The idea is basic equation:
DesignScreenWidth -> 24pt(current font size)
Note9ScreenWidth -> x (adjusted font size for note9)
x = Note9ScreenWidth * 24 / DesignScreenWidth
So, that's how you get any ratio to adjust content based on width, height, screen PPI, or whatever. You basically treat the values as proportions and multiply by that normalization factor
currentFactor=currentValue/designedValue.
Hope it clarifies a little the concept for “multiresolution awareness”
Best easiest way to to make responsive Text for different screen size is Sizer plugin.
Check it this plugin ⬇️
https://pub.dev/packages/sizer
.sp - for font size
If you want to set responsive text size in any screen size device also tablet then use .sp after value.
Example:
Text('Sizer', style: TextStyle(fontSize: 12.0.sp)) //use SomeValue.sp in fonsize for responsive text
Also you can use this plugin for responsive widget
.h  - for widget height
.w  - for widget width
Example:
Container(
height: 10.0.h, //10% height of screen
width: 80.0.w, //80% width of screen
child: Text('Sizer', style: TextStyle(fontSize: 12.0.sp)),
);
hi you some one else mention it in the above answer that you can use the screenutil package for text resizing which is kind of good . but I would also like to recommend to try FittedBox widgets in flutter it is also helpul some time you need fitted box with screen util in some situation screen util does not help a lot. although it is a good package for make your design responsive . or you can also try to use different text size size for different screen size .
The is a Flutter package called auto_size_text 2.1.0 which may well address your problem.
https://pub.dev/packages/auto_size_text/versions/2.1.0#-readme-tab-