I have a problem with displaying in a Row in flutter - 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,
),
)
]
)

Related

How to transfer Map content to a button?

I have 3 Maps containing images and a key. I would like to transfer their contents to the columns in the buttons as an image.
I have a few questions.
Did I write the images in the Map correctly ? They are stored in my Resources folder in the iconImage file.
How do I get the recorded images in the button (each button has its own value without repetitions, the order is not important)
And how can I click on the buttons then find out if their keys match ? (The button from the first column with the key 1 coincides with the button from the 2nd column with the key 1, and so on).
final Map <int, Image> column1;
column1: const {
1: Image(image: AssetImage(IconImages.test1)),
2: Image(image: AssetImage(IconImages.test2)),
3: Image(image: AssetImage(IconImages.test3)),
}
final Map <int, Image> column2;
column2: const {
1: Image(image: AssetImage(IconImages.test1)),
2: Image(image: AssetImage(IconImages.test2)),
3: Image(image: AssetImage(IconImages.test3)),
}
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:[
Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: column1.values
iconSize: 60,
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: column1.values
iconSize: 60,
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.lightBlueAccent,
child: IconButton(
icon: column1.values
iconSize: 60,
onPressed: () {},),
),
],
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
color: Colors.red,
child: IconButton(
icon: column2.values
iconSize: 60,
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.red,
child: IconButton(
icon: column2.values
iconSize: 60,
onPressed: () {},),
),
SizedBox(height: 40,),
Container(
color: Colors.red,
child: IconButton(
icon: column2.values
iconSize: 60,
onPressed: () {},),
),
],
),
]
);
}

Shaping appbar in flutter

Someone asked this question before with no straight answer:
Is there a way to shape the appbar to look like this photo ?
i dont think you can do it with the AppBar widget, but you can build your own like this:
not my best code but i think this can help you have an idea on how to do it
Widget build(BuildContext context)
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {}, icon: const Icon(Icons.arrow_back)),
Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
IconButton(
onPressed: () {}, icon: const Icon(Icons.search)),
IconButton(
onPressed: () {},
icon: const Icon(Icons.notifications),
),
],
)
],
),
SizedBox(
height: 40,
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30), color: Colors.green),
height: MediaQuery.of(context).size.height,
)
],
),
)),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}`

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: () => {},
),
],
),
),
),
],
),

Flutter - How add separators fot BottomNavigationBar?

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 :

How to achieve dynamic text under icon in the best way

I'm trying to implement 2 Icons in a Row and under each Icon some text
My current implementation:
ButtonBar(
alignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
icon: Icon(
Icons.directions_car,
size: 55.0,
),
onPressed: () {},
),
IconButton(
icon: Icon(
Icons.streetview,
size: 55.0,
),
onPressed: () {},
),
],
),
Padding(
padding: const EdgeInsets.fromLTRB(18.0, 10.0, 0, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text('some text',
style: TextStyle(
fontSize: 16.0
),
),
Text('some text'),
],
),
),
My problem is that my text should be dynamic, So every time the text changed, the location of my text changes and sometimes it look really ugly.
How can in place the text in a better way that is posted here?
try this.. so what I have done is I added both Icon and Text inside a Column so they will behave as a group
ButtonBar(
alignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Column(
children: <Widget>[
IconButton(
padding: EdgeInsets.zero,
icon: Icon(
Icons.directions_car,
size: 55.0,
),
onPressed: () {},
),
SizedBox(
height: 10,
),
Text('some text'),
],
),
Column(
children: <Widget>[
IconButton(
padding: EdgeInsets.zero,
icon: Icon(
Icons.streetview,
size: 55.0,
),
onPressed: () {},
),
SizedBox(
height: 10,
),
Text('some text'),
],
),
],
),