I want the arrow_right icon close after the Text Widget, but I have to wrap the text with a Expanded as it might overflow
I think this can solve it.
Row(
children: const [
Expanded(
child: Text(
'this line is overflow, but icon is in right position',
style: TextStyle(fontSize: 12),
softWrap: false,
maxLines: 2,
overflow: TextOverflow.ellipsis, // new
),
),
],
)
or
Row(
children: const [
Expanded(
child: FittedBox(
child: Text(
'this line is overflow, but icon is in right position',
style: TextStyle(fontSize: 12),
softWrap: false,
overflow: TextOverflow.ellipsis,
),
),
),
],
)
code Example
return Scaffold(
body: SafeArea(
child: Expanded(
child: ColoredBox(
color: Colors.deepOrangeAccent,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Line 1',
style: TextStyle(fontSize: 24),
),
Row(
children: const [
Container(
child: Text(
'this line might overflow',
style: TextStyle(fontSize: 12),
),
),
Icon(Icons.arrow_right),
],
),
Row(
children: const [
Expanded(
child: Text(
'this line is overflow, but icon is in right position // this line is overflow, but icon is in right position',
style: TextStyle(fontSize: 12),
softWrap: false,
//maxLines: 2,
overflow: TextOverflow.ellipsis, // new
),
),
Icon(Icons.arrow_right),
],
),
],
),
),
),
),
);
result Image
I'll tell you two ways I know of.
Use Container Widget
How to fix the size by giving width: This method allows you to attach an icon next to the text.
Use Expanded Widget
'Expanded' is a percentage. Since you are using one Expanded widget now, the Text widget is taking up all the remaining space except for the space for the Icon widget.
You can use it according to the conditions of your app.
finally, Flexible can do it!!!
You should try this solution
main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
SizedBox(
height: 130,
child: Row(
children: [
Expanded(
flex: 2,
child: Container(
color: Colors.yellowAccent,
),
),
Expanded(
flex: 5,
child: Column(
children: [
Expanded(
flex: 5,
child: Container(
color: Colors.deepOrangeAccent,
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Expanded(
flex: 2,
child: FittedBox(
child: Text("line 1"),
),
),
Expanded(
flex: 3,
child: Column(
children: [
Expanded(
child: Row(
children: const [
Expanded(
child: Text(
"this line might overflow",
overflow: TextOverflow.ellipsis,
),
),
AspectRatio(
aspectRatio: 1,
child: FittedBox(
child: Icon(Icons.arrow_right)),
),
],
)),
Expanded(
child: Row(
children: const [
Expanded(
child: Text(
"this line is overflow, the icon is in right position",
overflow: TextOverflow.ellipsis,
),
),
AspectRatio(
aspectRatio: 1,
child: FittedBox(
child: Icon(Icons.arrow_right)),
),
],
)),
],
),
),
],
),
),
),
Expanded(
flex: 3,
child: Container(
color: Colors.deepPurpleAccent,
),
),
],
),
),
],
),
),
],
),
);
}
}
Solution Image
N.B: This Solution is totally responsive.
******* TRY THIS ********
Text("Any Text Add",
textScaleFactor: 0.85,
style: TextStyle(
fontSize: 12
),
)
Related
I want to align the text "2022" on the bottom of the screen while keeping the image and its neighbor text at the center of the screen.
class SplashScreen extends StatelessWidget {
const SplashScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Globalcolors.mainColor,
body: Container(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Image.asset(
'assets/images/splash_logo.png',
color: Colors.white,
),
),
const Padding(
padding: EdgeInsets.all(50),
child: Text(
'Sample Text',
style: TextStyle(
color: Colors.white,
fontSize: 36,
fontFamily: 'MouseMemoirs'),
),
),
const Text(
'2022',
style: TextStyle(color: Colors.white, fontSize: 12),
),
],
),
),
);
}
}
I tried to use this link to solve the problem. I used Expanded:
const Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Text(
'2022',
style: TextStyle(color: Colors.white, fontSize: 12),
),
),
)
But, The result is not good:
You can user Stack and column widgets like
Stack(
children: [
Column(), // contains the image and title
Align(
alignment: Alignment.bottomCenter,
child: Text("2022"),
),
]
)
You can also use Positioned widget to align the text 2022
you can use the bottom method that comes with the scaffold
Scaffold(
bottom : Text("2020")
)
this will make the text always on the bottom of the screen
I try to display a ExpansionTile with some text in it followed by an Image. Referring to the screenshot you can see that there is some space between both widgets. Does someone know how I can reduce this space?
class Test extends StatelessWidget {
const Test({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: backGround,
appBar: AppBar(
title: const Text('Aromatics'),
backgroundColor: appbarColor,
),
body: Column(
children: [
ExpansionTile(
iconColor: titleColor,
title: const Text('Tst', style: TextStyle(color: titleColor)),
subtitle: const Text('Test subtitle',
style: TextStyle(color: Colors.white)),
children: [
Container(
margin: const EdgeInsets.only(left: 20),
child: Column(
children: const [
Text('Test text',
style:
TextStyle(color: Colors.white, fontSize: 13)),
],
),
),
]),
const SizedBox(
height: 1,
),
Expanded(
child: Image.asset('assets/images/test.webp'),
),
],
));
}
}
As per the ExpansionTile Doc, you can use childrenPadding to manage the padding for the children widget
Specifies padding for children.
If this property is null then ExpansionTileThemeData.childrenPadding is used. If that is also null then the value of childrenPadding is EdgeInsets.zero.
Won't removing Expanded suit your goals?
...
const SizedBox(
height: 1,
),
Image.asset('assets/images/test.webp'),
...
Just remove the Expanded widget around the image because it is taking all the remaning space in the Column for the image.
Current Code:
Column(
children: [
...
Expanded(
child: Image.asset('assets/images/test.webp'),
),
...
],
),
New Code:
Column(
children: [
...
Image.asset('assets/images/test.webp'),
...
],
),```
I am unable to make my text stop extending the screen and overflowing. My code is as follows:
class OrderTileDisplay extends StatelessWidget {
final MenuOrderData orderItem;
final editable;
OrderTileDisplay(this.orderItem, {this.editable = true});
#override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
GestureDetector(
onTap: (){},
child: Container(
color: Colors.transparent,
margin: EdgeInsets.symmetric(vertical: 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
orderItem.quantity.toString() + 'x',
style: Title16,
),
SizedBox(
width: 8,
),
Text(
orderItem.name, // OVERFLOWS WHEN IT IS A LONGER SENTENCE
style: Title18bold,
overflow: TextOverflow.ellipsis,
),
],
),
Row(
children: [
editable? Icon(Icons.edit, color: Colors.grey[200], size: 20,) : Container(),
SizedBox(width: 8,),
Text("£" + Provider.of<CP>(context).getTotalForItem(orderItem).toStringAsFixed(2),
style: Title18bold,),
],
),
],
),
),
),
Container(... and other stuff)
I have tried putting Text inside a container then Expanded widget but I keep getting errors. Not sure how to solve it.
Try to wrap your Row and Text inside a Flexible:
Flexible( //flexible here
child: Row(
children: [
Text(
orderItem.quantity.toString() + 'x',
style: Title16,
),
SizedBox(
width: 8,
),
Flexible( //and here
child: Text(
orderItem.name, // no more overflow
style: Title18bold,
overflow: TextOverflow.ellipsis, //working as expected
),
),
],
),
),
As the documentation states, TextOverflow says how overflowing text should be handled, but it will not work if it's parent don't have a fixed size. In this case, Flexible is been used to restrict the total size of the text to prevent overflow, when it reaches a very large width.
i tried to reduce space between Rows(Textfields) with height proprety,but it doesn't work,Sizedbox didn't work as well,can't omit expanded widget because of my filterList(it shows“A RenderFlex overflowed by pixels ” error),i tried to fix it with flex Value but it doesn't work too.
any Idea how can i fixt it?!
my emulator screenshot
import 'package:flutter/material.dart';
import 'package:filter_list/filter_list.dart';
class FilterPage extends StatefulWidget {
const FilterPage({Key key, this.allTextList}) : super(key: key);
final List<String> allTextList;
#override
_FilterPageState createState() => _FilterPageState();
}
class _FilterPageState extends State<FilterPage> {
#override
Widget build(BuildContext context) {
List<String> countList = [
"Art",
"Mt",
"P",
"Pl"
];
return Scaffold(
appBar: AppBar(
title: Text("Filter list Page"),
),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: FilterListWidget(
allTextList: countList,
height: MediaQuery.of(context).size.height,
hideheaderText: true,
selectedTextBackgroundColor: Colors.red,
applyButonTextBackgroundColor: Colors.red,
allResetButonColor: Colors.grey,
onApplyButtonClick: (list) {
//Navigator.pop(context, list);
},
),
),
Expanded(
child: Row(
children: [
Container(
width: 180,
child: TexstInput(lable: 'min-Upvote',icons: Icons.favorite,),
),
Container(
width: 180,
child: TexstInput(lable: 'max-Upvote'),
),
],
),
),
Expanded(
child: Row(
children: [
Container(
width: 180,
child: TexstInput(lable: 'min',icons: Icons.person_rounded,),
),
Container(
width: 180,
child: TexstInput(lable: 'max'),
),
],
),
),
Container(
child: RaisedButton(child:Text(
'apply'
),),
),
],
),
),
);
}
}
class TexstInput extends StatelessWidget {
TexstInput({
#required this.lable,this.icons
}) ;
IconData icons;
String lable;
#override
Widget build(BuildContext context) {
return TextField(
keyboardType: TextInputType.number,
decoration: InputDecoration(
icon: Icon(icons),
contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
labelText: lable,
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.red, width: 5.0),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey, width: 0.8),
)
),
);
}
}
main
import 'package:flutter/material.dart';
import 'filter.dart';
void main() async{
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.red,
),
debugShowCheckedModeBanner: false,
home:FilterPage(),
);
}
}
Not 100% sure how you imagine your layout.
You plan to add more search tags? Change the flex values if you want to.
If you want to have your rows right under the FilterListWidget, than add mainAxisAlignment: MainAxisAlignment.start to second Column.
SafeArea(
child: Column(
children: [
Flexible(
flex: 2,
child: FilterListWidget(
allTextList: countList,
hideheaderText: true,
selectedTextBackgroundColor: Colors.red,
applyButonTextBackgroundColor: Colors.red,
allResetButonColor: Colors.grey,
onApplyButtonClick: (list) {
//Navigator.pop(context, list);
},
),
),
Flexible(
flex: 3,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Row(
children: [
Container(
width: 180,
child: TexstInput(lable: 'min-Upvote',icons: Icons.favorite,),
),
Container(
width: 180,
child: TexstInput(lable: 'max-Upvote'),
),
],
),
Row(
children: [
Container(
width: 180,
child: TexstInput(lable: 'min',icons: Icons.person,),
),
Container(
width: 180,
child: TexstInput(lable: 'max'),
),
],
),
Container(
child: RaisedButton(child:Text(
'apply'
),),
),
],
),
),
],
),
)
Try with the below lines
Expanded(
child: Row(
children: [
Container(width: 2 ),
Expanded(
child: TexstInput(lable: 'min-Upvote',icons: Icons.favorite,),
),
Container(width: 2 ),
Expanded(
child: TexstInput(lable: 'max-Upvote'),
),
Container(width: 2 ),
],
),
),
Expanded(
child: Row(
children: [
Container(width: 2 ),
Expanded(
child: TexstInput(lable: 'min',icons: Icons.person_rounded,),
),
Container(width: 2 ),
Expanded(
child: TexstInput(lable: 'max'),
),
Container(width: 2 ),
],
),
),
I am trying to show some data from the database and my app must contain UI like this.
But I am encountering this kind of problem.
Problems:
The text is overflowing and not wrapping (I tried to use Flexible and Expanded but it produces more exceptions, mostly of non-zero flex and so on)
The list needs fixed height and width, whereas I need height to match_parent. double.infinity don't work as well.
Here is my code:
class CategoryDetailPage extends StatefulWidget {
final Category category;
CategoryDetailPage({Key key, this.category}) : super(key: key);
#override
_CategoryDetailPageState createState() => _CategoryDetailPageState();
}
class _CategoryDetailPageState extends State<CategoryDetailPage> {
DatabaseProvider databaseProvider = DatabaseProvider.instance;
List<Phrase> phrases;
final List<Color> _itemColors = [
Color(0xff16a085),
Color(0xff2980b9),
Color(0xff8e44ad),
Color(0xff2c3e50),
Color(0xffd35400),
Color(0xffbdc3c7),
Color(0xff27ae60),
Color(0xfff39c12),
Color(0xff7f8c8d),
Color(0xffc0392b),
];
int _colorCounter = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Row(
children: [
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image(
image: AssetImage("assets/images/categories/${widget.category.image}"),
width: 32,
),
),
),
Padding(
padding: const EdgeInsets.only(left: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
"Say ${widget.category.name}",
style: TextStyle(fontSize: 24, fontFamily: "Pacifico"),
),
Text(
"\"${widget.category.quote}\" --${widget.category.quoteAuthor} aaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey,
fontStyle: FontStyle.italic
),
),
],
),
),
],
),
Row(
children: <Widget>[
RotatedBox(
quarterTurns: -1,
child: Column(
children: <Widget>[
Text(
"Informal",
style: TextStyle(
fontSize: 32,
color: Colors.grey.withOpacity(0.5),
fontFamily: "AbrilFatFace"),
),
],
),
),
Container(
height: 300,
width: 300,
child: FutureBuilder(
future: databaseProvider
.getPhrasesByCategoryId(widget.category.id),
builder: (context, snapshot) {
return snapshot.hasData
? ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, i) {
return _buildPhraseItem(snapshot.data[i]);
})
: Center(
child: CircularProgressIndicator(),
);
},
),
),
],
),
],
),
),
),
);
}
Widget _buildPhraseItem(Phrase phrase) {
Random random = Random();
int colorIndex = random.nextInt(_itemColors.length - 1);
Color currentColor = _itemColors[colorIndex];
if (_colorCounter >= 10) _colorCounter = 0;
return InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PhraseDetail(
phraseToShow: phrase.phrase,
color: currentColor,
)));
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 80,
decoration: BoxDecoration(
color: currentColor,
borderRadius: BorderRadius.all(Radius.circular(4)),
boxShadow: [
BoxShadow(
blurRadius: 8,
color: Colors.grey.withOpacity(0.5),
offset: Offset(0, 3))
]),
child: Center(
child: Text(
phrase.phrase,
style: TextStyle(color: Colors.white),
)),
),
),
);
}
}
wrap the second child(Padding) of the first Row with Flexible
wrap the second child(Container) of the second Row with Flexible and remove width: 300 from the container parameters.
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CategoryDetailPage(),
);
}
}
Random random = Random();
class CategoryDetailPage extends StatefulWidget {
CategoryDetailPage({
Key key,
}) : super(key: key);
#override
_CategoryDetailPageState createState() => _CategoryDetailPageState();
}
class _CategoryDetailPageState extends State<CategoryDetailPage> {
final List<Color> _itemColors = [
Color(0xff16a085),
Color(0xff2980b9),
Color(0xff8e44ad),
Color(0xff2c3e50),
Color(0xffd35400),
Color(0xffbdc3c7),
Color(0xff27ae60),
Color(0xfff39c12),
Color(0xff7f8c8d),
Color(0xffc0392b),
];
int _colorCounter = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Row(
mainAxisSize: MainAxisSize.min,
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image(
image: NetworkImage(
'https://source.unsplash.com/random',
),
width: 32,
),
),
),
Flexible(
child: Padding(
padding: const EdgeInsets.only(left: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'say congratulations',
style:
TextStyle(fontSize: 24, fontFamily: "Pacifico"),
),
Text(
"At every party there are two kinds of people – those who want to go home and those who don’t. The trouble is, they are usually married to each other. - Ann Landers",
textAlign: TextAlign.left,
style: TextStyle(
color: Colors.grey,
fontStyle: FontStyle.italic),
),
],
),
),
),
],
),
Row(
children: <Widget>[
RotatedBox(
quarterTurns: -1,
child: Column(
children: <Widget>[
Text(
"Informal",
style: TextStyle(
fontSize: 32,
color: Colors.grey.withOpacity(0.5),
fontFamily: "AbrilFatFace"),
),
],
),
),
Flexible(
child: Container(
height: 300,
child: ListView(children: [
_buildPhraseItem(),
_buildPhraseItem(),
_buildPhraseItem(),
_buildPhraseItem(),
]),
),
),
],
),
],
),
),
),
);
}
Widget _buildPhraseItem() {
var colorIndex = random.nextInt(_itemColors.length - 1);
var currentColor = _itemColors[colorIndex];
if (_colorCounter >= 10) _colorCounter = 0;
return InkWell(
onTap: () {
print('Navigator.push');
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 80,
decoration: BoxDecoration(
color: currentColor,
borderRadius: BorderRadius.all(Radius.circular(4)),
boxShadow: [
BoxShadow(
blurRadius: 8,
color: Colors.grey.withOpacity(0.5),
offset: Offset(0, 3))
]),
child: Center(
child: Text(
'phrase.phrase',
style: TextStyle(color: Colors.white),
)),
),
),
);
}
}