I am trying to show two horizontal lines either side of text: Or . As shown in the image below:
I have tried to do this using a Row with two dividers and a text in it. However it is only showing the Text("Or"), on the screen.
Here is the code I am trying:
Widget showDivider() {
return Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Divider(
indent: 10,
endIndent: 10,
thickness: 5.0,
height: 5.0,
color: Colors.black87,
),
Text('or'),
Divider(
indent: 10,
endIndent: 10,
thickness: 5.0,
height: 5.0,
color: Colors.black87,
),
],
),
);
}
No matter which settings I try on the Dividers they are not showing.
Any help in getting this sorted would be appreciated. Many thanks.
Wrap your Both Dividers with Expanded.
Wrap both the dividers with expanded widgets
Widget showDivider() {
return Padding(
padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Divider(
indent: 10,
endIndent: 10,
thickness: 5.0,
height: 5.0,
color: Colors.black87,
),
)
Text('or'),
Expanded(
child: Divider(
indent: 10,
endIndent: 10,
thickness: 5.0,
height: 5.0,
color: Colors.black87,
),
).
],
),
);
Use Expanded Widgets
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
flex: 2,
child: Divider(
indent: 10,
endIndent: 10,
thickness: 5.0,
height: 5.0,
color: Colors.black87,
),
),
Expanded(
flex: 1,
child: Text(
'or',
textAlign: TextAlign.center,
),
),
Expanded(
flex: 2,
child: Divider(
indent: 10,
endIndent: 10,
thickness: 5.0,
height: 5.0,
color: Colors.black87,
),
),
],
),
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Container(
margin: const EdgeInsets.only(left: 10.0, right: 10.0),
height: .3,
width: double.infinity,
color: Colors.black),
),
Text('or'),
Expanded(
child: Container(
margin: const EdgeInsets.only(left: 10.0, right: 10.0),
height: .3,
width: double.infinity,
color: Colors.black),
),
],
),
),
);
}
Related
I have a somewhat complicated widget tree and can't figure this out. I've tried wrapping the Scaffold body in a SingleChildScrollView but for some reason it just makes the Container shrink and does not scroll. Here is the build function code:
return Stack(
children: [
Scaffold(
resizeToAvoidBottomInset: false,
body: Stack(
children: [
background(),
Padding(
padding: const EdgeInsets.only(top: 55),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/logo.png', height: 100),
const SizedBox(width: 5),
const Text('GLOBE',
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
color: Color(0xFF7FCCDC)))
],
),
),
Padding(
padding: const EdgeInsets.only(
top: 175, left: 35, right: 35, bottom: 50),
child: Container(
decoration: BoxDecoration(
color: const Color(0xFFFCFBF4).withOpacity(0.5),
borderRadius: BorderRadius.circular(20),
),
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text('Welcome!',
style: TextStyle(
fontSize: 30, color: Color(0xFF6B6FAB))),
],
),
loginForm()
],
),
),
),
],
),
),
if (_isLoading)
const Opacity(
opacity: 0.8,
child: ModalBarrier(dismissible: false, color: Colors.black),
),
if (_isLoading)
const Center(
child: CircularProgressIndicator(color: Color(0xFFb1bbd8)),
),
],
);
Return a scaffold and add a sized box of height and width same as device. As a body use stack. Then in children add the next stack.
return Scaffold(
resizeToAvoidBottomInset: false,
body: SizedBox(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Stack(
children: [
background(),
SizedBox(
height:MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
child: Column(
mainAxisSize : MainAxisSize.min,
children:[
Padding(
padding: const EdgeInsets.only(top: 55),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/logo.png', height: 100),
const SizedBox(width: 5),
const Text('GLOBE',
style: TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
color: Color(0xFF7FCCDC)))
],
),
),
Padding(
padding: const EdgeInsets.only(
top: 100, left: 35, right: 35, bottom: 50),
child: Container(
decoration: BoxDecoration(
color: const Color(0xFFFCFBF4).withOpacity(0.5),
borderRadius: BorderRadius.circular(20),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Text('Welcome!',
style: TextStyle(
fontSize: 30, color: Color(0xFF6B6FAB))),
],
),
loginForm()
],
),
),
),
]
)
)
if (_isLoading)
const Opacity(
opacity: 0.8,
child: ModalBarrier(dismissible: false, color: Colors.black),
),
if (_isLoading)
const Center(
child: CircularProgressIndicator(color: Color(0xFFb1bbd8)),
),
]
)
)
)
TextField has a property called scrollPadding.
scrollPadding: EdgeInsets.only(bottom: 40)
By default it is set to EdgeInsets.all(20.0)
i've a card like this.
Actual card
but i want a card like this
What i want
My code is:
return Card(
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
margin: EdgeInsets.fromLTRB(20.0, 5.0, 20.0, 5.0),
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
child: Container(
padding: EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Example text'),
Text("20-06-2021 - 13:00",
style:
TextStyle(fontSize: 15, fontStyle: FontStyle.italic)),
Text("Example text"),
Text("Example text"),
],
),
Column(
children: <Widget>[
Flag(
'FR',
height: 40,
width: 40,
replacement: Text('Error'),
),
],
),
],
),
),
);
Now, i have fixed height and width, i'm not able to obtain what i want because each attempt enlarge the card in height.(I want to keep the same height as the card above).The icons comes from the package flags (Flags package)
Instead of wrapping a Container in a Card. Try to Wrap a Row with Expanded in the Card. (I just changed the Container into padding)
Card(
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
margin: EdgeInsets.fromLTRB(20.0, 5.0, 20.0, 5.0),
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
child: Padding(
padding: EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
flex: 1,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Example text'),
Text("20-06-2021 - 13:00",
style: TextStyle(
fontSize: 15, fontStyle: FontStyle.italic)),
Text("Example text"),
Text("Example text"),
],
),
),
Expanded(
flex: 1,
child: Container(
height: 100,
color: Colors.red,
),
)
],
),
),
),
try this
to let it fit the width and the height:
Flag('AD', height: double.infinity, width: double.infinity, fit: BoxFit.fill)
or maybe this:
Flag('AD', height: null, width: null, fit: BoxFit.fill)
I recommend to make this flag Expandable widget inside the row
wrapped your Flag inside SizedBox if you know what fixed size it is like this.
SizedBox(
height: 65,
width: 140,
child: Flag(
'FR',
replacement: Text('Error'),
fit: BoxFit.cover,
),
)
More about SizedBox Here
I'm having trouble aligning my text and my icons.
Here's what my output looks like:
I wanted the text to be in the middle left side of the icons.
I've tried adding margins and padding. But the icons just wont align with the text, I even tried placing the icons in a different widget.
Here's my code:
Widget buildFollowText() {
return Padding(
padding: EdgeInsets.only(
left: 5,
top: 5,
),
child: Container(
child: Column(children: [
Padding(
padding: EdgeInsets.only(
left: 0,
right: 250,
),
child: Text(
'or follow us on:',
style: TextStyle(
fontFamily: "DMSans",
fontSize: 15,
fontWeight: FontWeight.w600,
letterSpacing: -0.3,
color: Colors.grey,
),
),
),
Container(
width: 50,
height: 30.0,
margin: EdgeInsets.only(
right: 70,
bottom: 10,
),
child: buildIcons(),
)
])));
}
Widget buildIcons() {
return Padding(
padding: EdgeInsets.only(
top: 0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: new Padding(
padding: const EdgeInsets.only(bottom: 50.0),
child: Icon(
MdiIcons.facebook,
color: Colors.blue,
size: 50,
),
),
),
Expanded(
child: new Padding(
padding: const EdgeInsets.only(bottom: 50.0, left: 40),
child: Icon(
MdiIcons.twitter,
color: Colors.blue,
size: 50,
),
),
)
]));
}
remove the padding.
set mainAxisAlignment center for the column.
Wrap the Text('or follow us on') with Row() and set mainAxisAlignment.center.
Follow this code.
Padding(
padding: EdgeInsets.only(
left: 5,
top: 5,
),
child: Container(
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'or follow us on:',
style: TextStyle(
fontFamily: "DMSans",
fontSize: 15,
fontWeight: FontWeight.w600,
letterSpacing: -0.3,
color: Colors.grey,
),
),
],
),
Container(
width: 50,
height: 30.0,
margin: EdgeInsets.only(
right: 70,
bottom: 10,
),
child: buildIcons(),
)
],
),
),
),
Scaffold(
body: Container(
child: Row(
children: [
// text container
Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text('or follow us on:'),
],
),
),
// icon container
Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
children: [
Icon(
Icons.face,
color: Colors.blue,
size: 30,
),
Icon(
Icons.transfer_within_a_station_rounded,
color: Colors.blue,
size: 30,
)
],
),
],
),
)
],
),
),
)
check below code
Container(
child: Column(
children: [
// text container
Container(
alignment: Alignment.centerLeft,
child: Text('or follow us on:'),
),
// icon container
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.face,
color: Colors.blue,
size: 30,
),
Icon(
Icons.transfer_within_a_station_rounded,
color: Colors.blue,
size: 30,
)
],
),
)
],
),
);
I have a container that represents a card with dynamical content. So it can be about 175 pixels height, can be 300. If this card is disabled I want to overlay this card by another container with, let say, gray color with opacity 0.5. I've realized that I can use Stack for this, but how can I define the correct size for overlay container?
Widget build(BuildContext context) {
return Center(
child: Stack(children: [
_buildCard(context),
Container(
margin: EdgeInsets.only(top: 8, left: 16, right: 16, bottom: 8),
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
color: Colors.blue.withOpacity(0.75),
borderRadius: BorderRadius.circular(20.0)),
child: Center(
child: Text(
AppTranslations.of(context).text("pending"),
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold),
)))
]),
);
}
Widget _buildCard(BuildContext context) {
return Container(
margin: EdgeInsets.only(top: 8, left: 16, right: 16, bottom: 8),
child: Container(
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(new Radius.circular(20.0)),
boxShadow: [
BoxShadow(
color: Colors.grey,
blurRadius: 10.0,
),
],
),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
child: Material(
clipBehavior: Clip.antiAlias,
color: Colors.white,
child: Column(
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_buildImageLogo(),
Expanded(
child: Container(
padding: EdgeInsets.only(top: 12, left: 6, right: 6),
// margin: EdgeInsetsDirectional.fromSTEB(0, 8, 0, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
LimitedBox(
maxWidth: 135,
child: Text(
widget.serviceInfo.title == null
? ""
: widget.serviceInfo.title,
style: TextStyle(fontSize: 16),
textAlign: TextAlign.start,
),
),
LimitedBox(
maxWidth: 50,
child: Container(
margin: EdgeInsetsDirectional.fromSTEB(
0, 0, 8, 0),
child: Text(
widget.serviceInfo.price == null
? ""
: ("AED" +
widget.serviceInfo.price
.toString()),
style: Theme.of(context)
.textTheme
.title
.copyWith(
fontSize: 13,
color: Colors.black54),
textAlign: TextAlign.end,
),
),
),
],
),
SizedBox(
height: Size.fromHeight(15).height,
),
Text(
widget.serviceInfo.description == null
? ""
: widget.serviceInfo.description,
style: Theme.of(context)
.textTheme
.caption
.copyWith(fontSize: 13),
),
serviceOptions(),
],
),
)),
],
),
SizedBox(
height: Size.fromHeight(10).height,
),
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.only(
top: 8, bottom: 8, left: 8, right: 8),
child: _btnDecline(),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.only(
top: 8, bottom: 8, left: 8, right: 8),
child: _btnAccept(),
),
),
],
),
],
),
),
),
));
}
the full hierarchy looks like
Scaffold -> Consumer -> Center -> Column -> Expanded -> Consumer -> ListView -> ServiceCard
You GlobalKey to get the BuildContext and use it to find out your container's size using it's RenderBox. Then you can give this size to the overlaying container. However, this is overkill imo.
You can also define a boolean to see whether or not your container should be overlayed and adjust it's decoration conditionally.
I'm currently learning how to build apps using the Flutter SDK and Android Studio. My problem is that I need to add a Divider widget between the 'Administrative' text and the rest of the Card but as you can see in the screenshot below, the divider isn't showing up. I've tried changing the size (In which case the space between the two texts just increases) and I've tried setting the color to see if it was defaulting to transparent on my phone. Nothing works!
Here's my code for the Card Widget State:
class BBSCardState extends State<BBSCard>{
#override
Widget build(BuildContext context) {
return new Padding(
padding: const EdgeInsets.only(top: 16.0, bottom: 16.0, left: 12.0, right: 12.0),
child: new Card(
child: new Row(
children: <Widget>[
new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Padding(
padding: const EdgeInsets.only(top: 22.0, bottom: 8.0),
child: new Text("Administrative", style: new TextStyle(color: new Color.fromARGB(255, 117, 117, 117), fontSize: 32.0, fontWeight: FontWeight.bold)),
),
new Divider(),
new Text("text")
],
),
],
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
)
)
);
}
}
And here's the screenshot of what the card looks like:
Also:
Is there any way to increase the size of the actual line from the Divider? (Not just the spacing)
Thanks!
You could remove Row, then Column would take all available space and Divider would have width.
#override
Widget build(BuildContext context) {
return new Padding(
padding: const EdgeInsets.only(
top: 16.0, bottom: 16.0, left: 12.0, right: 12.0),
child: new Card(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Padding(
padding: const EdgeInsets.only(top: 22.0, bottom: 8.0),
child: new Text("Administrative",
style: new TextStyle(
color: new Color.fromARGB(255, 117, 117, 117),
fontSize: 32.0,
fontWeight: FontWeight.bold)),
),
new Divider(
color: Colors.red,
),
new Text("text")
],
),
),
);
}
To make custom divider you could check implementation of Divider and adjust it. E.g. replace Divider with
new SizedBox(
height: 10.0,
child: new Center(
child: new Container(
margin: new EdgeInsetsDirectional.only(start: 1.0, end: 1.0),
height: 5.0,
color: Colors.red,
),
),
)
it was happening to me but I found out that this property solves it: thickness
child: Divider(
color: Colors.teal.shade100,
thickness: 1.0,
),
Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.lightGreen,width: 3.0),
),
),
)
Instead of using divider you can use a customized container...
I had the same issue, but by putting my Divider inside an Expanded Widget fixed my problem.
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Divider(
thickness: 1,
color: Color(0xff818181),
),
),
SizedBox(width: 10),
Text(
'Login using Social Media',
style: TextStyle(color: Color(0xff818181), fontWeight: FontWeight.w500),
),
SizedBox(width: 10),
Expanded(
child: Divider(
thickness: 1,
color: Color(0xff818181),
),
),
],
),
In Column, use Divider() and in Row, use VerticalDivider()
Container(
width: 200,
child: Divider(
thickness: 10,
color: Colors.red,
),
),
or
Expanded(
child: Divider(
thickness: 10,
color: Colors.red,
),
),
If you want to draw line for the Widget Views, Try using the BoxDecoration as like in below example
child: new Container(
decoration: new BoxDecoration(
border: Border(
top: BorderSide(width: 1.0, color: Colors.grey),
left: BorderSide(width: 1.0, color: Colors.grey),
right: BorderSide(width: 1.0, color: Colors.grey),
bottom: BorderSide(width: 1.0, color: Colors.grey),),
);
child: new Row(
....
),
)
Horizontal divider
Container(
width: double.infinity,
height: 2, // Thickness
color: Colors.grey,
)
Vertical divider
Container(
width: 2, // Thickness
height: double.infinity,
color: Colors.grey,
)
Recently I have to archive divider which have circular corner. but if I'm using container with border side it's not give accurate output. So you can use this code if you want circular corner.
ClipRRect(
borderRadius: BorderRadius.circular(6.33.r),
child: Container(
width: 100.w,
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Color.fromRGBO(196, 196, 196, 0.6),
width: 5.0.w),
),
),
),
),
I tried with expanded widget but not worked. But only this way it works for me
SizedBox(
height: 50,
child: VerticalDivider(
thickness: 3,
width: 4,
indent: 0,
color: ColorConstants.silverColor,
),
),
This way also worked for me, when I wrapped parent (Row) with IntrinsicHeight
IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: const [
Padding(
padding: EdgeInsets.symmetric(horizontal: 15.0),
child: VerticalDivider(
thickness: 3,
width: 4,
indent: 0,
color: ColorConstants.silverColor,
),
),
HighTrendWidget(
trendValue: 'HIGH',
trendDifference: '1743.88',
)
],
),
)
If you want to use it inside row like you have multiple widgets to show inside row and also a divider then you can use Sizedbox with height and width . pasting code
SizedBox(
width: 60,
height: 30,
child: Divider(
height: 20,
color: defaultColor,
thickness: 5,
),
),
Add the thickness:
Divider(
thickness: 1,
color: Colors.teal.shade100,
)
Just adding the thickness parameter to 1 or above will solve the issue.
Working sample:
const Divider(
height: 1,
thickness: 1),
Padding(
padding: const EdgeInsets.only(right:20),
child:Divider(
color: Color(0xfff8a9c5),
thickness: 2,
),
),
Just Use this function for wherever you want
Divider verticalDivider() {
return Divider(
height: 2,
color: Colors.greenAccent,
);
}