Is there something like mainaxisaligment for a stack? - flutter

Is there a way to align the blue chart bars to the bottom instead of the top?
Here's the code, the blue bar is the FractionallySizedBox:
#override
Widget build(BuildContext context) {
return Column(
children: [
Container(
height: 20,
child: FittedBox(
child: Text('\$${getShortForm(spendingAmount)}'),
),
),
SizedBox(
height: 4,
),
Container(
height: 64,
width: 10,
child: Stack(
children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1),
color: Color.fromRGBO(220, 220, 220, 1),
borderRadius: BorderRadius.circular(10),
),
),
FractionallySizedBox(
heightFactor: spendingPctOfTotal,
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(10),
),
),
),
],
),
),
SizedBox(
height: 4,
),
Text(label)
],
);
}
}
I tried working with the Stack's alignment attribute but that didn't seem to work. It seems I can only work with the order in which the children are stacked, not position. I tried wrapping it with a column but that broke the entire thing.

Mostly we need to wrap stack children with positioned widget like Aling/Positioned. For your case
child: Stack(
alignment: Alignment.bottomCenter,//default is topStart
children: [
Or
Align(
alignment: Alignment.bottomCenter,
child: FractionallySizedBox(
heightFactor: .3,
child: Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(10),
),
),
),
),

Related

Flutter container inside a container using a widget

I want to add two boxes, one inside another. Here's my code and its not working for two boxes. how should I correct this. doc UI in this doc you can see the UI I implement so far and the UI I want to be implement.
Widget DetailBox() => Padding(
padding: const EdgeInsets.only(left: 25, right: 25),
child:Column(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: Colors.white,
),
// alignment: Alignment.bottomCenter,
height: 400,
width: 300,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: Colors.lightBlue,
),
// alignment: Alignment.bottomCenter,
height: 100,
width: 300,
),
),
],
),
);
May it help you
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
elevation: 5,
shadowColor: Colors.blue.shade200,
child: Column(
children: [
Expanded(
flex: 1,
child: Container(
decoration: BoxDecoration(
color: Colors.lightBlueAccent,
borderRadius: BorderRadius.only(topLeft:Radius.circular(25),topRight:Radius.circular(25))
),
),
),
Expanded(
flex: 3,
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(bottomRight:Radius.circular(25),bottomLeft:Radius.circular(25))
),
),
)
],
),
),
According to the design of the app, put the image in the bule box.

How to add text in a section of a container in flutter?

The result I want to have
My current result
I was trying to add a title section for my categories but the result was not what I expected.
I'm looking to add a title section below each illustration in my categories.
child: Container(
height: 400.0,
width: 500.0,
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: ColorPallete.secondColor[50],
borderRadius: BorderRadius.circular(20.0),
),
child: SvgPicture.asset(
'assets/images/svg/megacategory/art__grocery.svg',
),
)
You can use a Column widget to align its contents vertically, in your example you can use it as follow (I have used some online images and colors you can edit it as per your requirement)-
Column(
children: [
SizedBox(height: 50.0),
Container(
height: 100.0,
width: 100.0,
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: Colors.orange[50],
borderRadius: BorderRadius.circular(20.0),
),
child: Image.network(
'https://img.icons8.com/cotton/online-shop-2--v2.png',
),
),
SizedBox(height: 10.0),
Text("Title Here")
],
),
It will arrange the contents as follows -
You can read more about Column widget here
child: Container(
height: 400.0,
width: 500.0,
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: ColorPallete.secondColor[50],
borderRadius: BorderRadius.circular(20.0),
),
child: Column(
children: [
SvgPicture.asset(
'assets/images/svg/megacategory/art__grocery.svg',
),
Text('Abarrotes')
)
You can wrap it with Column widget so you can add a Text widget under your container.
child: Column(
children: [
Container(
height: 400.0,
width: 500.0,
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: ColorPallete.secondColor[50],
borderRadius: BorderRadius.circular(20.0),
),
child: SvgPicture.asset(
'assets/images/svg/megacategory/art__grocery.svg',
),
),
Text("Your text",style: TextStyle(color: Colors.black) ),
],
),
You try put image in decoration instead of child and add text in child container as below.
Container(
decoration: BoxDecoration(
image: DecorationImage(image: NetworkImage(*imageUrl*)),
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
child: Text(*title*))
As I saw from the answers above you are facing an overflow problem when you are implementing these lines of code:
child: Container(
height: 400.0,
width: 500.0,
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: ColorPallete.secondColor[50],
borderRadius: BorderRadius.circular(20.0),
),
child: Column(
children: [
SvgPicture.asset(
'assets/images/svg/megacategory/art__grocery.svg',
),
Text('Abarrotes')
)
So add these two variables under your Widget build:
Widget build(BuildContext context) {
double width,height;
width = MediaQuery.of(context).size.width;//This means that width== the width of the device running the app in double
height = MediaQuery.of(context).size.height;//Same thing with the height
So now that you have the values of the device width and height you can experiment with their proportions:
child: Container(
height: height*x,//for example if you want it to take place in 1/4 of the screen x=0.25
width: width*x,//same thing here
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: ColorPallete.secondColor[50],
borderRadius: BorderRadius.circular(20.0),
),
child: Column(
children: [
SvgPicture.asset(
'assets/images/svg/megacategory/art__grocery.svg',
),
Text('Abarrotes')
)

How to remove the inner border of a Widget in a Stack in Flutter?

I wan't to get the outline border of two containers in a Stack, one positioned like the image suggest. My goal is to remove the "red" part.
How can I accomplish this in Flutter?
Widget build(BuildContext context) {
return Stack(
overflow: Overflow.visible,
children: [
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
border: Border.all(),
),
child: const Center(child: Text("BLABLABLA")),
),
Positioned(
top: -10,
left: 10,
child: Container(
width: 150,
height: 25,
decoration: BoxDecoration(
border: Border.all(),
),
child: const Center(child: Text("BLABLABL2")),
),
),
],
);
}
If I have understood your question correctly, then this would work for you!
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stack Example'),
),
body: Center(
child: Stack(
overflow: Overflow.visible,
children: [
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.grey.withOpacity(0.2),
border: Border.all(),
),
child: const Center(child: Text("BLABLABLA")),
),
Positioned(
top: -10,
left: 10,
child: Container(
width: 150,
height: 25,
decoration: BoxDecoration(
color: Colors.white,
),
child: Stack(children: [
Container(
width: double.infinity,
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: Colors.black),
left: BorderSide(color: Colors.black),
right: BorderSide(color: Colors.black),
),
),
height: 11),
Center(
child: Text("BLABLABL2"),
),
],
),
),
),
],
),
),
);
}
The output will be like,

How to make the bar chart starts from the bottom to top in flutter?

I would like to make the bar chart starts from bottom to top in flutter because my bar chart is starting from top to down and this is leaving a bad impact for the users. So, how to can I make the bar chart starts from the bottom to top in flutter?
Bellow are two widgets of the bar chart, I just spread them into two different files.
print('build() ChartBar');
return LayoutBuilder(builder: (ctx, constraints) {
return
Column(
children: <Widget>[
Container(
height: constraints.maxHeight * 0.15,
child: FittedBox(
child: Text('\$${spendingAmount.toStringAsFixed(0)}'),
),
),
SizedBox(
height: constraints.maxHeight * 0.05,
),
Container(
height: constraints.maxHeight * 0.6,
width: 10,
child: Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 1.0),
color: Color.fromRGBO(220, 220, 220, 1),
borderRadius: BorderRadius.circular(10),
),
),
FractionallySizedBox(
heightFactor: spendingPercentageOfTotal,
alignment: Alignment.bottomCenter,
child: Container(
decoration: BoxDecoration(
color: Colors.purple,
borderRadius: BorderRadius.circular(10),
),
)),
],
),
),
SizedBox(
height: constraints.maxHeight * 0.05,
),
Container(
height: constraints.maxHeight * 0.15,
child: FittedBox(
child: Text(label),
),
)
],
);
});
}
}
Widget build(BuildContext context) {
print('build() Chart');
return Card(
elevation: 6,
margin: EdgeInsets.all(10),
child: Container(
padding: EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: groupedTransactionValues.map(
(data) {
return Flexible(
fit: FlexFit.tight,
child: ChartBar(
data['day'],
data['amount'],
totaLSpending == 0.0
? 0.0
: (data['amount'] as double) / totaLSpending));
},
).toList(),
),
),
);
}
}
you can use this code below. Flutter default is from top to bottom. You can change this by using Align widget.
Align(
alignment: Alignment.bottomCenter,
child: FractionallySizedBox(....),
)
add this into Stack widget
alignment: AlignmentDirectional.bottomStart,
the easy way is to use dependencies
https://pub.dev/packages/fl_chart

Flutter Row with two container with centered image and text above it. Like in image attached

How to create Row with two containers with centered image and text above image also centered.
Like in image attached.
enter image description here
Is this what you are trying to do:
class ExampleDesign extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: Container(
height: 180,
padding: EdgeInsets.symmetric(
horizontal: 5,
),
child: Row(
children: <Widget>[
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 3,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 60,
width: 120,
padding: EdgeInsets.only(left: 10),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.black,
),
),
child: Align(
alignment: Alignment.centerLeft,
child: Text('Image'),
),
),
Text('Some text centered'),
],
),
),
),
SizedBox(
width: 20,
),
Expanded(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 3,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 60,
width: 120,
padding: EdgeInsets.only(left: 10),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.black,
),
),
child: Align(
alignment: Alignment.centerLeft,
child: Text('Image'),
),
),
Text('Some text centered'),
],
),
),
),
],
),
),
);
}
}
OUTPUT:
I hope this answers your question.
Create a widget using this post How to to display text over the images in Flutter? which returns an image above and a text below, Wrap two of these widgets using a Row. That should work.