condition 0.0.7

package: condition #

A package that contains a set of Widgets to remove ugly if, if else and switch statements from your layout code and keep it more consistent.

Usage #

The Conditioned Widget is just like any other and can be used in your Widget tree. Conditioned will have one child. What that child will be is up to you. You will provide the cases and Conditioned will figure out which builder to call.

Note: You will always provide a builder method that results in a Widget. Providing builders instead of Widgets are better for performance and allows us to use null objects.

Basics #

When you want to add a specific child based on a condition you can avoid the if, else if and else drama by using the Conditioned Widget. Conditioned will use the builder of the first Case whos expression validates as true.

Without Conditioned

We cannot use if .. else if .. else directly in our layout code. We have to propagate it to a function (not recommended) or to a new Widget.

Widget build(BuildContext context){
  return Container(
    child: _getIcon(),
  );
}

Widget _getIcon() {
  if (myNumber < 25)
    return Icon(Icons.ac_unit);
  else if (myNumber < 50)
    return Icon(Icons.home);
  else if (myNumber < 75)
    return Icon(Icons.wb_cloudy);
  else
    return Icon(Icons.wb_sunny);
}

With Conditioned

With the use of Conditioned you don't have to break up the tree at the point of the if .. else if .. else. You can break up the tree where it makes sense, which is often right after the condition.

Widget build(BuildContext context) {
  return Container(
    child: Condition(
      cases: [
        Case(myNumber < 25, builder: () => Icon(Icons.ac_unit)),
        Case(myNumber < 50, builder: () => Icon(Icons.home)),
        Case(myNumber < 75, builder: () => Icon(Icons.wb_cloudy)),
      ],
      defaultBuilder: () => Icon(Icons.wb_sunny),
    ),
  );
}

Simple boolean checks #

For simple true/false checks you can use the Conditioned.boolean constructor. It will not reduce your lines of code, it does however make it more consistent with the rest of your layout code.

Without Conditioned.boolean

Widget build(BuildContext context) {
    return Container(
        child: myNumber > 50
          ? Text(
              'The condition is true!',
              style: TextStyle(color: Colors.green[200]),
            )
          : Text(
              'The condition is false!',
              style: TextStyle(color: Colors.green[800]),
        ),
    );
}

With Conditioned.boolean

Widget build(BuildContext context) {
    return Container(
        child: Conditioned.boolean( myNumber > 50,
            trueBuilder: () => Text(
              'The condition is true!',
              style: TextStyle(color: Colors.orange[200]),
            ),
            falseBuilder: () => Text(
              'The condition is false!',
              style: TextStyle(color: Colors.orange[800]),
            ),
        ),
    );
}

Switch cases #

If instead of checking for boolean conditions but want to check by equality where you would normally want to use a switch, you can use the Conditioned.equality<T> static method.

Let's say we need to build a screen based on the following information:

enum State {empty, loading, full, error}
final state = State.idle;

Without Conditioned.equality<T>

We cannot use a switch directly in our layout code. We have to propagate it to a function (not recommended) or to a new Widget.

Widget build(BuildContext context) {
  return Container(
    child: _getStateText(),
  );
}

Widget _getStateText() {
  switch(state) {
    case State.loading:
      return Text('The device is loading');
      break;
    case State.full:
      return Text('The device is fully loaded');
      break;
    case State.error:
      return Text('An error occured');
      break;
    default:
      return Text('The device is operating as expected');
      break;
    }
}

With Conditioned.equality<T>

With the use of Conditioned.equality<T> you don't have to break up the tree at the point of the switch or introduce a Builder Widget. You can break up the tree where it makes more sense, right after the switch for example.

Widget build(BuildContext context) {
  return Container(
    child: Conditioned.equality<State>( state,
      values: [
        Value(State.loading, builder: () => Text('The device is loading')),
        Value(State.full, builder: () => Text('The device is fully loaded')),
        Value(State.error, builder: () => Text('An error occured')),
      ],
      defaultBuilder: () => Text('The device is operating as expected')
    ),
  );
}

0.0.7 - 08/14/2019 #

  • Completely overhauled the API so it's now 1 Widget named Conditioned.
  • No more passing of Widgets. Instead uses Widget builders.

0.0.6 - 08/11/2019 #

  • Renamed a lot.
  • Updated README.md.
  • Updated the example.
  • Add some doc to the code.

0.0.5 - 08/10/2019 #

  • The Widget for simple true/false checks has been renamed from Condition to SimpleCondition.
  • The Widget used for more complex use-cases has been renamed from MatchCondition to Condition.
  • A new Widget (SwitchCondition) has been added for use in cases where you would normally use a switch.
  • The README.md has been changed to reflect the changes and additions.

0.0.2 - 08/09/2019 #

  • Fix a typo in pubspec.yaml (homepage url).

0.0.1 - 08/09/2019 #

  • Add the Condition Widget for simple true/false condition checks.
  • Add the MatchCondition Widget for more complex condition checks.

example/lib/main.dart

import 'package:condition/condition.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final _myNumberNotifier = ValueNotifier<double>(0.0);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Condition Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Condition Demo'),
        ),
        body: Column(
          children: <Widget>[
            ValueListenableBuilder<double>(
              valueListenable: _myNumberNotifier,
              builder: (context, myNumber, child) => Column(
                children: <Widget>[
                  Builder(
                    builder: (context) {
                      if (myNumber < 25)
                        return Icon(Icons.ac_unit);
                      else if (myNumber < 50)
                        return Icon(Icons.home);
                      else if (myNumber < 75)
                        return Icon(Icons.wb_cloudy);
                      else
                        return Icon(Icons.wb_sunny);
                    },
                  ),
                  Conditioned(
                    cases: [
                      Case(myNumber < 25, builder: () => Icon(Icons.ac_unit)),
                      Case(myNumber < 50, builder: () => Icon(Icons.home)),
                      Case(myNumber < 75, builder: () => Icon(Icons.wb_cloudy)),
                    ],
                    defaultBuilder: () => Icon(Icons.wb_sunny),
                  ),
                  myNumber > 50
                      ? Icon(Icons.airplanemode_active)
                      : Icon(Icons.directions_car),
                  Conditioned.boolean(
                    myNumber > 50,
                    trueBuilder: () => Icon(Icons.airplanemode_active),
                    falseBuilder: () => Icon(Icons.directions_car),
                  ),
                  Builder(
                    builder: (context) {
                      switch (myNumber.round()) {
                        case 100:
                          return Icon(Icons.wb_sunny);
                        case 0:
                          return Icon(Icons.add_a_photo);
                        default:
                          return Icon(Icons.ac_unit);
                      }
                    },
                  ),
                  Conditioned.equality<int>(
                    myNumber.round(),
                    values: [
                      Value(100, builder: () => Icon(Icons.wb_sunny)),
                      Value(0, builder: () => Icon(Icons.add_a_photo))
                    ],
                    defaultBuilder: () => Icon(Icons.ac_unit),
                  ),
                  Text(myNumber.toStringAsFixed(1)),
                  Slider(
                    value: myNumber,
                    min: 0,
                    max: 100,
                    onChanged: (value) => _myNumberNotifier.value = value,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  condition: ^0.0.7

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:condition/condition.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
4
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Aug 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/src/case.dart.

Run flutter format to format lib/src/case.dart.

Format lib/src/conditioned.dart.

Run flutter format to format lib/src/conditioned.dart.

Format lib/src/value.dart.

Run flutter format to format lib/src/value.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test