How to implement a timer not on the root widget but on its own widget?Flutter Layout: How can I put a Row inside a Flex (or another Row) in Flutter? Also using Stack widgetFlutter : Can I add a Header Row to a ListViewHow to offset a scaffold widget in Flutter?How to implement cycle wheel scroll list widgetFlutter : Bad state: Stream has already been listened toHow to cancel a running Timer() when Widget disappearsshowDialog from root widgetHow I can put Widget above Widget using List<Widget> widget in Flutter?hello ,,,,,when I am clicking on card then show me this error how to fix this error

Is this resistor leaking? If so, is it a concern?

Rests in pickup measure (anacrusis)

What do different value notes on the same line mean?

Why is this Simple Puzzle impossible to solve?

Can a wire having a 610-670 THz (frequency of blue light) AC frequency supply, generate blue light?

Would the Geas spell work in a dead magic zone once you enter it?

What does it mean when you think without speaking?

Full horizontal justification in table

Plot twist where the antagonist wins

Why colon to denote that a value belongs to a type?

Placing bypass capacitors after VCC reaches the IC

What are these (utility?) boxes at the side of the house?

Tabulated absorption spectra of greenhouse gases?

1960s sci-fi novella with a character who is treated as invisible by being ignored

Is one obligated to listen to a Rav?

How many chess players are over 2500 Elo?

Why doesn't the Earth's acceleration towards the Moon accumulate to push the Earth off its orbit?

Is there a general effective method to solve Smullyan style Knights and Knaves problems? Is the truth table method the most appropriate one?

What is the largest (size) solid object ever dropped from an airplane to impact the ground in freefall?

Apparent Ring of Craters on the Moon

What's the Difference between Two Single-Quotes and One Double-Quote?

Is CD audio quality good enough for the final delivery of music?

Is there any use case for the bottom type as a function parameter type?

How to plot an unstable attractor?



How to implement a timer not on the root widget but on its own widget?


Flutter Layout: How can I put a Row inside a Flex (or another Row) in Flutter? Also using Stack widgetFlutter : Can I add a Header Row to a ListViewHow to offset a scaffold widget in Flutter?How to implement cycle wheel scroll list widgetFlutter : Bad state: Stream has already been listened toHow to cancel a running Timer() when Widget disappearsshowDialog from root widgetHow I can put Widget above Widget using List<Widget> widget in Flutter?hello ,,,,,when I am clicking on card then show me this error how to fix this error






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am working on a meditation app. Here's the link to the github repo: https://github.com/dscognitif/Sati_App



I am trying to implement a timer on one of the app states specifically the timer state. I have found some inspiration from a flutter tutorial: (https://github.com/tensor-programming/flutter_timer_example) on how to implement the timer but they've implemented it on the root widget of the app and I would like for it to be on its own widget. How could I accomplish this?



Here's the code:




import 'package:flutter/material.dart';
import 'dart:math' as math;

void main() => runApp(MaterialApp(
home: MyApp(),
theme: ThemeData(
canvasColor: Colors.blueGrey,
iconTheme: IconThemeData(
color: Colors.white,
),
accentColor: Colors.pinkAccent,
brightness: Brightness.dark,
),
));

class MyApp extends StatefulWidget
@override
MyAppState createState() => MyAppState();


class MyAppState extends State<MyApp> with TickerProviderStateMixin
AnimationController controller;

String get timerString
Duration duration = controller.duration * controller.value;
return '$duration.inMinutes:$(duration.inSeconds % 60).toString().padLeft(2, '0')';


@override
void initState()
super.initState();
controller = AnimationController(
vsync: this,
duration: Duration(seconds: 10),
);


@override
Widget build(BuildContext context)
ThemeData themeData = Theme.of(context);
return Scaffold(
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Align(
alignment: FractionalOffset.center,
child: AspectRatio(
aspectRatio: 1.0,
child: Stack(
children: <Widget>[
Positioned.fill(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return CustomPaint(
painter: TimerPainter(
animation: controller,
backgroundColor: Colors.white,
color: themeData.indicatorColor,
));
,
),
),
Align(
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Count Down",
style: themeData.textTheme.subhead,
),
AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return Text(
timerString,
style: themeData.textTheme.display4,
);
),
],
),
),
],
),
),
),
),
Container(
margin: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return Icon(controller.isAnimating
? Icons.pause
: Icons.play_arrow);
,
),
onPressed: ()
if (controller.isAnimating)
controller.stop();
else
controller.reverse(
from: controller.value == 0.0
? 1.0
: controller.value);

,
)
],
),
)
],
),
),
);



class TimerPainter extends CustomPainter
TimerPainter(
this.animation,
this.backgroundColor,
this.color,
) : super(repaint: animation);

final Animation<double> animation;
final Color backgroundColor, color;

@override
void paint(Canvas canvas, Size size)
Paint paint = Paint()
..color = backgroundColor
..strokeWidth = 5.0
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;

canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
paint.color = color;
double progress = (1.0 - animation.value) * 2 * math.pi;
canvas.drawArc(Offset.zero & size, math.pi * 1.5, -progress, false, paint);


@override
bool shouldRepaint(TimerPainter old)
return animation.value != old.animation.value











share|improve this question






















  • you want an animated CustomPainter or something else? what do you want to achieve actually?

    – pskink
    Mar 24 at 7:13












  • Yes I want to achieve an animated CustomPainter as a timer but not on the root widget as this code shows but on another widget.

    – Ronin Winter
    Mar 24 at 7:51






  • 1





    so add CustomPaint to that "another widget", what is the problem here?

    – pskink
    Mar 24 at 7:52












  • Alright, let me try that. Thanks!

    – Ronin Winter
    Mar 24 at 8:44

















0















I am working on a meditation app. Here's the link to the github repo: https://github.com/dscognitif/Sati_App



I am trying to implement a timer on one of the app states specifically the timer state. I have found some inspiration from a flutter tutorial: (https://github.com/tensor-programming/flutter_timer_example) on how to implement the timer but they've implemented it on the root widget of the app and I would like for it to be on its own widget. How could I accomplish this?



Here's the code:




import 'package:flutter/material.dart';
import 'dart:math' as math;

void main() => runApp(MaterialApp(
home: MyApp(),
theme: ThemeData(
canvasColor: Colors.blueGrey,
iconTheme: IconThemeData(
color: Colors.white,
),
accentColor: Colors.pinkAccent,
brightness: Brightness.dark,
),
));

class MyApp extends StatefulWidget
@override
MyAppState createState() => MyAppState();


class MyAppState extends State<MyApp> with TickerProviderStateMixin
AnimationController controller;

String get timerString
Duration duration = controller.duration * controller.value;
return '$duration.inMinutes:$(duration.inSeconds % 60).toString().padLeft(2, '0')';


@override
void initState()
super.initState();
controller = AnimationController(
vsync: this,
duration: Duration(seconds: 10),
);


@override
Widget build(BuildContext context)
ThemeData themeData = Theme.of(context);
return Scaffold(
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Align(
alignment: FractionalOffset.center,
child: AspectRatio(
aspectRatio: 1.0,
child: Stack(
children: <Widget>[
Positioned.fill(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return CustomPaint(
painter: TimerPainter(
animation: controller,
backgroundColor: Colors.white,
color: themeData.indicatorColor,
));
,
),
),
Align(
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Count Down",
style: themeData.textTheme.subhead,
),
AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return Text(
timerString,
style: themeData.textTheme.display4,
);
),
],
),
),
],
),
),
),
),
Container(
margin: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return Icon(controller.isAnimating
? Icons.pause
: Icons.play_arrow);
,
),
onPressed: ()
if (controller.isAnimating)
controller.stop();
else
controller.reverse(
from: controller.value == 0.0
? 1.0
: controller.value);

,
)
],
),
)
],
),
),
);



class TimerPainter extends CustomPainter
TimerPainter(
this.animation,
this.backgroundColor,
this.color,
) : super(repaint: animation);

final Animation<double> animation;
final Color backgroundColor, color;

@override
void paint(Canvas canvas, Size size)
Paint paint = Paint()
..color = backgroundColor
..strokeWidth = 5.0
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;

canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
paint.color = color;
double progress = (1.0 - animation.value) * 2 * math.pi;
canvas.drawArc(Offset.zero & size, math.pi * 1.5, -progress, false, paint);


@override
bool shouldRepaint(TimerPainter old)
return animation.value != old.animation.value











share|improve this question






















  • you want an animated CustomPainter or something else? what do you want to achieve actually?

    – pskink
    Mar 24 at 7:13












  • Yes I want to achieve an animated CustomPainter as a timer but not on the root widget as this code shows but on another widget.

    – Ronin Winter
    Mar 24 at 7:51






  • 1





    so add CustomPaint to that "another widget", what is the problem here?

    – pskink
    Mar 24 at 7:52












  • Alright, let me try that. Thanks!

    – Ronin Winter
    Mar 24 at 8:44













0












0








0


0






I am working on a meditation app. Here's the link to the github repo: https://github.com/dscognitif/Sati_App



I am trying to implement a timer on one of the app states specifically the timer state. I have found some inspiration from a flutter tutorial: (https://github.com/tensor-programming/flutter_timer_example) on how to implement the timer but they've implemented it on the root widget of the app and I would like for it to be on its own widget. How could I accomplish this?



Here's the code:




import 'package:flutter/material.dart';
import 'dart:math' as math;

void main() => runApp(MaterialApp(
home: MyApp(),
theme: ThemeData(
canvasColor: Colors.blueGrey,
iconTheme: IconThemeData(
color: Colors.white,
),
accentColor: Colors.pinkAccent,
brightness: Brightness.dark,
),
));

class MyApp extends StatefulWidget
@override
MyAppState createState() => MyAppState();


class MyAppState extends State<MyApp> with TickerProviderStateMixin
AnimationController controller;

String get timerString
Duration duration = controller.duration * controller.value;
return '$duration.inMinutes:$(duration.inSeconds % 60).toString().padLeft(2, '0')';


@override
void initState()
super.initState();
controller = AnimationController(
vsync: this,
duration: Duration(seconds: 10),
);


@override
Widget build(BuildContext context)
ThemeData themeData = Theme.of(context);
return Scaffold(
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Align(
alignment: FractionalOffset.center,
child: AspectRatio(
aspectRatio: 1.0,
child: Stack(
children: <Widget>[
Positioned.fill(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return CustomPaint(
painter: TimerPainter(
animation: controller,
backgroundColor: Colors.white,
color: themeData.indicatorColor,
));
,
),
),
Align(
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Count Down",
style: themeData.textTheme.subhead,
),
AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return Text(
timerString,
style: themeData.textTheme.display4,
);
),
],
),
),
],
),
),
),
),
Container(
margin: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return Icon(controller.isAnimating
? Icons.pause
: Icons.play_arrow);
,
),
onPressed: ()
if (controller.isAnimating)
controller.stop();
else
controller.reverse(
from: controller.value == 0.0
? 1.0
: controller.value);

,
)
],
),
)
],
),
),
);



class TimerPainter extends CustomPainter
TimerPainter(
this.animation,
this.backgroundColor,
this.color,
) : super(repaint: animation);

final Animation<double> animation;
final Color backgroundColor, color;

@override
void paint(Canvas canvas, Size size)
Paint paint = Paint()
..color = backgroundColor
..strokeWidth = 5.0
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;

canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
paint.color = color;
double progress = (1.0 - animation.value) * 2 * math.pi;
canvas.drawArc(Offset.zero & size, math.pi * 1.5, -progress, false, paint);


@override
bool shouldRepaint(TimerPainter old)
return animation.value != old.animation.value











share|improve this question














I am working on a meditation app. Here's the link to the github repo: https://github.com/dscognitif/Sati_App



I am trying to implement a timer on one of the app states specifically the timer state. I have found some inspiration from a flutter tutorial: (https://github.com/tensor-programming/flutter_timer_example) on how to implement the timer but they've implemented it on the root widget of the app and I would like for it to be on its own widget. How could I accomplish this?



Here's the code:




import 'package:flutter/material.dart';
import 'dart:math' as math;

void main() => runApp(MaterialApp(
home: MyApp(),
theme: ThemeData(
canvasColor: Colors.blueGrey,
iconTheme: IconThemeData(
color: Colors.white,
),
accentColor: Colors.pinkAccent,
brightness: Brightness.dark,
),
));

class MyApp extends StatefulWidget
@override
MyAppState createState() => MyAppState();


class MyAppState extends State<MyApp> with TickerProviderStateMixin
AnimationController controller;

String get timerString
Duration duration = controller.duration * controller.value;
return '$duration.inMinutes:$(duration.inSeconds % 60).toString().padLeft(2, '0')';


@override
void initState()
super.initState();
controller = AnimationController(
vsync: this,
duration: Duration(seconds: 10),
);


@override
Widget build(BuildContext context)
ThemeData themeData = Theme.of(context);
return Scaffold(
body: Padding(
padding: EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
child: Align(
alignment: FractionalOffset.center,
child: AspectRatio(
aspectRatio: 1.0,
child: Stack(
children: <Widget>[
Positioned.fill(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return CustomPaint(
painter: TimerPainter(
animation: controller,
backgroundColor: Colors.white,
color: themeData.indicatorColor,
));
,
),
),
Align(
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Count Down",
style: themeData.textTheme.subhead,
),
AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return Text(
timerString,
style: themeData.textTheme.display4,
);
),
],
),
),
],
),
),
),
),
Container(
margin: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton(
child: AnimatedBuilder(
animation: controller,
builder: (BuildContext context, Widget child)
return Icon(controller.isAnimating
? Icons.pause
: Icons.play_arrow);
,
),
onPressed: ()
if (controller.isAnimating)
controller.stop();
else
controller.reverse(
from: controller.value == 0.0
? 1.0
: controller.value);

,
)
],
),
)
],
),
),
);



class TimerPainter extends CustomPainter
TimerPainter(
this.animation,
this.backgroundColor,
this.color,
) : super(repaint: animation);

final Animation<double> animation;
final Color backgroundColor, color;

@override
void paint(Canvas canvas, Size size)
Paint paint = Paint()
..color = backgroundColor
..strokeWidth = 5.0
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;

canvas.drawCircle(size.center(Offset.zero), size.width / 2.0, paint);
paint.color = color;
double progress = (1.0 - animation.value) * 2 * math.pi;
canvas.drawArc(Offset.zero & size, math.pi * 1.5, -progress, false, paint);


@override
bool shouldRepaint(TimerPainter old)
return animation.value != old.animation.value








flutter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 7:08









Ronin WinterRonin Winter

33




33












  • you want an animated CustomPainter or something else? what do you want to achieve actually?

    – pskink
    Mar 24 at 7:13












  • Yes I want to achieve an animated CustomPainter as a timer but not on the root widget as this code shows but on another widget.

    – Ronin Winter
    Mar 24 at 7:51






  • 1





    so add CustomPaint to that "another widget", what is the problem here?

    – pskink
    Mar 24 at 7:52












  • Alright, let me try that. Thanks!

    – Ronin Winter
    Mar 24 at 8:44

















  • you want an animated CustomPainter or something else? what do you want to achieve actually?

    – pskink
    Mar 24 at 7:13












  • Yes I want to achieve an animated CustomPainter as a timer but not on the root widget as this code shows but on another widget.

    – Ronin Winter
    Mar 24 at 7:51






  • 1





    so add CustomPaint to that "another widget", what is the problem here?

    – pskink
    Mar 24 at 7:52












  • Alright, let me try that. Thanks!

    – Ronin Winter
    Mar 24 at 8:44
















you want an animated CustomPainter or something else? what do you want to achieve actually?

– pskink
Mar 24 at 7:13






you want an animated CustomPainter or something else? what do you want to achieve actually?

– pskink
Mar 24 at 7:13














Yes I want to achieve an animated CustomPainter as a timer but not on the root widget as this code shows but on another widget.

– Ronin Winter
Mar 24 at 7:51





Yes I want to achieve an animated CustomPainter as a timer but not on the root widget as this code shows but on another widget.

– Ronin Winter
Mar 24 at 7:51




1




1





so add CustomPaint to that "another widget", what is the problem here?

– pskink
Mar 24 at 7:52






so add CustomPaint to that "another widget", what is the problem here?

– pskink
Mar 24 at 7:52














Alright, let me try that. Thanks!

– Ronin Winter
Mar 24 at 8:44





Alright, let me try that. Thanks!

– Ronin Winter
Mar 24 at 8:44












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321481%2fhow-to-implement-a-timer-not-on-the-root-widget-but-on-its-own-widget%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid …


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321481%2fhow-to-implement-a-timer-not-on-the-root-widget-but-on-its-own-widget%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?