How to set background colour to the width containing text in flutter - text

I have a text which is basically a title in a box and I want to provide a background color to the space behind that text .
Padding(
padding: EdgeInsets.all(15.0),
child: Text(
'Your Profile',
style: TextStyle(
color: Colors.blue,
fontSize: 26.0,
background: Paint()..color = Colors.blue,
),
This is what I tried but it seems not working.
Expected:Here
"Your profile" text has a background color, I need to achieve this.

You can wrap it with a Container() but, most probably you have to define the width with something like double.maxFinite
Check this
Container(
width: double.maxFinite,
color: Colors.red, //define the background color
child: Text("My Text"),
)

Short answer:
Text(
"Hello World",
style: TextStyle(backgroundColor: Colors.blue), // give any color here
)
Full answer
You can use this in your showDialog call, Here is the full widget you were looking for.
Dialog(
elevation: 12,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
color: Colors.blue[800],
width: double.maxFinite,
alignment: Alignment.center,
padding: EdgeInsets.symmetric(vertical: 16),
child: Text(
"Your Profile...",
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 32),
child: Column(
children: <Widget>[
SizedBox(height: 12),
TextField(decoration: InputDecoration(hintText: "Select a Photo")),
TextField(decoration: InputDecoration(hintText: "Take a Photo...")),
TextField(decoration: InputDecoration(hintText: "Choose form Library")),
],
),
),
Container(
padding: EdgeInsets.symmetric(vertical: 12),
child: RaisedButton(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(50)),
child: Text("Back"),
onPressed: () {},
color: Colors.red,
textColor: Colors.white,
),
)
],
),

I agree with Saeb Nabil's answer, the easiest way is to use a container. That way you can also control the padding around the text. You can also use .withOpacity() to add transparency if you wish:
Container(
width: 100,
padding: EdgeInsets.symmetric(horizontal: 0, vertical: 10),
color: Colors.grey[900].withOpacity(0.6),
child: Text(
"Hello world!",
style: TextStyle(
fontSize: 14,
color: Colors.white),
), // Text
), // Container

Related

when using drawer widget in flutter ,taps make me navigate to another page

this error appears when taping drawer widget ,the page which appeares should appears when taping child of drawer and there is an image descripes what is happening ...............................................................
return Drawer(
child: Column(
children: [
Container(
color: Colors.yellow,
alignment: Alignment.topLeft,
padding:const EdgeInsets.all(20),
height: 120,
width: double.infinity,
child: Text(
'cook up ',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Theme.of(context).primaryColor),
),
),
const SizedBox(height:20),
BuildList('Meals', Icons.restaurant,(){
Navigator.of(context).push(MaterialPageRoute(builder: (_){return FilterScreen();}));
}),
BuildList('Filters ', Icons.settings,(){
Navigator.of(context).push(MaterialPageRoute(builder: (_){return TabsScreen();}));
}),
],
),
);
}
}

How to make modify Sliverappbar so I can see the background image with FlexibleSpaceBar when I scroll up?

How to make modify Sliverappbar so I can see the background image with FlexibleSpaceBar when I scroll up?
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text("Collapsing Toolbar",
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
)),
background: Image.network(
"https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350",
fit: BoxFit.cover,
)),
),
I'm afraid this is the standard behavior of the FlexibleSpaceBar. You can change the color but not make it transparent.
A workaround I found is to use a Stack instead of the FlexibleSpaceBar:
SliverAppBar(
expandedHeight: 200.0,
floating: true,
pinned: true,
snap: true,
flexibleSpace: Stack(
children: <Widget>[
Positioned.fill(
child: Image.network(
"https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350",
fit: BoxFit.cover,
),
),
Positioned.fill(
child: Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(
"Collapsing Toolbar",
style: TextStyle(
color: Colors.white,
fontSize: 24.0,
),
textAlign: TextAlign.center,
),
),
),
),
],
),
),

flutter container with different vertical spacings by the same contents

I have different vertical spacing in two containers with the same contents. I have tried it with different widgets, it is always the same.
The 'Card' widget contains a 'Column' with two identical 'Containers', each containing an identical 'ListTile' with an identical 'child: Text'.
What can I do to make the vertical distances equal and as small as possible?
Container(
width: screenWidth * 0.5, height: 300,
padding: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Container(
width: screenWidth * 0.5, height: 280,
padding: EdgeInsets.all(1),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: const BorderRadius.all(Radius.circular(8.0),),
side: BorderSide(
color: Colors.black,
width: 1.0,),
),
color: Colors.white,
elevation: 4.0,
shadowColor: Colors.blue,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children:[
Container(
padding: EdgeInsets.all(0.0),
margin: EdgeInsets.all(0.0),
decoration: BoxDecoration(
color: Colors.yellow,
border: Border.all(color: Colors.black),
),
child: ListTile(
contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 5.0),
dense: true,
isThreeLine: false,
title: Text(
selTitle,
style: TextStyle(
color:Colors.black,
//backgroundColor: Colors.limeAccent,
backgroundColor: selColor(selIndex),
fontSize: 20,
fontWeight: FontWeight.bold,
),
)
)
),
Container(
padding: EdgeInsets.all(0.0),
margin: EdgeInsets.all(0.0),
decoration: BoxDecoration(
color: Colors.yellow,
border: Border.all(color: Colors.black),
),
child: ListTile(
contentPadding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 5.0),
dense: true,
isThreeLine: false,
title: Text(
"noch ein sehr langer Text: jihiuwh uewua8h ieshg lisru hirluh spi",
style: TextStyle(
backgroundColor: myBTN_HELLGRAU,
fontSize: 16,
)
),
)
)
]
)
),
)
]
)
),
see details and
here is the whole application
The vertical distances in 'Andorra' are much larger than in the text below.
How do I get the spacing in the upper field smaller?
I have found a solution. Insert into the first ListTile solved my problem:
visualDensity: VisualDensity(horizontal: 0, vertical: -4),

Flutter: Text clipping not working when in a Row

I have a situation where there are two text items in a row. The first text can have a variable length, but I don't want to wrap the texts in two lines. So I need the first text to be clipped when the parent does not have enough width for it and the second text. Now, if I do not place the texts in a row, it works as expected. But if they are in a Row, the clipping does not occur. I can not wrap the first text in an Expanded widget (which fixes the clipping problem) because it adds a blank space between the two texts.
Here is a code snippet
Container(
child: Row (
children: <Widget>[
Text("Long text that needs to be clipped",
overflow: TextOverflow.clip,
textAlign: TextAlign.left),
//there should be no blank space between the elements
Text( "+1",
textAlign: TextAlign.left,
overflow: TextOverflow.fade),
],
)
)
Been scratching my head for a while...
We can use Expanded as per below:
Material(
child: Container(
color: appColor,
child: Row(
children: <Widget>[
Expanded(
child: Align(
alignment: AlignmentDirectional.centerStart,
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
softWrap: false,
overflow: TextOverflow.fade,
style: TextStyle(color: Colors.white),
),
),
),
),
IconButton(
icon: Image.asset('assets/images/filter.png'),
onPressed: () {},
),
IconButton(
icon: Image.asset('assets/images/notification.png'),
onPressed: () {},
),
],
)),
);

How to use MediaQuery to set scaleFactor for text in Flutter?

With MediaQuery I can get height and width of my Samsung S7 Edge screen size so I can work with it. But how to use MediaQuery to layout the multiple column dynamic text in ListTile? In my demo project I set the text size to 12 and in Samsung S6 I get overflow issues... Any info or example for text scale factor in Flutter?
I found one solution here (How can Flutter handle dpi text and image size differences) and I try to build demo project. S6 textScaleFactor is 1.1 an the S7 is 1.0....
My original demo app that I test it with S7 text size was 12. And when I try to test it in S6 I get overflow issue... I need to scale down or up with MediaQuery to set text scale factor, so it can work most of the devices without getting a overflow issues?
In ListTile I have a column that has 4 separate lines of text and all the value comes from database. If the text value is long I get a overflow issues on S6 device. So my question is How to use MediaQuery to set scaleFactor for text in Flutter?
Update:
new ListTile(
trailing: new Icon(Icons.chevron_right),
onTap: () {
},
title: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
‘My Account Details:’,
style: new TextStyle(
fontSize: 14.0, fontWeight: FontWeight.bold, color: Colors.black),
overflow: TextOverflow.fade,
),
],
),
subtitle: new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(‘Hello Flutter’,
style: new TextStyle(
fontSize: 12.0, color: Colors.black54),
overflow: TextOverflow.fade,
),
new Text(’Today is **************’,
style: new TextStyle(
fontSize: 12.0, color: Colors.black54),
overflow: TextOverflow.fade,
),
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(‘Name’,
style: new TextStyle(
fontSize: 12.0, color: Colors.black54),
overflow: TextOverflow.fade,
),
new Text(‘Dart’,
style: new TextStyle(
fontSize: 12.0, color: Colors.black54),
overflow: TextOverflow.fade,
),
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(’Surname’,
style: new TextStyle(
fontSize: 12.0, color: Colors.black54),
overflow: TextOverflow.fade,
),
new Text(‘Flutter’,
style: new TextStyle(
fontSize: 12.0, fontWeight: FontWeight.bold, color: Colors.black),
overflow: TextOverflow.fade,
),
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(‘Account Name: Let’s Flutter all day long till down with fancy User Interface’,
style: new TextStyle(
fontSize: 12.0, color: Colors.black54),
overflow: TextOverflow.fade,
),
new Text(‘109.65’,
style: new TextStyle(
fontSize: 12.0, fontWeight: FontWeight.bold, color: Colors.black),
overflow: TextOverflow.fade,
),
],
),
],
),
),
You can solve your issue wrapping your Text widget into a Flexible to avoid the overflow. I put maxLines 1 to see the Fade overflow :
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: new Text(
"Account Name: Let's Flutter all day long till down with fancy User Interface",
maxLines: 1,
style: new TextStyle(
fontSize: myTextSize, color: Colors.black54),
overflow: TextOverflow.fade,
),
),
new Text(
"109.65",
style: new TextStyle(
fontSize: myTextSize,
fontWeight: FontWeight.bold,
color: Colors.black),
overflow: TextOverflow.fade,
),
],
),
I added a global variable :
var myTextSize = 12.0;
Also you can use the LayoutBuilder in order to get the maxHeight or maxWidth of your device, after that you can change the text size like this example:
var myTextSize = 12.0;
return LayoutBuilder(builder: (context, boxConstraints) {
print(boxConstraints.maxHeight);
if (boxConstraints.maxHeight <= 667.0){
myTextSize = 10.0;
}
return Container(
child: new ListTile(
trailing: new Icon(Icons.chevron_right),
onTap: () {},
title: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
"My Account Details:",
style: new TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold,
color: Colors.black),
overflow: TextOverflow.fade,
),
],
),
subtitle: new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
"Hello Flutter",
style: new TextStyle(
fontSize: myTextSize, color: Colors.black54),
overflow: TextOverflow.fade,
),
new Text(
"Today is **************",
style: new TextStyle(
fontSize: myTextSize, color: Colors.black54),
overflow: TextOverflow.fade,
),
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
"Name",
style: new TextStyle(
fontSize: myTextSize, color: Colors.black54),
overflow: TextOverflow.fade,
),
new Text(
"Dart",
style: new TextStyle(
fontSize: myTextSize, color: Colors.black54),
overflow: TextOverflow.fade,
),
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Text(
"Surname",
style: new TextStyle(
fontSize: myTextSize, color: Colors.black54),
overflow: TextOverflow.fade,
),
new Text(
"Flutter",
style: new TextStyle(
fontSize: myTextSize,
fontWeight: FontWeight.bold,
color: Colors.black),
overflow: TextOverflow.fade,
),
],
),
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: new Text(
"Account Name: Let's Flutter all day long till down with fancy User Interface",
maxLines: 1,
style: new TextStyle(
fontSize: myTextSize, color: Colors.black54),
overflow: TextOverflow.fade,
),
),
new Text(
"109.65",
style: new TextStyle(
fontSize: myTextSize,
fontWeight: FontWeight.bold,
color: Colors.black),
overflow: TextOverflow.fade,
),
],
),
],
),
),
);
});
#override
Widget build(BuildContext context) {
return WidgetsApp(
// ...
builder: (context, child) {
final mediaQueryData = MediaQuery.of(context);
return MediaQuery(
data: mediaQueryData.copyWith(textScaleFactor: 1.5),
child: child,
);
},
);
}
If you want to make the Text font size to be Responsive
this is a simple trick
fontSize: MediaQuery.of(context).size.width * 0.05 //u can play with equation
or
fontSize: MediaQuery.of(context).size.width > 500 ?60
: MediaQuery.of(context).size.width < 300 ?40: 30,

Resources