Flutter Flow: Carousel Menu
Tags: flutter, dart, androiddev, mobile
Published: 2023-09-27 13:00:00

I am going to discuss how to make a simple carousel menu in the Flutter Flow platform. I will provide the full source code as well.

![](https://cdn-images-1.medium.com/max/2000/1*b7SxlEwb59Vl-W3xWwIM9Q.gif)

We are going to use the following package to help us achieve this:
[**carousel_slider | Flutter Package**](https://pub.dev/packages/carousel_slider/example)

### Custom Widget

First, let’s create our custom widget. Click the custom functions tab on the left-hand side of the flutter flow platform

![](https://cdn-images-1.medium.com/max/2000/1*-7iXjsG2aowws7RN7P0fPA.png)

Now click **Add > Widget**

![](https://cdn-images-1.medium.com/max/2000/1*zLtUygMuHCWAAlAm5Qrg9A.png)

Give your new widget a name such as **CarouselMenu**

![](https://cdn-images-1.medium.com/max/2780/1*NimfULu3zjy7bv5DTRrpYQ.png)

We will need to pass JSON as a parameter to build our menu items. I will discuss this more later. Create a parameter called **jsonMenuItems** of type JSON.

![](https://cdn-images-1.medium.com/max/2000/1*w2wvDOiAoJjsHU2jtxHtkA.png)

Click the green **View Boilerplate Code** button.

![](https://cdn-images-1.medium.com/max/2000/1*ky7VGY_r89LW2oLYrO__eA.png)

It should show this screen.

![](https://cdn-images-1.medium.com/max/3220/1*qUn6oRjk-QzokwpiL_jX0Q.png)

Click **Copy to Editor**

Now click **Save** at the top right of the screen

### **Adding Carousel Slider dependency**

Under the parameters add the carousel_slider dependency.

![](https://cdn-images-1.medium.com/max/2000/1*vgdBP-W9_4IHg16DINVUpA.png)

Click **Save** again. Then click **Compile.**

### Adding JSON menu items to local state

For this example, we are going to store our menu items in Flutter Flow local state. You could also retrieve the JSON from an API.

Click on the **Local State** tab.

![](https://cdn-images-1.medium.com/max/2000/1*Ux372lwMxpr4N4jwEpuDtQ.png)

Now click **Add State Variable.**

![](https://cdn-images-1.medium.com/max/2000/1*61m6IskSGN7aNNkStoqO5Q.png)

Add a JSON field. You can name it the same as the parameter we created earlier.

![](https://cdn-images-1.medium.com/max/2000/1*om1D1e5sJea8lLHLGT8M5A.png)

### Menu Item Example JSON

You can use this example JSON array to populate your menu carousel. This is a JSON Array containing three JSON Objects. Each object is a menu item and contains a title, route, and imageUrl.
{% gist https://gist.github.com/cmcoffeedev/48cf66c91b930840df767d1abbd6968e.js %}
If you look at the Flutter Flow source code you will see they use named routes to navigate in the app using the go router dependency. The route will be each screen’s Scaffold name. You will see this later.

Copy this JSON as the default value for the state variable we just created.

![](https://cdn-images-1.medium.com/max/2000/1*OCgddpUnORlhCPL1U5KZZA.png)

Save this using Ctrl-S/CMD-S.

### Writing the custom carousel menu code

Go back to our custom widget so that we may implement the carousel menu.

First, add the import for the carousel_slider dependency.

![](https://cdn-images-1.medium.com/max/2000/1*L7hsNBsaHMQYds5Q7NEo4g.png)

Create a class called **CarouselItem** and put it under **_CarouselMenuState**. This class will hold each menu item from the JSON.

![](https://cdn-images-1.medium.com/max/2000/1*eOalLl63M2TGkD4w7I0HKA.png)

Create a list variable of CarouselItem’s.

![](https://cdn-images-1.medium.com/max/2000/1*5rp8dfT4dRJWgYb4gKZCcw.png)

Under the carousel item list, let’s override **initState**. This method get’s called once when the widget first gets created.
[**initState method - State class - widgets library - Dart API**](https://api.flutter.dev/flutter/widgets/State/initState.html)

![](https://cdn-images-1.medium.com/max/2000/1*u5n2L5uin1-lxDZLVIevAQ.png)

Under **initState**, create a method to build our carousel widgets. This method doing 3 things.

 1. Iterating through each JSON Object in the JSON array.

 2. Creating a CarouselItem object with the current object’s value.

 3. Adding this object to the carouselItems list.

![](https://cdn-images-1.medium.com/max/2324/1*wm18eDtrboXH__zzUjut1Q.png)

Let’s change our build method to return the carousel_slider.

![](https://cdn-images-1.medium.com/max/2528/1*ZyCuxlOie01QONuCe0nS3A.png)

Notice that I have **context.pushNamed(route);** commented out. This is the code FlutterFlow uses, but in the online editor, it will give you errors when compiling but will work when downloading the project.

There are some workarounds to get it to run on the Flutter Flow platform. Essentially, after you launch the app in test mode, uncomment the code, save it, and click instant reload.

Save and compile what we have so far.

### Setting up screens to run our custom widget

Next, go to the **Widget Tree** tab to create our screens.

Create four screens. One that has our custom widget and three others that the carousel items will navigate to. These are the names that you will use as the route in the custom JSON state.

![](https://cdn-images-1.medium.com/max/2000/1*iD5gnsK1cGIZUnnZOzrz5w.png)

On the page/screen you want your custom widget, go to the UI Builder and drag and drop the custom widget.

![](https://cdn-images-1.medium.com/max/2000/1*iIPc7v-Ml9bRFD9N20VaYw.png)

Here are my settings for the custom widget.

![](https://cdn-images-1.medium.com/max/2000/1*ShVKRltfQHCAQ-4p-9aGig.png)

Notice I set the JSON variable from our local state JSON variable.

![](https://cdn-images-1.medium.com/max/2000/1*jeWguE7jqhtaDevuwfS9wA.png)

Save the project and click the run in test mode button on the top right.

![](https://cdn-images-1.medium.com/max/2000/1*tp19iCRvN_p_PfQ5npEpIA.png)

To test clicking the menu items, go back to the custom code and uncomment it. Save and then compile it. Go back to Test Mode and click instant reload. This will work fine when you download the code.

Here is the full source code:

{% gist https://gist.github.com/cmcoffeedev/c164a182fd33112871995a7129a81b8c
%}