Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the displayed time is not working #16

Closed
JogyBlack opened this issue Feb 26, 2022 · 4 comments · Fixed by #18
Closed

Changing the displayed time is not working #16

JogyBlack opened this issue Feb 26, 2022 · 4 comments · Fixed by #18

Comments

@JogyBlack
Copy link

JogyBlack commented Feb 26, 2022

I have a static clock (isLive: false) that displays a time. Then, as a result of user action, it has to display a different time.
This does not work. Instead of displaying the second time, the AnalogClock widget starts displaying the current system time.

Minimal example to demonstrate the problem:

import 'package:analog_clock/analog_clock.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DateTime time = DateTime(2022, 1, 1, 12, 30, 0);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: AnalogClock(
          isLive: false,
          datetime: time,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => setState(() => {
          time = time.add(const Duration(minutes: 15))
        }),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

When tapping the (+) button, the clock should show 12:45. Instead it shows the current time.

@JogyBlack
Copy link
Author

Looking at the source of the AnalogClock widget, the bug is in didUpdateWidget:

    if (!widget.isLive && widget.datetime != oldWidget.datetime) {
      datetime = widget.datetime ?? DateTime.now();
      datetime = DateTime.now();
    }

The second assignment statement is wrong, as it unconditionally sets the clock time to the current time.

@SimonTellier
Copy link

if (widget.isLive && widget.datetime != oldWidget.datetime) { datetime = DateTime.now(); }
(isLive: true)

@walles
Copy link
Contributor

walles commented Jun 27, 2022

The second assignment statement is wrong, as it unconditionally sets the clock time to the current time.

@JogyBlack did you consider making a PR?

walles added a commit to walles/analog_clock that referenced this issue Jun 30, 2022
walles added a commit to walles/barnklocka2 that referenced this issue Jun 30, 2022
walles added a commit to walles/barnklocka2 that referenced this issue Jul 2, 2022
@furkantektas
Copy link
Owner

Thank you @JogyBlack for spotting the bug and fixing it :) also for providing an example use case, @walles's PR (#18) fixed that bug. Thanks @walles for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants