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

Provide onTap function for BasicEvent type or add a BasicTouchEvent type #11

Closed
raLaaaa opened this issue May 29, 2020 · 4 comments · Fixed by #12
Closed

Provide onTap function for BasicEvent type or add a BasicTouchEvent type #11

raLaaaa opened this issue May 29, 2020 · 4 comments · Fixed by #12
Assignees
Labels
T: Feature Type: :tada: New Features

Comments

@raLaaaa
Copy link
Contributor

raLaaaa commented May 29, 2020

Hey there, I think it would be helpful if I could touch the basic widget and provide a function to handle the touch. This would allow me for example to mark events in the timetable or perform any other action while touching on the event body.

You could easily write your own Event to support that functionality how ever I think it's such a common thing that the library maybe should provide either a default event type for that (like for example BasicTouchEvent) or allow to give the BasicEvent an onTap function.

In the following I created a new event type to achieve that (basically taking the BasicEvent and just adding the onTap functionality):

class BasicTouchEvent extends Event {
  const BasicTouchEvent({
    @required Object id,
    @required this.title,
    @required this.color,
    @required this.onTap,
    @required LocalDateTime start,
    @required LocalDateTime end,
  })  : assert(title != null),
        super(id: id, start: start, end: end);

  final String title;
  final Color color;
  final VoidCallback onTap;

  @override
  bool operator ==(dynamic other) =>
      super == other && title == other.title && color == other.color;

  @override
  int get hashCode => hashList([super.hashCode, title, color]);
}

/// A simple [Widget] for displaying a [BasicEvent].
class BasicTouchWidget extends StatelessWidget {
  const BasicTouchWidget(this.event, {Key key})
      : assert(event != null),
        super(key: key);

  /// The [BasicEvent] to be displayed.
  final BasicTouchEvent event;

  @override
  Widget build(BuildContext context) {
    return Material(
      shape: RoundedRectangleBorder(
        side: BorderSide(
          color: context.theme.scaffoldBackgroundColor,
          width: 0.75,
        ),
        borderRadius: BorderRadius.circular(4),
      ),
      color: event.color,
      child: GestureDetector(
          onTap: event.onTap,
          child: Padding(
            padding: EdgeInsets.fromLTRB(4, 2, 4, 0),
            child: DefaultTextStyle(
              style: context.textTheme.bodyText2.copyWith(
                fontSize: 12,
                color: event.color.highEmphasisOnColor,
              ),
              child: Text(event.title),
            ),
          )),
    );
  }
}

From my opinion that would be a good thing to improve the out of the box functionality of the library. Might do a pull request if wished.

Cheers!

@raLaaaa raLaaaa added the T: Feature Type: :tada: New Features label May 29, 2020
@JonasWanke
Copy link
Owner

Thanks for the feature request! That's definitely a nice feature to have, I'll probably implement it tomorrow morning

@JonasWanke
Copy link
Owner

This is now implemented in v0.2.2. For a cleaner separation of concerns (data vs. UI), the onTap-parameter can be specified in BasicEventWidget & BasicAllDayEventWidget as opposed to specifying it in BasicEvent.

@raLaaaa
Copy link
Contributor Author

raLaaaa commented Jun 1, 2020

After thinking a while about this my mind raised a question. Adding the onTap parameter to the BasicEventWidget sounds legit regarding the seperation of concerns. How ever doesnt this lead to the fact that regardless of the event the onTap` will trigger the same method?

So what would be the solution if I need different callbacks depending on the event? I hope my question is clear.

@JonasWanke
Copy link
Owner

While all taps invoke the same lambda, you know the event at creation time of the BasicEventWidget and using code like the following you can also access it in the onTap-handler:

Timetable<BasicEvent>(
  eventBuilder: (event) {
    return BasicEventWidget(
      event,
      onTap: () {
        if (event.something) {
          // ...
        }
      },
      // or like this:
      onTap: () => myOnTapHandler(event),
    );
  },
);

void myOnTapHandler(BasicEvent event) {
  // ...
}

I hope this answers your question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: Feature Type: :tada: New Features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants