I am trying to make a colour transition animation inside a StreamBuilder which is inside a stateless widget. I have no idea how to perform that since all the examples and tutorials about this subject use a Stateful Widget.
I thought about using FadeTransition Widget but and maybe store the state in my Bloc that controls that view.
Please give me any suggestions if you have, Thank you.
To perform a fade in transition in a streambuilder, it's easy, simply use a widget called AnimatedSwitcher :
#override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
return AnimatedSwitcher(
duration: Duration(seconds: 1),
child: _getMainWidget(snapshot, context),
);
},
);
}
Here we use an AnimatedSwitcher to animate a transition when the child of the AnimatedSwitcher change's, the default animation is a fade animation, but you could add your custom animation by passing the widget a TransitionBuilder as an argument
Related
When I move from tabs in the UI it's takes too long to load the chips under the tabs.
I've used the AutomaticKeepAliveClientMixin<> but it broke my Scrollbar and still takes time to load large Chips children from the Wrap Widget. What I would need is a Wrap.builder() but doesn't exist. Is there something I can do to optimize the code to load smoothly?
I have an interactive sample code of the current problem:
https://dartpad.dev/009e9ccae07175074cb77d7792c3692b
Try moving the tabs left to right in the sample to see the performance issue.
If it can't be fixed, I wonder if I can detect when the widget is rendering to show a circular progress indicator while switching tabs.
Any Ideas?
Thanks
Technically the idea of Wrap.builder wouldn't be a solution here because the issue persists even when all the items are in the displayed (nothing to display on demand)
You can make the swiping experience better by delaying the rendering of the tab content until after the swiping animation is done
class _ChipsContentState extends State<ChipsContent> {
bool visible = false;
final List<String> _filters = <String>[];
#override
void initState() {
super.initState();
Future.delayed(kTabScrollDuration).then((value) {
if (mounted) {
setState(() => visible = true);
}
});
}
#override
Widget build(BuildContext context) {
if (!visible) {
return Center(child: CircularProgressIndicator());
}
return ListView(
children: [
if (visible)
Wrap(
spacing: 8.0,
children: widget.chipData.map((chipName) => ChipFilter(chipName: chipName, filter: _filters)).toList(),
),
],
);
}
}
A Wrap.builder() is indeed missing from Flutter. A GitHub issue was opened about this but then closed without a satisfying solution.
I can think of a few workarounds, like using a swiper/carousel, a GridView.builder(), or a List.builder() with Rows inside it. Of course, they're far from being as convenient as a Wrap in this case.
Try Changing your filterChips and "build" methods as follows,
List<Widget> get filterChips {
List<Widget> widgets = [];
for (final chipName in widget.chipData) {
widgets.add(ChipFilter(chipName: chipName, filter: _filters));
}
return widgets;
}
#override
Widget build(BuildContext context) {
// super.build(context);
return Scrollbar(
child: SingleChildScrollView(
child: Wrap(//IS THERE A Wrap.builder()? OR ALTERNATIVE TO DESIRED LAYOUT?
spacing: 8.0,
children: filterChips,
),),
);
}
I want to display a loading widget while compressing the image selected.
I am using the "image.dart" package to select an image, using ImagePicker.GetImage().
Then I want to display a loading widget while the image is being compressed, and when it's finished display the compressed image.
My code fundamentally works, my loading widget is cool (thanks spinkit), I can select any image, compress it and display the new one, but there is a small freeze (2-3s) while the image is being compressed.
So I'm trying to put a loading widget to not make the user panic and tap 10 000 times to select the image wanted, but I'm not completely comfortable with asynchronous code yet.
Here's my code :
import 'package:image/image.dart' as Img;
#override
Widget build(BuildContext context) {
if (loading == true) {
return LoadingScreen();
} else {
return Scaffold(
backgroundColor: Colors.brown[300],
body: Column(
children: [
SizedBox(
height: 50,
),
RaisedButton(
child: Text('Select an image'),
onPressed: () async {
await getImage();
// You can see here that I'm trying to trigger the loading widget
setState(() {
loading = true;
});
compressImage();
// And I want to disable the loading widget here, after the compression
setState(() {
loading = false;
});
},
),
SizedBox(
height: 10,
),
pickedImage != null ? printSelectedImage() : Center(child: Text('Please select an image')),
],
),
);
}
}
getImage() async {
pickedFile = await picker.getImage(source: ImageSource.gallery);
}
compressImage() async {
Img.Image selectedImage = Img.decodeJpg(File(pickedFile.path).readAsBytesSync());
if (selectedImage.width > selectedImage.height) {
Img.Image compressedImage = Img.copyResize(selectedImage, width: 500);
File(pickedFile.path).writeAsBytesSync(Img.encodePng(compressedImage));
} else {
Img.Image compressedImage = Img.copyResize(selectedImage, height: 500);
File(pickedFile.path).writeAsBytesSync(Img.encodePng(compressedImage));
}
if (pickedFile.path != null) {
setState(() {
pickedImage = pickedFile.path;
});
}
}
But the result is the same, the screen is still stuck in the file selector while compressing, and then directly display the compressed image.
I started learning dart/Flutter 2 weeks ago, so am I missing some fundamental principles of the dart language ? Am I not seeing something obvious ?
Thanks for reading
Making something async doesn't move it into some magical background thread. Dart uses isolates which are basically an execution context in which dart code can run. Flutter has a single isolate which runs your app, if you do too much work in it, your app gets slow or skips frames.
You can create other isolates to do calculations and offload work and communicate between isolates by passing messages with primitive content back and forth. The drawback in Flutter is, that other isolates don't have access to Flutter plugins thus a lot of things don't work there.
There are ways to work around this but they are pretty involved.
The easiest way to start an isolate is the compute(callback, message) function.
Spawn an isolate, run callback on that isolate, passing it message, and
(eventually) return the value returned by callback.
I am not sure if the image/image.dart library uses a plugin or some other features that are not available (dart:ui for example), you can try offloading your image compression into the callback. If it fails with a MissingPluginException or something similar, then it won't work.
Remember, you can only pass primitive messages so you can not path the pickedFile, only the pass as argument.
For your problem however there may be an easier solution. The image picker accepts parameters for max width/height and quality. If you need the image to be encoded as PNG however then you need to create a complex solution yourself.
I made an Android app long time ago where the main focus were a bunch of differently colored icon buttons representing psychological emotions. I realized that a few people I had it tested with were slightly confused about the icons, as they unconsciously associated different colors to these emotions than the ones I had chosen.
So, now I'm rewriting the app with Flutter, and I've decided that these icon buttons will have gentle gradients instead of solid colors, as it seems that specific color relationships, rather than colors, are more consistently associated with the emotions, plus it encourages association by learning instead of usage of innate ideas, since it makes the appearance of each icon harder to completely describe but easier to distinguish from the others.
Anyway, I'm using a ShaderMask with a LinearGradient, and an IconButton as child. Obviously it eats up GPU and rendering time is slightly above 16 ms. I thought of memoizing the rendered icon with the gradient, by pre-rendering it to an image then using it in the buttons. I've seen a good increase in performance, but the resulting code felt so hackish and buggy that I just reverted everything.
Is there a way to do this without feeling like I'm fighting the framework? Like, an official way to pre-render widgets, or at least to tell Flutter when something can be re-used without changes (performance is terrible even when updating a widget that doesn't have anything to do with these buttons).
Edit: memoizing these gradient icons would also allow me to use them elsewhere, increasing overall consistency whenever these emotions must be represented. Doing this right now would simply nuke performance.
Edit: code:
class MyButton extends StatelessWidget {
MyButton({this.index, this.constraints});
final int index;
final BoxConstraints constraints;
#override
Widget build(BuildContext context) {
return GradientMask(
child: IconButton(
onPressed: () {},
iconSize: constraints.maxWidth,
padding: EdgeInsets.all(0.0),
color: Colors.white,
icon: iconList[index],
),
index: index,
);
}
}
class GradientMask extends StatelessWidget {
GradientMask({this.index, this.child});
final int index;
final Widget child;
#override
Widget build(BuildContext context) {
return ShaderMask(
shaderCallback: (Rect bounds) => LinearGradient(
begin: Alignment.bottomLeft,
end: Alignment.topRight,
colors: gradientList[index],
tileMode: TileMode.repeated,
).createShader(Rect.fromLTWH(0.0, 0.0, 100.0, 100.0)),
child: child,
);
}
}
I'm using AnimatedOpacity to hide a the navbar based on a scroll event. I'm specifically having trouble making it animate in and out well, while also making the widget not clickable.
In order to hide my widget and prevent it from being clicked, I conditionally rendering the my CustomNavBar() component or Container() based on the visible state. However, this cuts my animation abruptly when i fade out. It works fading in, however.
AnimatedOpacity(
opacity: _isVisible ? 1.0 : 0.0,
duration: Duration(milliseconds: 300),
child: _isVisible
? CustomNavBar()
: Container(),
)
This is expected, since the component literally is not existing when _isVisible is false. That kills the whole animation. How do I get around this?
Thanks
There may be other ways to do this but you could use two variables for opacity and visibility. Make use of the AnimatedWidget's onEnd callback to change visibility. Also, you may want to use the Visibility widget for this. It lets you even maintain the child's state if you want.
/// somewhere outside of build in a StatefulWidget
bool _isVisible = true;
bool _isOpaque = true;
/// within the build method
AnimatedOpacity(
opacity: _isOpaque ? 1.0 : 0.0,
onEnd: (){
if(!_isOpaque)
setState((){
_isVisible = false;
});
},
child: Visibility(
child: child,
visible: _isVisible,
),
);
//then in some method to toggle the state...
setState((){
if(!_isVisible){
_isVisible = true;
_isOpaque = true;
}
else{
_isOpaque = false;
}
})
Come to think of it, you also have AnimatedSwitcher which is a LOT easier to do this. I will leave the above code for reference to the onEnd callback. Also, if maintaining state in the child widget is important then the above code would be better suited since you have that option. If it's not important then switch-away.
AnimatedSwitcher(
child: _isVisible ? child : SizedBox.shrink(),
)
IgnorePointer(
child: AnimatedOpacity(
child: child,
opacity: visible ? 1.0 : 0.0,
duration: const Duration(milliseconds: 200),
),
ignoring: !visible,
)
This is working for me like a charm. Basically, it says "ignore hit testing while child is invisible.
I also had a case for AppBar's particular action which I wanted to show / hide with animated opacity. After realizing that Opacity widget leaves child clickable I ended up using AnimatedSwitcher for the action Widget:
AnimatedSwitcher(
duration: Duration(milliseconds: 500),
child: !_visible
? SizedBox()
: IconButton(
icon: ... // more of the related code
),
)
I purposely negate the condition to put the empty SizedBox() to the top for the readability as the main widget can take a lot of lines in the code.
You can use your CustomNavBar instead of my IconButton:
AnimatedSwitcher(
duration: Duration(milliseconds: 500),
child: !_visible
? SizedBox()
: CustomNavBar(
... // more of the related code
),
)
I've followed the various animation tutorials on flutter.io, (tween, stagger, transitions, etc.) and its all great.
What I would like to explore is how to actually make custom animations based on the composition of a UI object.
Lets take a simple example, a Pause -> Play animation.
At first we have a Pause icon, two vertical bars.
Let's say I would like to
Grow the right bar into a triangle by adding an extra corner on the center of the rightmost vertical side, and moving it to the right.
After that moving that triangle from step 1 slightly to the left, so it now sticks to the leftmost vertical bar, into a bigger "triangle" (that'd be a pentagon actually).
That would look like a play button, and not a pause button anymore.
How would I achieve that kind of custom animation ? I'm assuming I can't work with the icons class. And I'm pretty sure I shouldn't do that with Widgets and just move them around.
Where would I go to start exploring that kind of precision in animations?
The answer from #Alaric points you at a couple of packages but doesn't really give any justification for why you'd use them.
The issue at hand is that the animation you're talking about is moderately complicated in terms of how it actually works. There are multiple items which change over time and possibly even become one bigger item.
There are two approaches you could take to solving this problem. The first is to use an external animation tool to create this animation, using whichever features the animation tool has to do item changing and merging. Then once you have an animation which runs to your satisfaction, you have to import it into your project somehow. That's where the fluttie and flare_flutter plugins come in - if you used Aftereffects, you use Lottie to export the file and then the fluttie plugin to show it. Flare is slightly simpler as it's meant for flutter, but you still create the animation externally and then add the file to your assets to be rendered.
The other approach is to do the animation yourself. That entails three things:
Creating a widget to contain the animation.
Creating a CustomPainter to actually draw the result.
Optionally, another class which acts as controller to start/stop/etc the animation.
The widget containing the animation could probably also be the controller if you use a GlobalKey to access it and expose start/stop methods, but that's a bit messy. It's better to have an external object that is the controller - and you could probably even use an AnimationController as-is although it would be less 'clean'.
If you don't pass it in, you'd probably have an AnimationController in your widget that you start and stop from your controller or class. It would essentially just go from 0 to 1 and back, and would be responsible for rebuilding the CustomPainter (probably using an AnimatedBuilder).
This is a very basic example that doesn't need an external controller as the gesture detection happens within the widget. Note that I'm not calling setState every time the 'started' member is set, because I don't actually want it to rebuild when it changes.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: StartStop(),
),
),
);
}
}
class StartStop extends StatefulWidget {
#override
StartStopState createState() {
return new StartStopState();
}
}
class StartStopState extends State<StartStop> with TickerProviderStateMixin<StartStop> {
bool started = false;
AnimationController animationController;
#override
void initState() {
animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 300));
super.initState();
}
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
started ? animationController.forward() : animationController.reverse();
started = !started;
},
child: SizedBox(
width: 100,
height: 100,
child: AnimatedBuilder(
animation: animationController,
builder: (context, child) {
return CustomPaint(
painter: StartStopPainter(animationController.value),
size: Size.infinite,
child: child,
);
},
),
),
);
}
}
class StartStopPainter extends CustomPainter {
final double percentAnimated;
StartStopPainter(this.percentAnimated) : assert(percentAnimated >= 0 && percentAnimated <= 1);
#override
void paint(Canvas canvas, Size size) {
var pausePaint = Paint()..color = Colors.black.withOpacity(1 - percentAnimated);
canvas.drawRect(Rect.fromLTRB(20, 10, 40, 90), pausePaint);
canvas.drawRect(Rect.fromLTRB(60, 10, 80, 90), pausePaint);
var playPaint = Paint()..color = Colors.black.withOpacity(percentAnimated);
canvas.drawPath(Path()..addPolygon([Offset(20, 10), Offset(20, 90), Offset(80, 50)], true), playPaint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
I'll leave the actual custom part of the animation (where you make the rectangle change to a triangle etc) to you. Instead of using opacity and a few different paint calls, you'd simply be using the input percentAnimated to decide which path or polygon to draw.