Flutter - How add separators fot BottomNavigationBar? - flutter

I use BottomNavigationBar in my flutter app. This is exist view:
but I need to add separators between items. like this:
Is it possible? is there a simple way to implement this?

It's possible using BottomAppBar by having Container as a child to specify custom height of the appbar and then it can have Row to add children. The Row can have 3 FlatButtons, each having an Icon and Text inside a Column. Between each FlatButton, you can add Container to add divider. Below is code snippet:
bottomNavigationBar: BottomAppBar(
child: Container(
height: 60,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlatButton(
padding: EdgeInsets.all(10.0),
onPressed: () {},
child: Column(
children: <Widget>[
Icon(Icons.home),
Text('Home')
],
),
),
Container(color: Colors.black, width: 2,),
FlatButton(
padding: EdgeInsets.all(10.0),
onPressed: () {},
child: Column(
children: <Widget>[
Icon(Icons.business),
Text('Business')
],
),
),
Container(color: Colors.black, width: 2,),
FlatButton(
padding: EdgeInsets.all(10.0),
onPressed: () {},
child: Column(
children: <Widget>[
Icon(Icons.school),
Text('School')
],
),
)
]
),
)
),
And the output :

Related

I have a problem with displaying in a Row in flutter

I'm a beginner in flutter, I want to display a text on the far left and a button on the far right in a container, here is my code:
Container(
width: double.infinity,
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(8.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('TEXT'),
IconButton(
onPressed: () {},
icon: Icon(
Icons.price_change_outlined,
color: Colors.blue,
))
],
),
),
Use mainAxisAlignment: MainAxisAlignment.spaceBetween,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('TEXT'),
IconButton(
onPressed: () {},
icon: Icon(
Icons.price_change_outlined,
color: Colors.blue,
))
],
),
You can find more about Row and layout
An alternative to the above solutions is to use a Spacer widget.
An advantage to this approach is that you can easily add more widgets to whatever side you want and keep the spacing in between.
Row(
children: [
Text('TEXT'),
const Spacer(),
IconButton(
onPressed: () {},
icon: Icon(
Icons.price_change_outlined,
color: Colors.blue,
),
)
]
)

Padding and layout of card in flutter

I am trying to add padding to the text in the card and for some reason, it is not working.
This is my code:
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Container(
height: 170.0,
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: Icon(Icons.phone),
title: Align(
child: Text('Need immediate help'),
alignment: Alignment(-1.2, 1.0),
),
subtitle: Column(
children: <Widget>[
Align(
child: Text('In an emergency, call 999.'),
alignment: Alignment(-1.2, 0),
),
Align(
child: Text(
"If you're in crisis and need to speak to someone call NHS 111."),
alignment: Alignment(-1.2, 0),
),
],
),
),
ButtonBar(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ButtonTheme(
minWidth: 150.0,
child: RaisedButton(
padding: EdgeInsets.all(8.0),
child: const Text('999'),
onPressed: () => launch("tel://000"),
),
),
ButtonTheme(
minWidth: 150.0,
child: RaisedButton(
padding: EdgeInsets.all(8.0),
child: const Text('111'),
onPressed: () => launch("tel://000"),
),
Currently it looks like this:
I am wanting to add padding around the text so it looks better but using alignment isn't affecting the second subtitle and isn't affecting the padding above or below any of the text. Any help would be appreciated!
Change mainAxisAlignment property of your Column as following and remove the mainAxisSize property
Column(
mainAxisAlignment: MainAxisAlignment.center,
// ...
),
Edit
Adjust your subtitle like this
ListTile(
leading: Icon(Icons.phone),
title: Align(
child: Text('Need immediate help'),
alignment: Alignment(-1.2, 1.0),
),
subtitle: Align(
alignment: Alignment(- 65.0, 1.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('In an emergency, call 999.', textAlign: TextAlign.left,),
Text("If you're in crisis and need to speak to someone call NHS 111.", textAlign: TextAlign.left,),
],
),
),
),

Centering a Row in a Stack

I have a widget that consists of a Row and an Icon in a stack. I want to center them both vertically in the stack (the row in the center, the icon in the right center). I wrap both widgets in an Align widget, but this only is affecting the Icon, not the Row. How can I have the ROw centered vertically?
This is my code
#override
Widget build(BuildContext context) {
return Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.center,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(searchLanguage == SearchLanguage.english ? word.english : word.korean,
style: Theme.of(context).textTheme.bodyText1),
SizedBox(
width: 8.0,
),
Icon(Icons.arrow_forward),
SizedBox(
width: 8.0,
),
Text(searchLanguage == SearchLanguage.english ? word.korean : word.english,
style: Theme.of(context).textTheme.bodyText1),
],
),
),
Align(
alignment: Alignment.centerRight,
child: IconButton(
icon: Icon(Icons.delete),
onPressed: () => deleteCallback(word),
),
)
],
),
),
);
}
This is the outcome
Try using the alignment of the stack instead:
Stack(
alignment: Alignment.topCenter,
Alignment properties works, also remove the unnecessary alignments
Full code:
return Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("One", style: Theme.of(context).textTheme.bodyText1),
SizedBox(
width: 8.0,
),
Icon(Icons.arrow_forward),
SizedBox(
width: 8.0,
),
Text("Two", style: Theme.of(context).textTheme.bodyText1),
],
),
Align(
alignment: Alignment.centerRight,
child: IconButton(
icon: Icon(Icons.delete),
onPressed: () {},
),
)
],
),
),
)

Flutter ButtonBar in horizontal mode doesn't seem to respect MainAxisSize

I would like to use flutter's ButtonBar due to the nice feature of automatically having a column when the device's width isn't enough. However, I'd also like to use the MainAxisSize.min, but I can't get it to use MainAxisSize.min in horizontal mode (wrt width). See example below.
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(border: Border.all()),
child: ButtonBar(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('short'),
color: Colors.blueAccent,
onPressed: () => {},
),
FlatButton(
child: Text('short'),
color: Colors.blueAccent,
onPressed: () => {},
),
],
),
),
Container(
decoration: BoxDecoration(border: Border.all()),
child: ButtonBar(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('loooooooooooooooooong'),
color: Colors.blueAccent,
onPressed: () => {},
),
FlatButton(
child: Text('loooooooooooooooooong'),
color: Colors.blueAccent,
onPressed: () => {},
),
],
),
),
],
),
What I get
What I want
A possible solution can be to use the Wrap class instead of the ButtonBar. You have to
wrap the Container with a Center widget add some padding for the layout and your're god to go:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(border: Border.all()),
child: ButtonBar(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FlatButton(
child: Text('short'),
color: Colors.blueAccent,
onPressed: () => {},
),
FlatButton(
child: Text('short'),
color: Colors.blueAccent,
onPressed: () => {},
),
],
),
),
Center(
child: Container(
decoration: BoxDecoration(border: Border.all()),
padding: EdgeInsets.all(10),
child: Wrap(
//alignment: MainAxisAlignment.center,
//mainAxisSize: MainAxisSize.max,
children: <Widget>[
FlatButton(
child: Text('looooooooooooooooooooooooong'),
color: Colors.blueAccent,
onPressed: () => {},
),
FlatButton(
child: Text('looooooooooooooooooooooooong'),
color: Colors.blueAccent,
onPressed: () => {},
),
],
),
),
),
],
),

Duplicate named argument - how to add more then one child to a body in flutter?

import 'package:flutter/material.dart';
Error in line 18:
Error: The argument for the named parameter 'child' was already specified. Try removing one of the named arguments, or correcting one of the names to reference a different named parameter.dart(duplicate_named_argument)
Why body can't have more then one child?
How to solve that?
class mainMenu extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Bar Iland"),
),
body: Center(
child:
Image.asset(
'assets/Bar_Iland_line.png',
height: 200,
color: Colors.black.withOpacity(0.80),
),
child:
Column(
children: <Widget>[
Spacer(flex: 8),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:[
SizedBox.fromSize(
size: Size(90, 90), // button width and height
child: ClipOval(
child: Material(
color: Colors.orange, // button color
child: InkWell(
splashColor: Colors.green, // splash color
onTap: () {}, // button pressed
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.call), // icon
Text("Call"), // text
],
),
),
),
),
),
SizedBox.fromSize(
size: Size(90, 90), // button width and height
child: ClipOval(
child: Material(
color: Colors.orange, // button color
child: InkWell(
splashColor: Colors.green, // splash color
onTap: () {}, // button pressed
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.call), // icon
Text("Call"), // text
],
),
),
),
),
),
SizedBox.fromSize(
size: Size(90, 90), // button width and height
child: ClipOval(
child: Material(
color: Colors.orange, // button color
child: InkWell(
splashColor: Colors.green, // splash color
onTap: () {}, // button pressed
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.call), // icon
Text("Call"), // text
],
),
),
),
),
),
],
),
Spacer(flex: 1),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:[
SizedBox.fromSize(
size: Size(90, 90), // button width and height
child: ClipOval(
child: Material(
color: Colors.orange, // button color
child: InkWell(
splashColor: Colors.green, // splash color
onTap: () {}, // button pressed
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.call), // icon
Text("Call"), // text
],
),
),
),
),
),
SizedBox.fromSize(
size: Size(90, 90), // button width and height
child: ClipOval(
child: Material(
color: Colors.orange, // button color
child: InkWell(
splashColor: Colors.green, // splash color
onTap: () {}, // button pressed
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.call), // icon
Text("Call"), // text
],
),
),
),
),
),
SizedBox.fromSize(
size: Size(90, 90), // button width and height
child: ClipOval(
child: Material(
color: Colors.orange, // button color
child: InkWell(
splashColor: Colors.green, // splash color
onTap: () {}, // button pressed
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.call), // icon
Text("Call"), // text
],
),
),
),
),
),
],
),
Spacer(flex: 10),
],
),
),
);
}
}
As pskink mentioned, you can't have two children in a body.
Use
Scaffold(
body: Column( // or Row or Wrap
children: [
Child1(),
Child2(),
]
)
)