I am using CustomPainter where I need to draw line
class ShapesPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
final Paint firstPaint = Paint();
firstPaint.color = const Color.fromARGB(255, 236, 0, 140);
final Path firstPath = Path();
firstPath.lineTo(size.width, 0);
firstPath.lineTo(0, size.height * 0.10);
firstPath.close();
canvas.drawShadow(firstPath, Colors.black87, 2.0, false);
canvas.drawPath(firstPath, firstPaint);
}
}
I need to leave margin around the screen so I am using margin in container:
........
Container(
color: Colors.white,
margin:
EdgeInsets.only(top: 60.0, bottom: 20.0, left: 15.0, right: 15.0),
child: Container(
child: CustomPaint(
painter: ShapesPainter(),
child: Container(
.......
I need to draw a shadow under my custom path which I used anvas.drawShadow method in my build Widget, But there is a little shadow also coming over the left side, Please see the image below I pointed an error, here is pointing arrow to small shadow:
As I couldn't find any solution for the canvas.drawShadow effect on Path, I just created another Path() on top of the shadowed path, which solved the issue, but its kind of a hack.
final Path firstPathb = Path();
firstPathHide.lineTo(size.width, 0);
firstPathHide.lineTo(-10.0, size.height * 0.10);
firstPathHide.close();
canvas.drawPath(firstPathHide, firstPaint);
Related
I'm trying to recreate this:
I've tried with a PageView.builder, and got this far (sorry for the ugliness of colors but it's for visualization purposes):
This is the Code:
Container(
height: 40 * 2 + 100.0,
child: PageView.builder(
scrollDirection: Axis.horizontal,
controller: controller,
itemCount: 9,
itemBuilder: (context, index) {
double value = 1.0;
try {
value = controller.hasClients
? controller.page - index
: 1.0;
} catch (e) {
value = 1.0;
}
value = value.abs().clamp(0.0, 1.0);
value = 1 - value;
return Padding(
padding: EdgeInsets.symmetric(horizontal: 6.0),
child: Container(
width: value == 1.0 ? 80.0 : 60.0,
height: 30.0,
color: Color.lerp(Colors.green,
Colors.red, value),
child: Center(
child: Text(
index.toString(),
style: TextStyle(
color: Color.lerp(
Colors.red, Colors.black, value),
fontSize: lerpDouble(20.0, 40.0, value)),
)),
),
);
},
))
And the controller variable is this:
controller = new PageController(initialPage: 5, viewportFraction: 1/4);
controller.addListener(() {
setState(() {});
As you can see I managed to change the color and textSize to the "selected" item (the red ones), but even If I try to set its size to be bigger, it has no effects. I tried with a ListView.builder which made me set different sizes but with it I didn't have page Snapping and a default initial Page, so I opted out.
hope everything is clear.
Is it possible to do it, and if it is, how Could I do it?
I know its an old question but you can change width and height by changing margin of the container. You have to basically change a container (this one inside the Padding widget) margins values by value of your "value" variable and also I would highly suggest using AnimatedContainer for out of the box animations. Add this code to your container:
EdgeInsets.only(bottom: 50, right: value == 1.0? 20 : 40, left: value == 1.0? 20 : 40, top: value == 1.0? 150 : 250),
You should also remove this code from the container:
width: value == 1.0 ? 80.0 : 60.0,
height: 30.0,
I have a Container widget inside of a ClipPath which uses a CustomClipper. Everything works fine, I have the desired widget shape.
However, I could not find a way to make a shadow for this custom shaped Widget.
Also, I want to have an outline(border) that follows the edges of this custom widget automatically.
Again no luck. I tried BoxDecoration:border, BoxDecoration:boxShadow, ShapeDecoration:shape, ShapeDecoration:shadows, Material:Elevation, etc..
based on #Bohdan Uhrynovskiy I investigated further and came up with this solution:
CustomPaint(
painter: BoxShadowPainter(),
child: ClipPath(
clipper: MyClipper(), //my CustomClipper
child: Container(), // my widgets inside
)));
class BoxShadowPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
Path path = Path();
// here are my custom shapes
path.moveTo(size.width, size.height * 0.14);
path.lineTo(size.width, size.height * 1.0);
path.lineTo(size.width - (size.width *0.99) , size.height);
path.close();
canvas.drawShadow(path, Colors.black45, 3.0, false);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
You must need to provide your own custom paths in paint() method of BoxShadowPainter
Look at source code of the library. Feature implemented in this library seems very similar to your task.
You have to implement CustomPainter that draws shadows and borders.
return AspectRatio(
aspectRatio: 1.0,
child: CustomPaint(
painter: BoxShadowPainter(specs, boxShadows),
child: ClipPath(
clipper: Polygon(specs),
child: child,
)));
class Test extends StatelessWidget {
Widget build(BuildContext context) {
return UnconstrainedBox(
child: Container(
height: 250.0,
width: 250.0,
decoration:
BoxDecoration(shape: BoxShape.circle, color: Colors.red),
child: Opacity(
opacity: 0.5,
child: Container( // WIDGET IN QUESTION
constraints:
BoxConstraints.expand(width: 50.0, height: 50.0),
color: Colors.yellow))));
}
}
According to the Container class documentation...
If the widget has no child and no alignment, but a height, width, or constraints are provided, then the Container tries to be as small as possible given the combination of those constraints and the parent's constraints.
Instead, the widget is trying to be as large as possible (size of parent) rather than 50x50. I understand that I can use something like UnconstrainedBox, but I'm looking for an explanation of this behavior.
Looking for:
Currently getting:
The problem is your root Container.
By setting a width+height without an alignment, Container forces its child to fill the available space.
If you want that child take the least amount of space, you need to specify your root container how it should align its child within its bounds.
Container(
width: 250,
height: 250,
alignment: Alignment.center,
child: Whatever(),
);
Let's say I want to position a widget inside a Stack but with a percent of the Stack position instead of a fixed size. How to do that in flutter ?
I'd expect that the Positionned.fromRelativeRect constructor would be the thing, using floats between 0 and 1. But seems like no.
Align allows to position the widget in percent. But heightFactor and widthFactor changes the Align size instead of the child size. Which is not what I want.
You can combine a Positioned.fill and LayoutBuilder to achieve such result.
new Stack(
children: <Widget>[
new Positioned.fill(
child: new LayoutBuilder(
builder: (context, constraints) {
return new Padding(
padding: new EdgeInsets.only(top: constraints.biggest.height * .59, bottom: constraints.biggest.height * .31),
child: new Text("toto", textAlign: TextAlign.center,),
);
},
),
)
],
),
one thing that i figured out not long ago is that you can position a widget on the screen using a container its alignment parameter with the help of the Alignment.lerp(x,y,z) function
//the widget will be placed in the center of the container
alignment: Alignment.lerp(Alignment.topCenter, Alignment.bottomCenter, 0),
//the widget will be placed in the bottom of the container
alignment: Alignment.lerp(Alignment.topCenter, Alignment.bottomCenter, 1),
//the widget will be placed in the bottom quarter of the container
alignment: Alignment.lerp(Alignment.topCenter, Alignment.bottomCenter, 0.5),
//the widget will be placed in the top quarter of the container
alignment: Alignment.lerp(Alignment.topCenter, Alignment.bottomCenter, -0.5),
use FractionalOffset & FractionallySizedBox it's very simple in contrast
around no unnecessary code like Positioned.fill
no no extra calculations like Alignment
...
Container(
color: Colors.blue[200],
alignment: FractionalOffset(0.7, 0.6),
child: FractionallySizedBox(
widthFactor: 0.1,
heightFactor: 1/3,
child: Container(color: Colors.red[900])
),
),
...
If you want to use LayoutBuilder then do without Positioned.fill, like this:
you need one LayoutBuilder, no need to turn around every elements and use Transform.translate instead of Padding.
new LayoutBuilder(
builder: (context, constraints) {
return Stack(
children: <Widget>[
Transform.translate(
offset: Offset(
constraints.biggest.width * left,
constraints.biggest.height * top),
child: new Text("toto", textAlign: TextAlign.center,),
),
...
],
);
}
)
The Flutter docs show an example of rotating a "div" by 15 degrees, both for HTML/CSS and Flutter code:
The Flutter code is:
var container = new Container( // gray box
child: new Center(
child: new Transform(
child: new Text(
"Lorem ipsum",
),
alignment: FractionalOffset.center,
transform: new Matrix4.identity()
..rotateZ(15 * 3.1415927 / 180),
),
),
);
And the relevant parts are new Transform and alignment: FractionalOffset.center and transform: new Matrix4.identity()..rotateZ(15 * 3.1415927 / 180)
I'm curious, is there a simpler way to rotate a Container in Flutter? Is there a short-hand for the case of "15 degrees" ?
Thanks!
In mobile apps, I think it's kind of rare to have things start out rotated 15 degrees and just stay there forever. So that may be why Flutter's support for rotation is better if you're planning to adjust the rotation over time.
It feels like overkill, but a RotationTransition with an AlwaysStoppedAnimation would accomplish exactly what you want.
new RotationTransition(
turns: new AlwaysStoppedAnimation(15 / 360),
child: new Text("Lorem ipsum"),
)
If you want to rotate something 90, 180, or 270 degrees, you can use a RotatedBox.
new RotatedBox(
quarterTurns: 1,
child: new Text("Lorem ipsum")
)
You can use Transform.rotate to rotate your widget. I used Text and rotated it with 45˚ (π/4)
Example:
import 'dart:math' as math;
Transform.rotate(
angle: -math.pi / 4,
child: Text('Text'),
)
If you are working with a canvas (as in a CustomPaint widget), you can rotate 15 degrees like this:
import 'dart:math' as math;
class MyPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
canvas.save();
// rotate the canvas
final degrees = 15;
final radians = degrees * math.pi / 180;
canvas.rotate(radians);
// draw the text
final textStyle = TextStyle(color: Colors.black, fontSize: 30);
final textSpan = TextSpan(text: 'Hello, world.', style: textStyle);
TextPainter(text: textSpan, textDirection: TextDirection.ltr)
..layout(minWidth: 0, maxWidth: size.width)
..paint(canvas, Offset(0, 0));
canvas.restore();
}
#override
bool shouldRepaint(CustomPainter old) {
return false;
}
}
However, if you are doing something simple then I would use a RotatedBox or Transform.rotate as suggested by the other answers.
There is Two Main Flutter Widget available for this functionality, RotationTransition and Transform.rotate
another supported option is RotatedBox but this rotate widget only
supports quarter turns, which means they support vertical and only horizontal orientation.
and if you rotate already created widgets like Container so for the container by transformAlignmentyou can rotate widget.
RotationTransition: which animates the rotation of a widget, mainly we prefer when we need rotation with animation transition.https://api.flutter.dev/flutter/widgets/RotationTransition-class.html
Transform.rotate: which applies a rotation paint effect, they Create a widget that transforms its child using a rotation around the center.
RotationTransition Widget example:-
RotationTransition(
turns: AlwaysStoppedAnimation(15 / 360),
child: Text("flutter is awesome")
)
Transform.rotate Widget example :-
Transform.rotate(
angle: 15 * math.pi / 180,
child: Text("flutter is awesome")
)
Container(
decoration: BoxDecoration(borderRadius: BorderRadius.circular(50), color: Color(0xffF6F8FF),),
width: MediaQuery.of(context).size.width*0.6,
height: MediaQuery.of(context).size.height*0.4,
alignment:
new Alignment(0, 0),
transform:
new Matrix4.translationValues(MediaQuery.of(context).size.width * 0.55, -250.0, 0.0)
..rotateZ(28 * 3.1415927 / 180),
),