Flutter scrollable horizontal avatar list

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In this article, you'll learn how to create a scrollable horizontal list, in our case filled with avatars.
However, feel free to put whatever you want on this list. The result for today will look like this:

For this article, we'll be starting from our basic Flutter application, which you can download on GitHub as well.
Let's first start by rendering avatars in Flutter, as this will be the basis of our application.
Usually, we would be able to use a CircleAvatar widget in Flutter. I want to include a border around the widget for my version, so I'm using a container to wrap everything.
return Center(
child: Container(
width: 120.0,
height: 120.0,
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black,
),
child: CircleAvatar(
backgroundImage: NetworkImage('{YOUR_IMAGE}'),
),
),
);
And with this, we get a circle image with a black border around it.

The last thing I'll do here is extracting this to be its very own widget so we can easily re-use it. I use Visual Studio to extract this into its own widget resulting in the following code:
class SampleAvatar extends StatelessWidget {
const SampleAvatar({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
width: 120.0,
height: 120.0,
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black,
),
child: CircleAvatar(
backgroundImage: NetworkImage(
'https://media-exp1.licdn.com/dms/image/C4D03AQFgZBilNtPUMA/profile-displayphoto-shrink_800_800/0/1604728137407?e=1632960000&v=beta&t=QKa1Nq3WKWQGEGaiKdZ1ovp1h6uAbwPZfihdqY2_pNU'),
),
);
}
}
Making it easy for us to use like so:
SampleAvatar()
Now that we have our sample avatar widget, let's go ahead and see how we can make a horizontally scrollable list including them.
return Container(
margin: EdgeInsets.all(10.0),
height: 140.0,
child: ListView.separated(
itemCount: 10,
separatorBuilder: (BuildContext context, int index) {
return SizedBox(
width: 10,
);
},
itemBuilder: (_, i) => SampleAvatar(),
scrollDirection: Axis.horizontal,
),
);
This code creates a container so we can add some margin and height to our scrollable list.
Inside, we render the ListView. I choose to use the separated one since we want some space between each item.
Then inside, we render each of our sample avatars.
And the most important part, we define the scrollDirection as Axis.horizontal making it scroll horizontally.
And that's all we need to create a horizontally scrollable list in Flutter.
If you are looking for the complete code for either our avatar widget or the full list view, you can find it on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter