How to give parent border to nested children in flutter? - flutter

I have several widgets which are reusable.
class ReusableWidget extends StatelessWidget {
build(context) {
return Container(
color: Colors.green,
child: Text('my widget')
);
}
They will be used in different layouts, in one of the layouts I need the page to have different borderRadius, so I decided to do something like this:
class LayoutWithRadius extends StatelessWidget {
final Widget child;
LayoutWithRadius(this.child);
build(context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.blue,
),
child: child
);
}
But the problem is that the radius will not be applied for the children containers, they have their own radius which will cover the parent's.
You have to note the child widgets themselves should not have radius for they are used in different layouts with different radius (and the radius should not be passed as parameter to widget).

change your LayoutWithRadius class to this
class LayoutWithRadius extends StatelessWidget {
final Widget child;
LayoutWithRadius(this.child);
build(context) {
return ClipRRect(
clipBehavior: Clip.antiAlias,
borderRadius: BorderRadius.circular(20),
child: child,
);
}
}

Related

how to make blur in flutter

I want to make When I click the sign-up button on the login page, blur is applied to the entire page and I want to show a pop-up window.
When clicking, I changed the filter value by specifying the sigmaX, Y values of the background filter as variables.
However, this uses the stack to place the backgrounddropfilter in front of the entire screen scaffold widget.When this happens, the button cannot be clicked because it goes behind the background filter.
how can I solve it?
Scaffold widgets with blur applied and scaffold widgets without blur
Isn't there a way to make each one and then print it out when the button is pressed?
Widget build(BuildContext context) {
return Stack(children: [
Scaffold(
appBar: AppBar(
...
),
body:ButtonTheme(
minWidth: 100.0,
height: 50.0,
child: ElevatedButton(
)
)
makeblur(a: )
)
);
}
class makeblur extends StatelessWidget {
makeblur({required this.a});
double a;
#override
Widget build(BuildContext context) {
return Positioned.fill(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: a, sigmaY: a),
child: Container(
color: Colors.transparent,
),
));
}
}
The child component of backdrop filter is the part that will not be blurred. So you may have to place the button as the child of the backdrop filter
BackdropFilter(
filter: ImageFilter.blur(sigmaX: a, sigmaY: a),
child: Button(),//<-- button that you wish not to blur here.
)
You can use this widget to have the effect, wrap your widget GlassMorphism
class GlassMorphism extends StatelessWidget {
GlassMorphism({
Key? key,
required this.blur,
required this.opacity,
required this.child,
BorderRadius? borderRadius,
}) : _borderRadius = borderRadius ?? BorderRadius.circular(12),
super(key: key);
final double blur;
final double opacity;
final Widget child;
final BorderRadius _borderRadius;
#override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: _borderRadius,
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: blur,
sigmaY: blur,
),
child: Container(
decoration: BoxDecoration(
color: ColorPallet.glassMorphism.withOpacity(opacity),
borderRadius: _borderRadius,
border: Border.all(
color: ColorPallet.glassMorphism.withOpacity(.2),
),
),
child: child,
),
),
);
}
}

Container color almost transparent in BoxDecoration with a predefined color function

The following code works fine to get the right container color. However, when I want to use the _green function replacing Color(0xff0c9869) in the BoxDecoration widget, the color turns out to be very pale/transparent.
It seems like the same solution, but the result is totally different, anyone knows why?
import 'package:flutter/material.dart';
class Home extends StatelessWidget {
final _green = Color(0xff0c9869);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
Container(
height: 200,
decoration: BoxDecoration(
color: Color(0xff0c9869),
borderRadius: BorderRadius.all(
Radius.circular(35),
),
),
child: Row(
children: [Text('Hi'), Icon(Icons.account_circle)],
),
)
]));
}
}

Flutter make container height match_parent

I have to build the following widget, which will be placed inside a ListView as parent element.
I have a problem with the green rectangle on the left side.
Basically I have structured my widget like that:
-> Card
-> Stack
-> Container for the "right" side (Tire ID, icon, temperature, pressure info)
-> Container for green rectangle,
-> Container with boxed decoration for green circle
-> Text with "5LO"
But this is how it looks like right now:
Basically the Container for the rectangle on the left side is not stretching it's height to full height of the parent stack. How can I do this?
Code:
class TireListItem extends StatelessWidget {
static const _circleSize = 36.0;
const TireListItem({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.zero,
key: Key('cardTire'),
child: Stack(
alignment: AlignmentDirectional.centerStart,
children: [
_TireContentInfoWidget(),
_buildTireIndicationRectangle(), // This should build the rectangle on the left side.
_buildTirePositionCircle(),
_buildTirePositionTextView(context)
],
));
}
Widget _buildTireIndicationRectangle() {
return Container(
width: _marginLeft,
decoration: BoxDecoration(
color: AppColor.green,
shape: BoxShape.rectangle,
borderRadius: BorderRadiusDirectional.horizontal(
start: Radius.circular(Dimens.cardCornerRadius))),
);
}
Container _buildTirePositionCircle() {
return Container(
width: _circleSize,
height: _circleSize,
decoration: BoxDecoration(color: AppColor.green, shape: BoxShape.circle),
);
}
Container _buildTirePositionTextView(BuildContext context) {
return Container(
margin: EdgeInsets.only(left: Dimens.spacingXs),
child: Text(
"2RO",
style:
Theme.of(context).textTheme.button.apply(color: AppColor.white),
));
}
}
If I set a fixed height to the rectangle container, it would basically work out, but I want that the text information area is defining the full height of the widget:
Widget _buildTireIndicationRectangle() {
return Container(
width: _marginLeft,
height: 150,
decoration: BoxDecoration(
color: AppColor.green,
shape: BoxShape.rectangle,
borderRadius: BorderRadiusDirectional.horizontal(
start: Radius.circular(Dimens.cardCornerRadius))),
);
}
You can solve this problem by using IntrinsicHeight class. If you wrapping Stack with InstrinctHeight, Stack's height is fixed by parent's height. Please look IntrinsicHeight document.
https://api.flutter.dev/flutter/widgets/IntrinsicHeight-class.html
#override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.zero,
key: Key('cardTire'),
child: IntrinsicHeight(child: Stack(
alignment: AlignmentDirectional.centerStart,
children: [
_TireContentInfoWidget(),
_buildTireIndicationRectangle(), // This should build the rectangle on the left side.
_buildTirePositionCircle(),
_buildTirePositionTextView(context)
],
),
),
);
}

Flutter : create a custom reusable widget image

I try to create a reusable widget but some error happens...
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class CustomLogo extends StatelessWidget {
#override
Widget showLogo(var nameImage, double radiusImage, double LeftPadding) {
return new Hero(
tag: 'hero',
child: Padding(
padding: EdgeInsets.fromLTRB(LeftPadding, 70.0, 0.0, 0.0),
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius: radiusImage,
child: Image.asset('assets/' + name),
),
),
);
}
}
And I don't understand override and the "construction" of the widget, how I can use var in the widget
You have to create a constructor to get values from where you are trying to call.
In following way you can create separate widget and pass arguments.
Moreover, here one mistake is left and it is hero tag. you are setting constant hero tag, which is fine if you are calling this widget once in a screen. if you are using this widget twice then it will not work because two hero's can’t have same tag in one screen. so, i also suggest you. to assign tag dynamically.
class CustomLogo extends StatelessWidget {
final nameImage;
final radiusImage;
final leftPadding;
CustomLogo({this.leftPadding, this.nameImage, this.radiusImage});
#override
Widget build(BuildContext context) {
return new Hero(
tag: 'hero',
child: Padding(
padding: EdgeInsets.fromLTRB(leftPadding, 70.0, 0.0, 0.0),
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius: radiusImage,
child: Image.asset('assets/' + nameImage),
),
),
);
}
}
How you can call or use this widget.
CustomLogo(leftPadding: 10,radiusImage: 5,nameImage: "hello",)

What is wrong with my code? It only shows a black screen

I am trying to make an application and now what I want to do is just create a container with shadows.
It said dead code so I deleted some code, and tried some other things.
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class appBar extends StatelessWidget {
appBar({this.title});
final Widget title;
#override
Widget build(BuildContext context) {
return Container(
height: 60.0,
padding: const EdgeInsets.all(10.0));
decoration: new BoxDecoration(color: Colors.cyan[500]);
child: Container(
decoration: new BoxDecoration(
boxShadow:[
BoxShadow(
color: Colors.black12,
blurRadius: 20,
spreadRadius: 4.0,
offset: Offset(
8.0,
8.0,
)
),
]
));
}
}
I expected a container but I got a black screen.
Instead of using decoration in Container to get the color. Just add color widget to your container. An example is as follows
return Container(
color:Colors.cyan
)
You see a black screen when you are missing a material widget. Wrapping the whole widget in a Scafold works.
I also see some wrong syntax, so here's the fix.
class appBar extends StatelessWidget {
appBar({this.title});
final Widget title;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: 60.0,
padding: const EdgeInsets.all(10.0),
decoration: new BoxDecoration(color: Colors.cyan[500]),
child: Container(
decoration: new BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black12,
blurRadius: 20,
spreadRadius: 4.0,
offset: Offset(
8.0,
8.0,
),
),
],
),
),
),
);
}
}
You have to warp your container with Scaffold widget. and you will get reslove your issue !! enjoy.because in flutter scaffold provide black screen with white canvas.
There were too many mistakes in your code, you unnecessarily used ; at the end of every line, and make sure your class name start with capital letter.
Here is the 100% working code.
void main() => runApp(MaterialApp(home: MyAppBar(title: "AppBar")));
class MyAppBar extends StatelessWidget {
MyAppBar({this.title});
final String title;
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
height: 60.0,
width: double.maxFinite,
padding: const EdgeInsets.all(10.0),
decoration: new BoxDecoration(color: Colors.cyan[500]),
child: Container(
child: Text(title ?? ''),
decoration: new BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black12,
blurRadius: 20,
spreadRadius: 4.0,
offset: Offset(8.0, 8.0),
),
],
),
),
),
),
);
}
}
appBar is not a nice class name. The convention is to start a class name with a capital letter. So AppBar would be nicer;
But AppBar is a flutter defined class, so... not a good choice;
your code has some ";" where you should have "," instead (you are inside of a Container function call, not a normal code body) Ex: padding: const EdgeInsets.all(10.0));
So i made corrections and tested your code this way (this is my main.dart file in a newly created flutter project):
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
#override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return AppBarCustom(title: Text('teste'));
}
}
class AppBarCustom extends StatelessWidget {
AppBarCustom({this.title});
final Widget title;
#override
Widget build(BuildContext context) {
return Container(
height: 60.0,
padding: const EdgeInsets.all(10.0),
decoration: new BoxDecoration(color: Colors.cyan[500]),
child: Container(
decoration: new BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 20,
spreadRadius: 10.0,
offset: Offset(
8.0,
8.0,
)),
],
),
),
);
}
}
I added a white background color to your inner Container just to show it more clearly and messed with the shadows...
Note your class at the end of the code.
He is the final rendering: