Custom Content Type
Real world apps will need a variety of Content Types and its only natural that a framework like Vyuh support such a scenario. Extensibility is one of the primary qualities of this framework and in this guide we will see how to introduce a new Custom Content Type. We will add the schema on the CMS and build its implementation on Flutter.
A quick summary of all the steps are given below, with the details in the following sections:
CMS Side
- Create a schema for the Content type
- Register it with the FeatureDescriptor
- Add a few instances of the new Content type for testing
Dart/Flutter Side
- Create the Dart equivalent of the content type with a custom ContentItem type
- Create its ContentBuilder that has a default LayoutConfiguration
- Register it with the FeatureDescriptor on the Dart side
- See the cards rendered live on the screen
1. A Product Card content type
Cards have become one of the most prominent types of visual metaphors for real world objects. You see them everywhere in apps, in a variety of industry verticals. We will continue with that tradition and build one for ourselves as a custom card.
Let’s create a Product Card that stands for the products you would present to your customers. These could be products from the Fashion, Sports, Finance, Healthcare or any other industry. For our use case, let’s use the Fashion domain.
Defining the Schema
The first step is to define a schema that represents a product. A typical
product would consist of the following properties: title
, description
,
price
, category
, skuId
and of course an image
. The following schema
captures all of this in a Sanity schema. The use of the preview field
helps in visualizing the card inside the CMS and gives the information in a
quick glance.
Exporting it in the FeatureDescriptor
We can now export this schema for usage inside the CMS. We do this with the
FeatureDescriptor
. Notice that we are exporting it as a custom content within
the contentSchemaBuilders
property and also adding to the regionItems
of the
RouteDescriptor
. This ensures we can add our product card to a region in the
page.
The ContentSchemaBuilder
for this productCard is a simple one and does not
have any custom configurations or layouts that needs to be assembled. Hence, we
go ahead with the standard BuiltContentSchemeBuilder
.
Creating a Product Card on the CMS
Now that we have the schema defined, let’s add some products to our page, in the CMS. Since the Product Card has already been added to the Route’s region-items, we can see it in the list of content items for the Region.
After adding some of the details of the Product, we have the card, ready to be displayed on the App.
2. Rendering the Product Card
Having an item defined on the CMS is only half the story. To render it on Flutter, we do need to create its Dart equivalent. If we fail to do so, we will be greeted with a message like below, indicating the missing piece of the Content definition.
Such a message is quite useful during development as it quickly helps in identifying the source of error. We are already aware of this missing piece, so let’s fix it now.
Building the Dart equivalent of the Schema
The Dart version of the Product schema is the type-safe version of the JSON we get from the CMS. We can represent it like below.
Notice the static fields: schemaName
, typeDescriptor
and the
contentBuilder
. These are by convention a way of keeping the meta details
about the content item together. The typeDescriptor
helps in defining the way
to hydrate a Dart version of the Product from its JSON equivalent. Similarly the
contentBuilder
helps in defining the creating the visual representation of the
Product. You will see these being used in the FeatureDescriptor
, where we
register the Product content type.
Rendering with a LayoutConfiguration
To render any ContentItem
, we need a LayoutConfiguration
that tells the
framework how to represent it in Flutter. A ContentItem
can have multiple
LayoutConfiguration
definitions and any feature can add more layouts for the
content type. This is one of the extensibility points of the framework.
In our case, we define a DefaultProductLayout
that renders the Product with a
simple Flutter Card
Widget. The code below shows this.
Registering the Product Content type
Just like we did on the CMS side, we also need to register this content type on
Flutter. We do this with the FeatureDescriptor
in the feature.dart file
(Line #16).
3. Seeing the Products in action
With the schema on the CMS and its equivalent ContentBuilder
created on
Flutter, we can now see it rendered in our App. It uses the default layout which
we created earlier to render the product as a simple Card
widget.
For the two products which we added on the page, the corresponding cards can be seen in the app below.
Summary
In this article we described the process of adding a new Content type within our
App. We started with the schema for the ProductCard Content type and
registered it with the FeatureDescriptor
to be visible inside the CMS.
On the Dart/Flutter side we did an equivalent registration where we created the
ProductCard
ContentItem
type that knows how to deserialize the JSON schema.
Using the combination of a ContentBuilder
for the Product type with a
DefaultProductCardLayout
we could render the ProductCard on the screen.
In this guide we created a single Default Layout. However, it is possible to
have several different layouts for the ProductCard
. We will cover creating
additional custom layouts in the guide on
Custom Layout.