How to remove space between two containers in Flutter? - flutter

I have two Containers of height 250 inside Column widget. There is no any other widget present between this two Container widget but still I can see very little space between two containers.
Here's my code...
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Example(),
);
}
}
class Example extends StatelessWidget {
#override
Widget build(BuildContext context) {
double height = 250;
TextStyle containerTextStyle = TextStyle(color: Colors.white, fontSize: 24);
return Scaffold(
body: Column(
children: <Widget>[
Container(
height: height,
color: Colors.black,
child: Center(
child: Text('Container 1', style: containerTextStyle),
),
),
Container(
height: height,
color: Colors.black,
child: Center(
child: Text('Container 2', style: containerTextStyle),
),
),
],
),
);
}
}
I don't know why but if I set the height of this containers to 100 or 400 it is not showing any space between container. Did not try with many different values for height but space is visible for some value and not visible for other value.
Here's the screenshot from my phone...
Both Containers height is equal to 250
Both Containers height is equal to 400

Replace your scaffold with this:
return Scaffold(
body: Column(
children: <Widget>[
Container(
height: height,
decoration: BoxDecoration(
border: Border.all(width: 0, color: Colors.black),
color: Colors.black,
),
child: Center(
child: Text('Container 1', style: containerTextStyle),
),
),
Container(
height: height,
decoration: BoxDecoration(
border: Border.all(width: 0, color: Colors.black),
color: Colors.black,
),
child: Center(
child: Text('Container 2', style: containerTextStyle),
),
),
],
),
);

As #sukhi said,
You need to use the BoxDecoration in your Container.
But Rather than giving border Color you can simple set width of border to 0.
Like Below:
Container(
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(width: 0),
),
height: height,
child: Center(
child: Text('Container 1', style: containerTextStyle),
),
),
Container(
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(width: 0),
),
height: height,
child: Center(
child: Text('Container 2', style: containerTextStyle),
),
),
And Do not forget to give your color inside BoxDecoration instead of Container itself else it will throw an error.
Cannot provide both a color and a decoration
The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".

A bit late, but currently seems to be an open Flutter issue. My solution was similar to other answers, but as the color in my case had opacity, I had to wrap the problematic container with another container, and add the decoration to the parent container:
decoration: BoxDecoration(
color: Colors.black, //the color of the main container
border: Border.all(
//apply border to only that side where the line is appearing i.e. top | bottom | right | left.
width: 2.0, //depends on the width of the unintended line
color: Colors.black,
),
),
As suggested from https://github.com/flutter/flutter/issues/14288#issuecomment-668795963

Related

dynamic progress bar in flutter

I want to create a progress bar in flutter which is like this
I get the data of week from backend but I have to create it this way.
and the icon on weeks or survey should be touchable.
thanks in advance.
Still a lot of things to change, but i thing it should be enough for the beginning
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
#override
Widget build(BuildContext context) {
List<String> points = [
'Survey',
'Week 1',
'Week 2',
'Week 3',
'Week 4',
];
var lineWidth =
MediaQuery.of(context).size.width - 16.0; // screen width - 2 * padding
var space = lineWidth / points.length; //space between dots
var currentSteps = 3; // <---- change this one
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: SizedBox(
height: 60.0,
child: Stack(
children: [
//grey line
Positioned(
top: 15,
left: 0,
right: 0,
child: Container(
height: 2.0,
width: double.infinity,
color: Colors.grey,
),
),
//red line
Positioned(
top: 15,
left: 0,
child: Container(
height: 2.0,
width: space * (currentSteps - 1) + space / 2,
color: Colors.orange,
),
),
//circles
Row(
children: points
.asMap()
.map((i, point) => MapEntry(
i,
SizedBox(
width: space,
child: Column(
children: [
Stack(
children: [
Container(
height: 30.0,
width: 30.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 1.5,
color: i == currentSteps - 1
? Colors.orange
: Colors.transparent,
),
),
child: Center(
child: Container(
height: 20.0,
width: 20.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: i < currentSteps
? Colors.orange
: Colors.grey,
),
),
),
),
if (i < currentSteps - 1)
const SizedBox(
height: 30.0,
width: 30.0,
child: Center(
child: Icon(
Icons.check,
size: 16.0,
color: Colors.white,
),
),
),
],
),
const SizedBox(height: 4.0),
Text(
point,
textAlign: TextAlign.left,
style: TextStyle(
color: i < currentSteps
? Colors.orange
: Colors.grey,
),
),
],
),
),
))
.values
.toList(),
),
],
),
),
),
),
);
}
}
You can try basic Stepper widget with type: StepperType.horizontal. As for the UI you like im_stepper package. If it fails to satisfy you can create custom widget with Stack and Row.
Try basic Stepper widget of flutter. if its not working properly then you need to create custom widget.

How do I make a text element take up half the height of the parent container in flutter?

I have the following code:
containers.add(Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.blue,
),
margin: const EdgeInsets.all(10),
width: MediaQuery.of(context).size.width/2-20,
height: MediaQuery.of(context).size.width/2-20,
child: Column(
children: [
Text(
value2,
style: TextStyle(
inherit: false,
color: Colors.white,
fontSize: MediaQuery.of(context).size.width/2,
fontWeight: FontWeight.bold,
)
),
]
)
));
});
}
});
The container itself goes into a list which is passed into a GridView eventually becoming a 2-wide grid down the screen. I want to make it so that the text height is half of the height of the container defined by the width and height MediaQuery.of(context).size.width/2-20. Is this possible?
Thanks
You can use Expanded together with FittedBox:
class HalfHeightText extends StatelessWidget {
final String label;
const HalfHeightText({required this.label});
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.blue,
),
margin: const EdgeInsets.all(10),
width: MediaQuery.of(context).size.width / 2 - 20,
height: MediaQuery.of(context).size.width / 2 - 20,
child: Column(
children: [
// ===========================================
Expanded(
child: FittedBox(
fit: BoxFit.contain,
child: Text(
label,
style: const TextStyle(
inherit: false,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
const Expanded(child: SizedBox.shrink()),
// ===========================================
],
),
);
}
}
Result:
HalfHeightText(label: "Text")
use
width: MediaQuery.of(context).size.width0.50,
its mean like user of width 50% or else MediaQuery.of(context).size.width0.25, mean 25%

Flutter: How to make widget always above shadow

I have an app with two containers next to each other/above each other with Row()/Column() widgets accordingly. Both containers have drop shadows in their box decoration parameter. If they are close to each other the one on the top/left casts a shadow on the other one.
Is there a way I can make a widget not have shadows drawn on it?
Edit: here is an example of what I mean.
I don't want the first container to have a shadow from the second container but rather have both containers above the shadows.
Here is the code for this sample: you can copy paste it into dartpad.dev for quick testing.
import 'package:flutter/material.dart';
final Color darkBlue = Colors.grey[200]!;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Row(
children: [
SizedBox(width: 20),
Container(
height: 100,
width: 300,
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [BoxShadow(
blurRadius: 20,
color: Colors.black.withOpacity(0.6)
)]
),
),
Container(
height: 100,
width: 300,
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [BoxShadow(
blurRadius: 20,
color: Colors.black
)]
),
),
]
),
),
),
);
}
}
if they both have their own shadows, I guess there is no way to accomplish what you want.
an alternative way could be, giving the shadow to the parent, here is the example code:
import 'package:flutter/material.dart';
final Color darkBlue = Colors.grey[200]!;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Container(
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(blurRadius: 20, color: Colors.black),
],
),
child: Row(
children: [
Expanded(
child: Container(
height: 100,
width: 300,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(width: .2),
),
),
),
Expanded(
child: Container(
height: 100,
width: 300,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(width: .2),
),
),
),
],
),
),
),
),
);
}
}
I wrapped each of them with Expanded, to make sure to fill the parent.

Vertical centered text and right aligned to an image

I am working on a SplashScreen for my new Flutter app.
Here you have a screenshot of it:
And here you have the code for the current Scaffold:
return Scaffold(
body: Stack(
alignment: Alignment.bottomCenter,
children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/splash/${yourList[randomIndex]}"),
fit: BoxFit.cover,
),
),
// child: [Your content here],
),
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.1,
decoration: new BoxDecoration(
color: const Color(0xFFFFFFFF).withOpacity(0.5),
image: DecorationImage(
image: new AssetImage('assets/images/app_icon_trans.png'),
),
),
child: Text(
"© 2020 Mov-Map version: ${_projectVersion}",
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
fontSize: 16.0),
))
],
),
);
I would like to change the layout for the second container. I would like to put the image on the left side and the text just after the image, vertically at the center of the image.
I have been searching for a solution for a couple of hours and I would appreciate your proposals.
Is this how you would like it? Use a row (I don't have your asset image, so I used a blue container instead)
Copy/paste this code into www.dartpad.dev/flutter to play with it:
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height * 0.1,
decoration:
new BoxDecoration(color: const Color(0xFFFFFFFF).withOpacity(0.5)),
child: Row(children: [
Container(
width: 40,
height: 40,
color: Colors.blue,
),
Text(
"© 2020 Mov-Map version: 1",
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.bold, fontSize: 16.0),
),
]));
}
}
You can also change the Row's alignment using crossAxisAlignment and/or mainAxisAlignment if needed.

Flutter: Borders Disappear When Their Colour Changes

Problem:
I have encountered an interesting problem that I cannot solve. The borders of the container disappear when I change their colour.
Code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Color(0xff31353B),
body: Home(),
),
);
}
}
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 50.0,
child: GestureDetector(
onTap: () {},
child: Container(
decoration: BoxDecoration(
border: Border(
left: BorderSide(
//TODO: HERE IS THE PROBLEM.
color: Color(0xFFF05A22),
//style: BorderStyle.solid,
width: 3.0,
),
top: BorderSide(
//TODO: HERE IS THE PROBLEM.
color: Color(0xFFF05A22),
style: BorderStyle.solid,
width: 3.0,
),
right: BorderSide(
//TODO: HERE IS THE PROBLEM.
color: Color(0xFFF05A22),
style: BorderStyle.solid,
width: 3.0,
),
bottom: BorderSide(
//TODO: HERE IS THE PROBLEM.
color: Color(0xFFF05A22),
style: BorderStyle.solid,
width: 3.0,
),
),
color: Colors.green,
borderRadius: BorderRadius.circular(30.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text(
"BUTTON",
style: TextStyle(
color: Color(0xFFF05A22),
fontFamily: 'Montserrat',
fontSize: 16,
fontWeight: FontWeight.w600,
letterSpacing: 1,
),
),
),
],
),
),
),
),
],
),
);
}
}
I have labelled the areas of interest with //TODO:
I cannot even change their colours to white or black without them disappearing. I have spent about an hour trying to resolve this issue on my own with internet searches, with no luck.
Question:
Why can I have one colour but not another?
Expectation:
To change the top and left borders to black and the bottom and right to white.
In flutter "A borderRadius can only be given for uniform borders." so if you remove radius it will work. But if you want to keep radius it is not easy to do with just one line, there is some samples in this question: