How to Use Segmented Buttons in Flutter with Back4app

10min

Introduction

Segmented Buttons are a UI component introduced in Material 3 that allows users to select from two to five options. They are particularly useful when you want to offer a set of exclusive or non-exclusive choices in a clean, organized manner. In this tutorial, we will explore how to use Segmented Buttons in a Flutter app, leveraging Back4app as a backend to store and manage the selected options.

Prerequisites

Before starting, ensure you have the following:

  • A Back4app account. Sign up for a free account at Back4app.com.
  • A Flutter development environment set up on your local machine. Follow the Flutter installation guide if you haven't set it up yet.
  • Basic knowledge of Dart, Flutter widgets, and using Back4app for backend services.

Step 1 – Setting Up Your Back4app Backend

  1. Create a Back4app Project: Log in to your Back4app account and create a new project.
  2. Create Parse Classes: For this tutorial, create a Parse Class named UserPreferences to store the selected options from the Segmented Buttons:
    • username (String): The name of the user.
    • selectedOption (String): The option selected by the user.
  3. Get Your Back4app Credentials: Navigate to your project settings to retrieve your Application ID and Client Key, which you’ll need to connect your Flutter app to Back4app.

Step 2 – Setting Up the Flutter Project

  1. Create a New Flutter Project: Open your terminal or command prompt and run:
Bash

  1. Add Dependencies: Open pubspec.yaml and add the following dependencies:
YAML


Run flutter pub get to install these dependencies.

  1. Initialize Parse in Your App: In lib/main.dart, initialize the Parse SDK:
Dart


Replace 'YOUR_BACK4APP_APP_ID' and 'YOUR_BACK4APP_CLIENT_KEY' with your actual Back4app credentials.

Step 3 – Implementing Segmented Buttons

  1. Create the PreferenceScreen Widget: In lib/main.dart, add a new widget to display and handle the Segmented Buttons:
Dart


This code defines a simple UI with a Segmented Button allowing users to choose from three options. The selected option is stored in Back4app whenever the selection changes.

Step 4 – Running the App

  1. Run your app using flutter run. You should see the Segmented Buttons displayed on the screen. Selecting an option will update the state and store the selection in Back4app.
  2. Verify the data in Back4app by logging into your Back4app dashboard and checking the UserPreferences class. You should see entries corresponding to your selections from the Flutter app.

Conclusion

In this tutorial, we explored how to implement and use Segmented Buttons in Flutter. We also demonstrated how to integrate Flutter with Back4app to store user preferences. Segmented Buttons offer a clean and intuitive way to present users with multiple options, and when combined with a powerful backend like Back4app, they can significantly enhance your app's user experience.

For more information on Flutter widgets, check out the Flutter documentation, and for backend integration tips, visit the Back4app documentation. Happy coding!