Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 21 days ago.
Improve this question
I want to create a circle progress indicator like that:
How to do that?
You can use multi color like this in CircularProgressIndicator().
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mis/app_extensions.dart';
class LoadingIndicator extends StatefulWidget {
const LoadingIndicator({ Key? key,}) : super(key: key);
#override
State<LoadingIndicator> createState() =>_LoadingIndicatorState();
}
class _LoadingIndicatorState extends State<LoadingIndicator>
with SingleTickerProviderStateMixin {
late AnimationController animationController;
#override
void initState() {
animationController =
AnimationController(vsync: this, duration: Duration(seconds: 1));
animationController.repeat();
super.initState();
}
#override
void dispose() {
animationController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
child: CircularProgressIndicator(
valueColor: animationController
.drive(ColorTween(begin: AppColors.blueTop, end: AppColors.red)),
),
);
}
}
Related
so I found myself doing the following without knowing actually why we should or should not do it.
let's say we have StatefulWidget:
class ExampleWidget extends StatefulWidget {
const ExampleWidget({super.key});
#override
State<ExampleWidget> createState() => ExampleStateWidget();
}
class ExampleStateWidget extends State<ExampleWidget> with TickerProviderStateMixin {
late AnimationController _controller;
#override
void initState() {
_controller = AnimationController(vsync: this, duration: Duration(microseconds: 300));
super.initState();
}
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Container();
}
}
why we should declare the the _controller inside the initState(), and not initialize it directly like this:
class ExampleWidget extends StatefulWidget {
const ExampleWidget({super.key});
#override
State<ExampleWidget> createState() => ExampleStateWidget();
}
class ExampleStateWidget extends State<ExampleWidget> with TickerProviderStateMixin {
AnimationController _controller = AnimationController(vsync: this, duration: Duration(microseconds: 300));
#override
void dispose() {
_controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Container();
}
}
what's the big difference between the two, I'm using the initState() but not knowing why exactly.
There are two types of widgets provided in Flutter.
The Stateful Widget
The Stateless Widget
As the name suggests Stateful Widgets are made up of some ‘States’. The initState() is a method that is called when an object for your stateful widget is created and inserted inside the widget tree. It is basically the entry point for the Stateful Widgets. initState() method is called only and only once and is used generally for initializing the previously defined variables of the stateful widget.
initState() method is overridden mostly because as mentioned earlier it is called only once in its lifetime. If you want to trigger it again you have to shift the control to an entirely new screen and a new state.
I activated the "Highlight repaints" and saw that the whole screen gets repainted all the time when using a widget with a AnimationController repeat().
Does anyone know if this is the intended behavior?
Here is a code example. If I use the widget in any screen, the whole screen is constantly repainted even if I just return a Container() and don't actually use the controller at all.
class ProgressBar extends StatefulWidget {
const ProgressBar({
Key? key,
}) : super(key: key);
#override
State<ProgressBar> createState() => _ProgressBarState();
}
class _ProgressBarState extends State<ProgressBar>
with SingleTickerProviderStateMixin {
AnimationController? _animController;
#override
void initState() {
super.initState();
_animController =
AnimationController(duration: const Duration(seconds: 4), vsync: this)
..repeat();
}
#override
void dispose() {
_animController?.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Container();
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can create fully custom widgets using dart programming for flutter
without using any packages
You can create custom widgets like this-
class CustomWidget extends StatefulWidget {
String? selectedOption;
CustomWidget({ this.selectedOption});
#override
_CustomWidgetState createState() => _CustomWidgetState();
}
class _CustomWidgetState extends State<CustomWidget> {
String? _dropDownValue;
TextEditingController t1Controller = TextEditingController();
#override
Widget build(BuildContext context) {
double _width = MediaQuery.of(context).size.width;
return Material(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
child: Container(
width: _width,
height: 50,
child: Widget// Add here your widgets
),
);
}
}
I want my text to animate through multiple colors in flutter how can I do it.
Pablo's answer (using ColorTween) will animate the color between two values. In order to transition among several colors, you can adapt that solution to either
build a "TweenSequence" chaining multiple color tweens
use RainbowColor which simplifies transition between multiple colors
See my article Multicolor Transitions in Flutter for a walkthrough on doing either.
For reference, here's a multi-color (B->G->R) animated text widget using RainbowColor.
class ColorText extends StatefulWidget {
const ColorText({
Key key,
}) : super(key: key);
#override
_ColorTextState createState() => _ColorTextState();
}
class _ColorTextState extends State<ColorText>
with SingleTickerProviderStateMixin {
AnimationController controller;
Animation<Color> _colorAnim;
#override
void initState() {
super.initState();
controller = AnimationController(duration: Duration(seconds: 3), vsync: this);
_colorAnim = RainbowColorTween([Colors.blue,
Colors.green,
Colors.red,
Colors.blue])
.animate(controller)
..addListener(() { setState(() {}); })
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.reset();
controller.forward();
} else if (status == AnimationStatus.dismissed) {
controller.forward();
}
});
controller.forward();
}
#override
Widget build(BuildContext context) {
return Text("Hello!", style: TextStyle(color: _colorAnim.value));
}
}
The example below animate a text color through a Colors range.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
MyAppState createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin{
AnimationController controller;
Animation animation;
Color color;
#override
#mustCallSuper
void initState() {
super.initState();
controller=AnimationController(
vsync: this,
duration: Duration(seconds: 5)
);
animation=ColorTween(begin:Colors.red,end: Colors.white).animate(controller);
animation.addListener((){
setState(() {
color=animation.value;
});
});
controller.forward();
}
#override
Widget build(BuildContext context) {
return MaterialApp(home:Scaffold(
appBar: AppBar(title: Text("Example")),
body:Center(child:Text("HELLO!!!",textScaleFactor:3,style: TextStyle(color: color),))));
}
}
I've created a screen in Flutter that displays a countdown timer. I'm able to play, pause and restart the timer, but I am trying to figure out how to perform an action when the timer reaches 0 (for example, restart itself).
As the dart file is fairly lengthy, I'm just copying below what I believe to be the relevant portions here. But I can add more if needed.
First I create a widget/class for the countdown timer:
class Countdown extends AnimatedWidget {
Countdown({ Key key, this.animation }) : super(key: key, listenable: animation);
Animation<int> animation;
#override
build(BuildContext context){
return Text(
animation.value.toString(),
style: TextStyle(
fontSize: 120,
color: Colors.deepOrange
),
);
}
}
I then have a stateful widget which creates the controller and also imports some data (gameData.countdownClock is the countdown timer's start time, it comes from user input at an earlier screen):
class _GameScreenState extends State<GameScreen> with TickerProviderStateMixin {
AnimationController _controller;
_GameScreenState(this.gameData);
GameData gameData;
#override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: gameData.countdownClock),
);
}
And then the container that displays the clock:
Container(
child: Countdown(
animation: StepTween(
begin: gameData.countdownClock,
end: 0,
).animate(_controller),
),
),
Do I have to add a listener in that last container? Or somewhere else? (Or something else entirely!)
Any help is appreciated. Thank you
I found the answer on this page:
After complete the widget animation run a function in Flutter
I needed to add .addStatusListener to the animation controller in the initState().
So the new initState() code looks like this:
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: gameData.countdownClock),
);
_controller.addStatusListener((status){
if(status == AnimationStatus.completed){
_controller.reset();
}
}
);
}
Possible value of Animation controller is between 0 to 1.
So I think you have to add listener on gamedata.countDownClock
Please check the below code you might get some idea from it.
import 'dart:async';
import 'package:flutter/material.dart';
class GameScreen extends StatefulWidget {
#override
_GameScreenState createState() => _GameScreenState();
}
class _GameScreenState extends State<GameScreen> with SingleTickerProviderStateMixin {
int countdownClock = 10;
#override
void initState() {
super.initState();
// Your Game Data Counter Change every one Second
const oneSec = const Duration(seconds:1);
new Timer.periodic(oneSec, (Timer t) {
// Restart The Counter Logic
if (countdownClock == 0)
countdownClock = 11;
setState(() { countdownClock = countdownClock - 1; });
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("")),
body: Center(
child: Text('$countdownClock')
),
);
}
}