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

Updating the wheel from outside the widget #30

Open
lyngbach opened this issue Dec 6, 2021 · 3 comments
Open

Updating the wheel from outside the widget #30

lyngbach opened this issue Dec 6, 2021 · 3 comments

Comments

@lyngbach
Copy link

lyngbach commented Dec 6, 2021

Hello, thank you for this great plugin!

I am currently having trouble updating the the TimeRangePicker widget from outside of it.

My use case is that I use onStartChange and onEndChange to update some "external" DateTime variables. These variables are what initial sets the TimeRangePicker (which works fine). However when updating these variables from other sources like the showTimePicker I cant for the life of me update the state of this widget. I have tried all kinds of setState and force redrawn of the widget (I use the widget mode usecase), but no luck.

Am I missing something here? Is it not possible? A controller would be nice :)

@TiagoHellebrandt
Copy link
Contributor

Hi @lyngbach,
Have you tried to create a start and end in the parent widget state and pass on prop to TimeRangePicker? with that the onStartChange and onEndChange can create what it needs.

class TimePicker extends StatefulWidget {
  const TimePicker({
    Key? key,
    this.initialStart,
    this.initialEnd,
    this.onChange,
    this.onStartChange,
    this.onEndChange,
  }) : super(key: key);

  final TimeOfDay? initialStart;
  final TimeOfDay? initialEnd;
  final void Function(TimeOfDay start, TimeOfDay end)? onChange;
  final void Function(TimeOfDay start)? onStartChange;
  final void Function(TimeOfDay end)? onEndChange;

  @override
  _TimePickerState createState() => _TimePickerState();
}

class _TimePickerState extends State<TimePicker> {
  late TimeOfDay start;
  late TimeOfDay end;

  @override
  void initState() {
    super.initState();

    if (widget.initialStart != null && widget.initialEnd != null) {
      setState(() {
        start = widget.initialStart!;
        end = widget.initialEnd!;
      });
    } else {
      setState(() {
        start = const TimeOfDay(hour: 0, minute: 0);
        end = const TimeOfDay(hour: 1, minute: 0);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return TimeRangePicker(
      start: start,
      end: end,
      onStartChange: (TimeOfDay startTime) {
        setState(() {
          start = startTime;
        });
        widget.onStartChange?.call(startTime);
        widget.onChange?.call(startTime, end);
      },
      onEndChange: (TimeOfDay endTime) {
        setState(() {
          end = endTime;
        });
        widget.onEndChange?.call(endTime);
        widget.onChange?.call(start, endTime);
      },
    );
  }
}

This has worked for me.

Sorry for the English, I'm Brazilian, I'm still learning, I hope I helped.

@TiagoHellebrandt
Copy link
Contributor

I've done other tests here, if you update the start or end state the widget will not update at all, as the start or end is passed to the child state only in initState.

You can force render the widget as in the article: https://jelenaaa.medium.com/how-to-force-widget-to-redraw-in-flutter-2eec703bc024

@TiagoHellebrandt
Copy link
Contributor

However this is not ideal as it renders the entire widget.

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

No branches or pull requests

2 participants