cassowary 0.2.0

  • Installing
  • Versions
  • 25

Cassowary in Dart

Build Status Coverage Status

This is an implementation of the Cassowary constraint solving algorithm in Dart. The initial implementation was based on the Kiwi toolkit written in C++. Implements a subset of the functionality described in the Cassowary paper and makes specific affordances for the needs of Flutter

The Solver

A solver is the object that accepts constraints and updates member variables in an attempt to satisfy the same.


In order to create constraints, the user needs to take specific parameter objects vended by elements in the view hierarchy and create expression from these. Constraints can then be obtained from these expressions. If the solver needs to update these parameters to satisfy constraints, it will call callbacks on these parameters.

Constructing Constraints

A constraint is a linear equation of the form ax + by + cz + ... + k == 0. Constraints need not be equality relationships. Less/greater-than-or-equal-to (<= or >=) relationships can also be specified. In addition, each constraint is specified at a given priority to help resolve constraint ambiguities. A system can also be overconstrained.

The constraint as a whole is represented by an instance of the Constraint object. This in turn references an instance of an Expression (ax + by + cz + ... k), a realtionship and the finally the priority.

Each expression in turn is made up of a list of Terms and a constant. The term ax has the coefficient a and Variable x. The Param that is vended to the user is nothing but a wrapper for this variable and deals with detecting changes to it and updating the underlying view in the hierarchy.

Once the user obtains specific parameter objects, it is straightforward to create constraints. The following example sets up constraints that specify that the width of the element must be at least 100 units. It is assumed that the left and right Param objects have been obtained from the view in question.

Constraint widthAtLeast100 = right - left >= CM(100.0)

Lets go over this one step at a time: The expression right - left creates an instance of an Expression object. The expression consists of two terms. The right and left params wrap variables. The coefficients are 1.0 and -1.0 respectively and the constant -100.0. Constants need to be decorated with CM to aid with the operator overloading mechanism in Dart.

All variables are unrestricted. So there is nothing preventing the solver from making the left and right edges negative. We can specify our preference against this by specifying another constraint like so:

Constraint edgesPositive = (left >= CM(0.0))

When we construct these constraints for the solver, they are created at the default Priority.required. This means that the solver will resist adding constraints where there are ambiguities between two required constraints. To specify a weaker priority, you can use the priority setter or use the | symbol with the priority while constructing the constraint. Like so:

Constraint edgesPositive = (left >= CM(0.0) | Priority.weak)

Once the set of constraints are constructed, they are added to the solver and the results of the solution flushed out.

solver.addConstraints([widthAtLeast100, edgesPositive])

Edit Constraints

When updates need to be applied to parameters that are a part of the solver, edit variables may be used. To illustrate this, we try to express the following case in terms of constraints and their update: On mouse down, we want to update the midpoint of our view and have the left and right parameters automatically updated (subject to the constraints already setup).

We create a parameter that we will use to represent the mouse coordinate.

Param mid = new Param(coordinate);

Then, we add a constraint that expresses the midpoint in terms of the parameters we already have.

solver.addConstraint(left + right == mid * CM(2.0));

Then, we specify that we intend to edit the midpoint. As we get updates, we tell the solver to satisfy all other constraints (admittedly our example is trivial).

solver.addEditVariable(mid, Priority.strong);

and finally




  • Minor API updates to address code review concerns


  • Add bulk remove options


  • Bulk update options on solver


  • == operator override on expression returns a constraint


  • Parameters contexts are non final


  • Parameters have contexts associated with them


  • When updating parameters, the set of updates is returned


  • Initial version

Use this package as a library

1. Depend on it

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

  cassowary: "^0.2.0"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:

      import 'package:cassowary/cassowary.dart';
Version Uploaded Documentation Archive
0.2.0 Jan 26, 2017 Go to the documentation of cassowary 0.2.0 Download cassowary 0.2.0 archive
0.1.7 Jul 8, 2015 Go to the documentation of cassowary 0.1.7 Download cassowary 0.1.7 archive
0.1.6 Jul 8, 2015 Go to the documentation of cassowary 0.1.6 Download cassowary 0.1.6 archive
0.1.5 Jul 8, 2015 Go to the documentation of cassowary 0.1.5 Download cassowary 0.1.5 archive
0.1.4 Jul 7, 2015 Go to the documentation of cassowary 0.1.4 Download cassowary 0.1.4 archive
0.1.3 Jun 30, 2015 Go to the documentation of cassowary 0.1.3 Download cassowary 0.1.3 archive
0.1.2 Jun 30, 2015 Go to the documentation of cassowary 0.1.2 Download cassowary 0.1.2 archive
0.1.1 Jun 29, 2015 Go to the documentation of cassowary 0.1.1 Download cassowary 0.1.1 archive
0.1.0 Jun 29, 2015 Go to the documentation of cassowary 0.1.0 Download cassowary 0.1.0 archive


We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3


Describes how popular the package is relative to other packages. [more]
2 / 100
Code health derived from static analysis. [more]
45 / 100
Reflects how tidy and up-to-date the package is. [more]
52 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:cassowary/cassowary.dart.


  • Homepage does not exists.

    We were unable to access at the time of the analysis.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 8 errors 10 hints.

    Strong-mode analysis of lib/src/solver.dart failed with the following error:

    line: 157 col: 33
    The function expression type '(Constraint) → Result' isn't of type '(dynamic) → Result'. This means its parameter or return type does not match what is expected. Consider changing parameter type(s) or the returned type(s).

    Run dartfmt to format lib/src/constraint.dart.

    Similar analysis of the following files failed:

    • lib/src/expression.dart (hint)
    • lib/src/param.dart (hint)
    • lib/src/parser_exception.dart (hint)
    • lib/src/result.dart (hint)
  • Package is getting outdated.

    The package was released 72 weeks ago.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use cassowary.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.21.0 <2.0.0
Dev dependencies
dart_coveralls ^0.4.0
test ^0.12.18