Skip to content

How to redraw when time is up (i.e. time=0) #1

Closed
@pebreo

Description

@pebreo

This is an awesome project.

I am trying to use your example for a quiz app that shows this timer with a certain word in the center of the ring for only a certain amount of time then moves to a different word when time is up.

I'm stuck on something though. I've been trying to redraw the widget, but I get exceptions.

First, I first tried customizing didUpdateWidget like this:

// customize timer
class MyApp extends StatefulWidget {
  final String word;
  MyApp({this.word});
  @override
  MyAppState createState() => MyAppState();
}

// this seems to never be called when duration changes
void didUpdateWidget(TimerCircle oldWidget) {
    super.didUpdateWidget(oldWidget);
    Duration duration = controller.duration * controller.value;
    if(duration.inSeconds==0) {
      var route = new MaterialPageRoute(
        builder: (BuildContext context) =>
        new MyApp(word: 'next')
      );
      Navigator.of(context).push(route);
   }
  }

Then I tried customizing AnimatedBuilder to call Navigator:

void _gotoNextWord() {
  var route = new MaterialPageRoute(
    builder: (BuildContext context) =>
    new MyApp(word: 'next')
  );
  Navigator.of(context).push(route);
}

AnimatedBuilder(
  animation: controller,
  builder: (BuildContext context, Widget child) {
    
    Duration duration = controller.duration * controller.value;
    // call Navigator and pass in a new parameter
    if(duration.inMilliseconds==0)
      _gotoNextWord();
    
    return new CustomPaint(
        painter: TimerPainter(
      animation: controller,
      backgroundColor: Colors.white,
      color: themeData.indicatorColor,
    ));
  },
),
)

Here's the exception that I get:

Another exception was thrown: setState() or markNeedsBuild() called during build.

Here's a screenshot of the error in my emulator:

https://imgur.com/gallery/Afl7rYe

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions