Flutter Transparent Container and BoxShadow - flutter

I am stuck on this. I am trying to learn Flutter. While I was doing my custom project I hit this wall. I know this can be solved with AppBar, maybe. I want to learn how to draw custom shadow or at least alter BoxShadow.
When I try to add a box shadow to my row which wrapped with container I get this result;
shadow
Code looks like this;
class HeaderContents extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
boxShadow: [BoxShadow(offset: Offset(0, 2), blurRadius: 5)]),
height: MediaQuery.of(context).size.height * 0.1,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
As you can guess I am just trying to get elevation result to the bottom. Thanks everyone.

Add the SpreaRadius and increase the OffsetY. Change all the value to get the desired need.
BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.blue.withOpacity(0.1),
spreadRadius: 5,
blurRadius: 20,
offset: Offset(
0, 10), // changes position of shadow
),
],
),

It seems it is just my retardness on assigning color. On main file I assigned both dark and bright colors for testing out dark/light mode functionality.
Changing this,
color: Theme.of(context).primaryColor
to this,
color: Theme.of(context).primaryColorDark
fixed the problem.

Related

Where are the elevation colors in Flutter material you?

I want my TextField widget to be elevated like Card or NavigationBar widgets. I'm using Material 3 (You).
I've tried using surface color from colors like:
Theme.of(context).colorScheme.surface
but it appears to be the same color as
Theme.of(context).colorScheme.background
You can wrap your text_form_field widget with Card widget and give it elevation as well as boxShadow. It will work for you.
Card(
child: //Your_text_form_field,
elevation: 10,
decoration: BoxDecoration(
boxShadow: [
new BoxShadow(
color: Colors.red,
blurRadius: 20.0,
),
],
),

How to animate color transitions in flutter?

I'm creating a custom checkbox-like widget that uses Card. I'm able to get the colors changing if the values are the same onTap. However, I want it to transition the color instead of it suddenly changing.
Here's my code:
return Card(
color: selected ? AppColors.primary : AppColors.primaryLight,
I'm relatively new to animations in Flutter, but coming from a strong Frontend/CSS understanding, the way I could achieve something like this is to add transition on a CSS property -- is there something similar to this in Flutter? If not, could someone point me in the right direction?
What I would do is instead of using the Card widget, I would use an AnimatedContainer and have the same condition as you for the color parameter. You will get a smooth transition when the selected value changes. However, you may need to also add boxShadow to have the same elevation effect as Card
AnimatedContainer(
duration: const Duration(milliseconds: 700),
curve: Curves.easeInOut,
decoration:BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 5,
spreadRadius: 5,
offset: Offset(0, 2),
),
],
color: selected ? AppColors.primary : AppColors.primaryLight,
),
)
One way to do this would be an AnimatedContainer
return Card(
child: AnimatedContainer(
duration: const Duration(milliseconds: 1000),
height: 200,
width: 200,
color: selected ? Colors.red : Colors.blue,
child: FooWidget(),
),
)

Flutter: How to remove the divider lines in CupertinoDatePicker?

While doing a coding walkthrough, I've never find a way to get rid of the divider lines.
How can I achieve this? For more info, I just created a new custom class by just copying the CupertinoDatePicker class.
Since you copied the CupertinoDatePicker class just remove the border in the _buildMagnifierScreen() method in picker.dart from /src/cupertino/
/// Draws the magnifier borders.
Widget _buildMagnifierScreen() {
final Color resolvedBorderColor = CupertinoDynamicColor.resolve(_kHighlighterBorder, context);
return IgnorePointer(
child: Center(
child: Container(
decoration: BoxDecoration(
// remove this attribute
border: Border(
top: BorderSide(width: 0.0, color: resolvedBorderColor),
bottom: BorderSide(width: 0.0, color: resolvedBorderColor),
),
),
constraints: BoxConstraints.expand(
height: widget.itemExtent * widget.magnification,
),
),
),
);
}

Theme.of(context) as const

I have several places with repetitive codes, for BoxDecoration
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(5),
boxShadow: [
BoxShadow(
color: Theme.of(context).hintColor.withOpacity(0.15),
offset: Offset(0, 3),
blurRadius: 10)
],
),
So I'd like to extract the BoxDecoration into separated dart file and reuse it.
Something like
static const boxDecoration = BoxDecoration(...);
then use it
final container = Container(
decoration: boxDecoration
)
But I get stuck on few things:
1. If I put it on file that only contains constants, I still need to access context, e.g. in Theme.of(context).primaryColor, which can only retrieved from build(Context) method
2. Then, I create a new stateless widget
import 'package:flutter/material.dart';
class FlutterTemplateStyle extends StatelessWidget {
static var boxDecoration;
#override
Widget build(BuildContext context) {
boxDecoration = BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(5),
boxShadow: [
BoxShadow(
color: Theme.of(context).hintColor.withOpacity(0.15),
offset: Offset(0, 3),
blurRadius: 10)
],
);
return Container();
}
}
And try to access decoration: FlutterTemplateStyle.boxDecoration, which doesn't work. I suppose this is because build is never called.
How can I achieve this constant styling?
Thanks
You cannot use const with any method call because methods cannot be evaluated at compile time - which is the requirement for a const value.
If you know that your primaryColor is constant, will never change, and that you will never want to dynamically change the color of your decoration, you can specify the color without the theme: color: const Color(0xfef1d2e1).
Otherwise, you do not want to use const. You might want to use final if you never reassign your variable, however, that will not work if you need to access your context.
Having said that, you will probably just want to create a function that returns your decoration, so you can use it in multiple places:
BoxDecoration myAwesomeBoxDecoration(BuildContext context) => BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(5),
boxShadow: [
BoxShadow(
color: Theme.of(context).hintColor.withOpacity(0.15),
offset: const Offset(0, 3),
blurRadius: 10,
),
],
);
Let me explain why I believe that the thought of making your BoxDecoration a constant is counterintuitive:
The color of your decoration depends on the theme of your app, which is something that is created entirely at runtime. This means that in order for your BoxDecoration to adapt to the Theme, it cannot be constant because it is adaptive.

Container shrinks to the size of his child

Why is my Container shrinks to the size of his child?
By the way, the container doesn't shrink if he is in slivers
Container(
height: 100,
decoration: BoxDecoration(
color: Colors.blue[50],
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 10.0,
offset: Offset(3.0, 10.0),
// spreadRadius: 2.0,
)
],
),
child: Icon(Icons.location_on,
color: Color(0XFF0D47A1), size: 60.0),
),
The way that widgets size themselves is actually quite complicated, and I don't think I can do a better job explaining than flutter's documentation: Layouts in Flutter. Different widget types size themselves differently depending on their children and their parents. So when a container is in a SliverList it might size itself differently than if it isn't.
However, the alignment property of the Container defines how the container sizes itself:
If non-null, the container will expand to fill its parent and position its child within itself according to the given value. If the incoming constraints are unbounded, then the child will be shrink-wrapped instead.
You could alternatively wrap the child in a Center or Alignment widget.