i want to implement camera and custom paint - flutter

Actually , i need a camera where after capturing a picture i can draw anything on that picture and save it.For that i already build a camera using camera plugin and custom paint . So now i want to merge this two. I am really confused about this matter. How can i do this ?
Here is my main.dart:
import 'package:flutter/material.dart';
import 'package:flutter_better_camera/new/src/support_android/camera.dart';
import 'package:painter_demo/camera.dart';
import 'package:painter_demo/painting.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: 'Camera App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Draw(),
);
}
}
Here i called the drawing class but i want to show over camera How can i do this ?

First, put your camera view and custom paint(or image from your assets) in Stack, and there are 2 approaches for image generation:
Capture your picture with camera shot, merge camera image and your custom paint (or asset image) with this package, image, with this function:
Image copyInto(Image dst, Image src, {int dstX, int dstY, int srcX, int srcY, int srcW, int srcH, bool blend = true});
Copy an area of the src image into dst.
Returns the modified dst image.
Make a custom button for screenshot (not camera shutter button), and follow the demo for screen shooting the Stack widget, screenshot

Related

Exception raised when tried to load an image from the Internet

Tried to use both NetworkImage and Image.network() methods to load jpg image from the Web, in both cases it says that there is "ImageCodecException" and it's "Fialed to load network image". Tried in DartPad.
This code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body:
// Load image from network
new Image.network(
'https://www.nasa.gov/images/content/162056main_PIA08329.jpg'),
),
);
}
}
At first try to wrap your Image.network to Center or something to align Image and the second one is check your url that you provide, you need to provide actual path for that just click to image and simply select "copy image adress" and put it in url. You can also check how to do it here - https://youtu.be/Ln2xvLs6dc4

Status Bar Color - Flutter

I need red status bar with white foreground for my entire app.
I'm using flutter_statusbarcolor package for this.
I did the following so far:
Added the package in pubsec.yaml
Imported the package in my main.dart file
Added the following lines of codes inside the build() of MyApp class
FlutterStatusbarcolor.setStatusBarColor(Colors.red[900]);
FlutterStatusbarcolor.setStatusBarWhiteForeground(true);
Result:
The status bar's color is red(working as it should).
The foreground color is white. But on restart changes to black. On hot reload, changes back to white. But on restart, it'll change back to black.
Here is my complete code:
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
FlutterStatusbarcolor.setStatusBarColor(Colors.red[900]);
FlutterStatusbarcolor.setStatusBarWhiteForeground(true);
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'App Title',
home: HomeScreen(),
);
}
}
I've also tried the following:
SystemChrome.setSystemUIOverlayStyle()
appBarTheme: Theme.of(context).appBarTheme.copyWith(brightness: Brightness.light)
Nothing's working. Can anyone please help me sustain the white foreground throughout the app?
You can try this from this Doc ==> setSystemUIOverlayStyle method
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.red,
);
return Placeholder(); // using As your Need
}
add this:
import 'package:flutter/services.dart'
use the code below after theme data
home: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
Hope it work...

How do I create an animation in After Effects for Flutter?

Currently in the company where I work, they are using Flutter, before I created the animations in After Effects, created a JSON and sent it to the developers. Now, I need to find a way in which After Affects talks to FLUTTER.
Use the lottie package. Add your JSON as an asset or host it somewhere. Then you can follow the example code(shown below) to display it.
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ListView(
children: [
// Load a Lottie file from your assets
Lottie.asset('assets/LottieLogo1.json'),
// Load a Lottie file from a remote url
Lottie.network(
'https://raw.githubusercontent.com/xvrh/lottie-flutter/master/example/assets/Mobilo/A.json'),
// Load an animation and its images from a zip file
Lottie.asset('assets/lottiefiles/angel.zip'),
],
),
),
);
}
}

Flutter app leaves blank space at the top of the screen when removing system overlays

When removing system overlays with SystemChrome.setEnabledSystemUIOverlays([]) the app does not expand to fill the screen.
Complete main.dart file:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIOverlays([]);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Container(
color: Colors.red,
),
);
}
}
Without SystemChrome.setEnabledSystemUIOverlays([]):
Flutter version: 1.22.2
I'm unable to reproduce with Flutter 1.22(stable) but in https://github.com/flutter/flutter/issues/14432 people seem to have had good results with
setting Scaffold.resizeToAvoidBottomPadding to true.
You can also try to update Flutter and/or changing channels. Perhaps even trying on a physical device.

Change color in rive (flare)

I have a .flr animation of a minion. Is it possible to change colors of his body, pants, eyes, etc dynamicaly and separately in flutter app?
PS Minion is just an example i found on rive.app .There will be another character with lots of different parts.
PPS Maybe there is a better way to make a simple animated character in flutter? For now, i have a stack with positioned colorfilterd images, but i guess it should be easier with rive.
Yes you can. There is an example in the Flare github:
https://github.com/2d-inc/Flare-Flutter/tree/master/example/change_color
import 'package:flare_flutter/flare_controller.dart';
import 'package:flare_flutter/flare.dart';
import 'package:flare_dart/math/mat2d.dart';
import 'package:flare_flutter/flare_actor.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
List<Color> exampleColors = <Color>[Colors.red, Colors.green, Colors.blue];
class _MyHomePageState extends State<MyHomePage> with FlareController {
FlutterColorFill _fill;
void initialize(FlutterActorArtboard artboard) {
// Find our "Num 2" shape and get its fill so we can change it programmatically.
FlutterActorShape shape = artboard.getNode("Num 2");
_fill = shape?.fill as FlutterColorFill;
}
void setViewTransform(Mat2D viewTransform) {}
bool advance(FlutterActorArtboard artboard, double elapsed) {
// advance is called whenever the flare artboard is about to update (before it draws).
Color nextColor = exampleColors[_counter % exampleColors.length];
if (_fill != null) {
_fill.uiColor = nextColor;
}
// Return false as we don't need to be called again. You'd return true if you wanted to manually animate some property.
return false;
}
// We're going to use the counter to iterate the color.
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++;
// advance the controller
isActive.value = true;
});
}
#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 Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: FlareActor("assets/change_color_example.flr", // You can find the example project here: https://www.2dimensions.com/a/castor/files/flare/change-color-example
fit: BoxFit.contain, alignment: Alignment.center, controller: this),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
If you're using the beta of Rive it's a bit different. I'm not sure if it's the best approach but I'm doing the following:
artboard.forEachComponent((child){
if (child.name == 'Fill') {
Fill fill = child;
fill.paint.color = Colors.red.withOpacity(0.25);
}
else if (child.name == 'Stroke') {
Stroke stroke = child;
stroke.paint.color = Colors.red;
}
});
As Kretin already outlined with the Flutter rive package it is possible even though it's beta as of writing.
To get this to work, you have to know thinks about your animation, for example the name of the shape you want to change (first), and the fill color you want to change.
Let's assume, I have an animation with a shape which name is 'Button1'.
This shape has one fill color that I'd like to change, then my code looks like this:
artboard.forEachComponent((child) {
if (child is Shape && child.name == 'Button1') {
final Shape shape = child;
shape.fills.first.paint.color = Colors.red;
}
});
I have tried the forEachComponent method that the other answers here mention, but it didn't work exactly like I expected. It iterated the components over and over again, spending a lot of process power and also because I was testing merging colors, it ended up merging again and again.
So I ended up checking the properties of the classes to find how to get the colors precisely and changing.
In my code, I'm changing a part of a color of linear gradients. But to get a solid color you can change
shape.fills.first.paintMutator.children.[first/last].color
to
shape.fills.first.paint.color
My code is like this:
final rootBone =
artboard.children.firstWhereOrNull((e) => e.name == 'Root Bone');
final rootBoneChildren = rootBone?.artboard?.drawables;
// Changing hair color
final hair =
rootBoneChildren?.firstWhereOrNull((e) => e.name == 'hair');
if (hair is Shape) {
((hair.fills.first.paintMutator as dynamic)?.children.first
as dynamic)
.color = AppColors.accent;
}
// Changing head color
final head =
rootBoneChildren?.firstWhereOrNull((e) => e.name == 'head');
if (head is Shape) {
final colorParts =
(head.fills.first.paintMutator as dynamic)?.children;
const mergeColor = AppColors.brown;
const timeline = 0.9;
// center
colorParts.first.color =
Color.lerp(colorParts.first.color, mergeColor, timeline);
// border
colorParts.last.color =
Color.lerp(colorParts.last.color, mergeColor, timeline);
}
ps: I'm casting as dynamic because it is a class called LinearGradient, extended from the paintMutator original class, and the class LinearGradient from rive has the same name as LinearGradient from flutter and I didn't want to use aliases everywhere in the code just because of those lines.