How to combine enum and switch statements to reuse an entire screen widget in flutter- #100DaysOfCode - Day 16
Tags: 100daysofcode, flutter, dart
Published: 2021-07-13 23:59:40

## Introduction

This post is part of my 100DaysOfCode series. In this series, I write about my 100DaysOfCode journey. My aim for this challenge is become proficient in flutter and firebase by building an Agriculture Management Information System.

## Recap

On Day 15 we discussed how to filter documents in firebase before making it a stream.

## Overview

In this post, we discuss how to reuse our all farmer screen widget. This is required to personalize our app for the app users while promoting code reusability.

### Location Filter Enum

The first step is to create a Location Filter enum. `enum LocationFilter { all, district, region }`.  This enum value will be passed to the `FarmerListScreen()` widget to modify the screen into an all farmer list screen, district farmer list screen or region farmer list screen.

```Dart
class FarmerListScreen extends StatefulWidget {
  static String routeName = 'AllFarmerScreen';
  final LocationFilter filter;
  const FarmerListScreen({
    this.filter: LocationFilter.all,
    Key? key,
  }) : super(key: key);

  @override
  _FarmerListScreenController createState() => _FarmerListScreenController();
}
```

### Getting Stream based on location filter enum

```Dart
class _FarmerListScreenController extends State<FarmerListScreen> {
  @override
  Widget build(BuildContext context) => _FarmerListScreenView(this);
  late Stream<QuerySnapshot<FarmerServiceModel>> stream;

  @override
  void initState() {
    super.initState();
    switch (widget.filter) {
      case LocationFilter.region:
        stream = RegionFarmerCommand(context).run();
        break;
      case LocationFilter.district:
        stream = DistrictFarmerCommand(context).run();
        break;
      default:
        stream = AllFarmerCommand(context).run();
        break;
    }
  }
```
When the widget is first created, a stream of region farmer documents, district farmer documents or all Farmer documents is returned if the location filter is of `LocationFilter.region`, `LocationFilter.district` or the default `LocationFilter.all` respectively.

The farmer command methods were discussed on Day 14.

### Screen title

```Dart
  String getScreenTitle() {
    var title = ' Farmer Register';
    switch (widget.filter) {
      case LocationFilter.district:
        return Provider.of<UserModel>(context).currentUser.district + title;

      case LocationFilter.region:
        return Provider.of<UserModel>(context).currentUser.region + title;
      default:
        return title;
    }
  }
```
The screen title will be based on the current user district or region depending on the LocationFilter type passed to the `FarmerListScreenWidget()`.

Screenshots of the different flavours of the farmer list screen are shown below.

Region Farmer List Screen
![Region List Farmer Screen]
(https://drive.google.com/uc?id=1N837BfG7-ng8h6iQ8vZVaxuifLnMdji0)


District Farmer List Screen
![District List Farmer Screen]
(https://drive.google.com/uc?id=1y_lv54g5nvaJJDaTcC4Zm8uv_WlmECKw) 


### Wrap Up

In this post, we discussed how to reuse our all farmer screen widget to personalize the screen for our app users.

## Connect with me

Thank you for reading my post. Feel free to subscribe below to join me on the #100DaysOfCodeChallenge or connect with me on [LinkedIn](https://www.linkedin.com/in/curtlycritchlow/) and [Twitter](https://twitter.com/CritchlowCurtly). You can also [buy me a book](https://www.buymeacoffee.com/curtlycritchlow) to show your support.