Remove mirrored effect from front camera - dart

I am currently using the camera plug-in on flutter. Is there a way to remove the mirror effect from the front camera (selfie camera) when you take a picture?

You can use the transform widget to manually mirror the camera widget:
import 'dart:math' as math; // import this
Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(math.pi),
child: /*Your Camera Widget*/,
)

In my case I let the camera widget normal and I apply the transform in preview.
import 'dart:math' as math;
if (bytesImage != null) ...[
SizedBox(
width: double.infinity,
height: double.infinity,
child: Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(math.pi),
child: Image.memory(
bytesImage!,
fit: BoxFit.cover,
),
),
),
],

Related

Is there a more optimal way to space widgets within a column?

So i'm emulating an old app I have where the UI looks like this
Currently i'm focused on creating the UI layout for the purple side bar, I did that by creating a Container with a purple background. Within the container I created a column with multiple children and just used an empty SizedBox to create distance between one widget from another.
import 'package:flutter/material.dart';
class SignInPage extends StatelessWidget {
const SignInPage({super.key});
#override
Widget build(BuildContext context) {
return Scaffold(
// appBar: AppBar(
// title: Text('Sample Text'),
// elevation: 5.0,
// ),
body: _buildContent(),
);
}
Widget _buildContent() {
// private method
return Container(
color: Colors.deepPurpleAccent,
padding: EdgeInsets.all(16.0),
child: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
color: Colors.orange,
child: SizedBox(
height: 100.0,
width: 80.0,
),
),
SizedBox(
height: 140.0,
),
Container(
color: Colors.red,
child: SizedBox(
height: 50.0,
width: 80.0,
),
),
SizedBox(
height: 8.0,
),
Container(
color: Colors.purple,
child: SizedBox(
height: 50.0,
width: 80.0,
),
),
],
),
);
}
}
I'm quite new to flutter so i'm wondering if there's a better way to structure my layout of the side bar? Also to think in the future, since the name "BagTrack" Is on the same level as "Analytic Overview" Should that just be one giant row"?
Actual answer
Method 1
#Mahfuzur Rahman answer is good, but to actually answer your question about other ways. Flex widgets (Column and Row extend the Flex widget), have both mainAxisAlignment and crossAxisAlignment, they can be used to align them more easily between different devices/screens sizes.
So maybe grouping your red and purple boxes in a Column with mainAxisSize set to MainAxisSize.min, and aligning the surrounding column with a specific size.
https://api.flutter.dev/flutter/widgets/Column-class.html
Method 2
Another way, if you would like some widget to occupy some percentage amount of space from it's parent, I suggest you look into Expanded widget, Flexible widget.
Method 3
Or even FractionallySizedBox could be a good widget for you, but then I would also look at this LayoutBuilder widget to solve the potential Unbounded Height/Width exception.
Second smaller question
It's entirely up to you to decide about your second question concerning the giant row. I wouldn't do it though. Probably would use a const SizedBox or const EdgeInsets (for Padding) and keep them at the same height this way.
Just complementing Flutter knowledge
PS: Since you are new to Flutter. As a suggestion for future performance: avoid the Container widget as much as you know, there are a lot of simpler widgets like SizedBox, ColoredBox, DecoratedBox and Padding that you can use in its place that could be marked as const sometimes and be less expensive.
For understanding final and const:
final is a variable that cannot be reassigned by accident inside your code. When you instantiate it you give it a value and that's it. (Using late changes that a bit but not much);
const is a variable assigned by the compiler, if you are familiar with C it's like #define but there is a little difference, every time you say const EdgeInsets.all(8) for example, the compiler will detect that and use the same variable, so you don't have to remember a specific constant variable name.
Yes there is. But using SizedBox also wont hurt.
I usually Prefer ListTile for each element in the drawer.
ListTile(
leading: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {},
child: Container(
width: 48,
height: 48,
padding: const EdgeInsets.symmetric(vertical: 4.0),
alignment: Alignment.center,
child: const CircleAvatar(),
),
),
title: const Text('title'),
dense: false,
)

Making Flutter Camera Fullscreen on IOS

on an android device I am able to use this code:
final size = MediaQuery.of(context).size;
var scale = size.aspectRatio *
Get.find<cameraservice>().camcontroller!.value.aspectRatio;
if (scale < 1) {
scale = 1 / scale;
}
return Transform.scale(
scale: scale,
child: Center(
child: CameraPreview(Get.find<cameraservice>().camcontroller!),
),
);
however, using the same code running on an IOS device, I see a white border on the sides.
any help would be appreciated.
using camera
camera 0.9.4+12 package.
Inside your Scaffold, you can do the following (and given that controller is your Camera controller instance (your Get.find().camcontroller!):
Scaffold(
body: Transform.scale(
scale: controller!.value.aspectRatio / MediaQuery.of(context).size.aspectRatio,
child: Center(
child: AspectRatio(
aspectRatio: controller!.value.aspectRatio,
child: CameraPreview(controller!)
)
)
)
)

Flutter: Constraints not constraining widget (explanation needed)

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(),
);

Position widget in stack with percent

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,),
),
...
],
);
}
)

How do I rotate something 15 degrees in Flutter?

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),
),

Resources