mercredi 30 octobre 2019

What is a good Dart/Flutter pattern for creating multi widget object library?

Since Dart/Flutter uses a composition style approach to building widgets (coupling that with how scoped model operates) what is a good pattern for developing a library of objects that encapsulate widgets?

Assume you have an application that allows you to display a list of objects within a list view that each have their own way displaying their information, but all have the same approach for doing so.

Consider the following pseudo-code, which takes the approach of encapsulating all of the required widgets and other such properties/methods needed for each object to follow.

    abstract class Vehicle {
        int type;
        String name;
        Widget listWidget
        Widget editWidget
        Widget settingsWidget
    }

    class Truck extends Vehicle {
        type = 1
        name = "Truck"
        listWidget = ... My truck widget for within lists...
        editWidget = ... My truck edit widget ...
        settingsWidget = ... My truck settings display ...
    }

    class Car extends Vehicle {
        type = 1
        name = "Car"
        listWidget = ... My car widget for within lists...
        editWidget = ... My car edit widget ...
        settingsWidget = ... My car settings display ...
    }

With that approach, then I would have to create each vehicle type and follow the abstract api pattern and not worry about how it is rendered.

When it comes to displaying the vehicles within a list (ListView for example), this approach would allows the object itself to dictate how it is rendered in that list, and it could be unique to each type of vehicle.

For example:

    Container(
        child: new ListView.builder(
            itemCount: items.length,
            itemBuilder: (BuildContext ctxt, int index) {
                return items[index].listWidget;
            }
        )
    )

Is this a valid Dart/Flutter way of accomplishing this or is there a better pattern that should be followed?

Am I violating Dart recommended coding patterns in that each widget would control it's own display?

The idea behind this is that if a settings panel needed to be formatted differently depending on the type of vehicle, I could do it, rather than trying to figure out how to create a common display pattern for all widgets.

Thoughts?

Aucun commentaire:

Enregistrer un commentaire