r/FlutterDev • • Oct 06 '20

Article Navigation design patterns with Flutter

https://medium.com/flutter-community/navigation-done-right-a-case-for-hierarchical-routing-with-flutter-ca0aac1275ad
130 Upvotes

7 comments sorted by

14

u/gmatuella Oct 06 '20

That’s a REALLY useful article. We need way more fucking things like these. I am currently developing another production flutter app and my main concerns were navigation and dependency injections. Coming from an iOS background, I tried to implement a coordinator pattern, but it had some flaws, because it was not a widget per-se. Attaching it to a tree and making use of it just like a provider makes way more sense. Thanks for the great insights (also kudos for writing in decent english, not many do this).

3

u/yarolegovich Oct 06 '20

Thanks. I can say that reading feedback like yours is absolutely worth the time put into the article.

4

u/esDotDev Oct 06 '20

Cool approach, but it does feel like a lot of architectural complexity here to manage a fairly simple use case. Seems like there is probably a more elegant/simple way to manage a sub-set of a flow, with a custom startpt, and endpt.

Still really nice food for thought, thanks for the post!

3

u/yarolegovich Oct 06 '20

yup, I agree, a bit non-trivial initial setup is required and there'll be some boilerplate all along the way, but out of all the approaches I've tried, this one showed best at minimizing the project complexity growth in the long run

1

u/esDotDev Oct 06 '20 edited Oct 06 '20

Ya, at first blush I would think some sorta Flow widget, that took a list of ()=>SomePage() builders, and could basically manage the flow between them using a Nested Navigator, or Indexed Stack, or whatever.... Then you could just setup a few pre-made wrappers around FlowWidget for various use cases, recombining the sub-views as needed.

This could probably be enabled pretty easily using a Notification? Have you dug into those at all? They're typed event payloads in Flutter, and really nice. Your screen btns could simply say, "FlowForward" or "FlowBack", the flow widget could catch that and then update the view stack?https://api.flutter.dev/flutter/widgets/NotificationListener-class.html

In that case your architectural complexity goes to nothing, and you could still support the coupled/simple use case by doing something like:

onPress: widget.deferNavigationToParent? _dispatchNotification() : _loadDefaultPage()

Which gives each view default next behavior, but can easily be overriden by a parent with a NotificationListener and who sets deferNavigationToParent=true

This is also nice cause it keeps the views fairly un-concerned with implementation (which means less boilerplate). The views need to know virtually nothing, except that they should dispatch FlowEvents if a setting on them is enabled, otherwise fallback to standard behavior.

Just some random thoughts in the spirit of discussion :D

2

u/yarolegovich Oct 06 '20

Yeah, event streams are always a solid alternative to old-fashioned listeners.
though I'd stay away from providing any default behavior because I like to see the complete picture of what's going on in all my flow controllers.

One strong point I can think of in favor of listeners is that the compiler won't let you forget to handle a new event or accidentally remove the handling of an old one in all the places where a screen is displayed. Dart unfortunately doesn't have anything like Kotlin sealed class that'd provide the same benefit with event streams.

1

u/[deleted] Oct 06 '20

non-trivial initial setup is required and there'll be some boilerplate all along the way

Sounds just like the official 2.0 or whatever routing that was unveiled a few days ago. I've spent the last few days boiling plates and just gave up this morning :)

Returning the 1.x named router examples for now, I gave up trying to figure out a simple interface class to speak to all of the major parts - the route delegate, the generic class you supply to it, and the route info stuff. It seemed like I was always missing info I needed for 1 or the other and it needs to just be hard coded in each of the pieces :/