expandable 2.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new82

Expandable #

A Flutter widget that can be expanded or collapsed by the user.

Introduction #

This library helps implement expandable behavior as prescribed by Material Design:

animated image

Expandable should not be confused with ExpansionPanel. ExpansionPanel, which is a part of Flutter material library, is designed to work only within ExpansionPanelList and cannot be used for making other widgets, for example, expandable Card widgets.

Usage #

The easiest way to make a user-expandable widget is to use ExpandablePanel:

/// Shows a header along with an indicator icon, and expandable text below it: 
class Widget1 extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ExpandablePanel(
      header: Text("Lorem ipsum",
        style: Theme.of(context).textTheme.body2,
      ),
      expanded: Text(loremIpsum, softWrap: true, ),
      tapHeaderToExpand: true,
      hasIcon: true,
    );
  }
}

You can also implement complex expandable widgets by controlling the state of Expandable widgets using ExpandableController. The controller is provided to Expandable widgets by the means of ExpandableNotifier. The following example demonstrates this usage pattern:

/// This widget has three sections: title, pictures, and description, which all expand
/// then the user clicks on the EXPAND button at the bottom.
class Widget2 extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ExpandableNotifier(
      controller: ExpandableController(false),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Expandable(
            collapsed: buildCollapsedTitle(),
            expanded: buildExpandedTitle(),
          ),
          Expandable(
            collapsed: buildCollapsedPictures(),
            expanded: buildExpandedPictures(),
          ),
          Expandable(
            collapsed: buildCollapsedDescription(),
            expanded: buildExpandedDescription(),
          ),
          Divider(height: 0.0,),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Builder(
                builder: (context) {
                  var exp = ExpandableController.of(context);
                  return MaterialButton(
                    child: Text(exp.expanded ? "COLLAPSE": "EXPAND",
                      style: Theme.of(context).textTheme.button.copyWith(
                        color: Colors.deepPurple
                      ),
                    ),
                    onPressed: () {
                      exp.toggle();
                    },
                  );
                }
              ),
            ],
          ),
        ],
      ),
    );
  }
}

Migration from version 1.x #

When Expandable was initially created, it was using Scoped Model plugin for communicating state changes. In version 2.0.0 the dependency on ScopedModel was eliminated.

This is a breaking change, so a code change is necessary to make the code work. The migration is mainly a search/replace:

  • ScopedModel<ExpandableModel> -> ExpandableNotifier
  • ExpandableModel -> ExpandableController
  • ScopedModel.of<ExpandableModel> -> ExpandableController.of

[2.0.0] - 2/14/2019

  • Eliminated the dependency on ScopedModel
  • Introduced ExpandableNotifier and ExpandableController
  • This is a breaking change. See README.md for details on migration from 1.x to 2.0

[1.1.0] - 02/01/2019

  • Added ExpandablePanel, a configurable expandable widget with optional header and icon

[1.0.0] - 01/29/2019

  • Initial release

example/example.dart

import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Expandable Demo',
      theme: ThemeData(
        primarySwatch: Colors.orange,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

  @override
  State createState() {
    return MyHomePageState();
  }
}

class MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Expandable Demo"),
      ),
      body: ListView(
        children: <Widget>[
          Card1(),
          Card2(),
        ].map((w) {
          return Padding(
            padding: EdgeInsets.only(top: 15, left: 15, right: 15,),
            child: w,
          );
        }).toList(),
      ),
    );
  }
}

const loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

class Card1 extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Card(
      clipBehavior: Clip.antiAlias,
      child: ExpandableNotifier(
        child: Column(
          children: <Widget>[
              SizedBox(
              height: 150.0,
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.grey,
                  shape: BoxShape.rectangle,
                  image: DecorationImage(image: AssetImage('images/IMG_4164.JPG'), fit: BoxFit.cover),
                ),
              ),
            ),
            ExpandablePanel(
              tapHeaderToExpand: true,
              header: Padding(
                padding: EdgeInsets.only(top: 10.0, left: 10),
                child: Text("Lorem ipsum",
                  style: Theme.of(context).textTheme.body2,
                )
              ),
              collapsed: Text(loremIpsum, softWrap: false, overflow: TextOverflow.ellipsis,),
              expanded: Text(loremIpsum, softWrap: true, overflow: TextOverflow.fade,),
              builder: (_, collapsed, expanded) {
                return Padding(
                  padding: EdgeInsets.only(left: 10.0, right: 10.0, bottom: 10.0),
                  child: Expandable(
                    collapsed: collapsed,
                    expanded: expanded,
                  ),
                );
              },
            ),
          ],
        ),
      )
    );
  }
}

class Card2 extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    buildImg(String path, double height) {
      return SizedBox(
        height: height,
        child: Container(
          decoration: BoxDecoration(
            color: Colors.grey,
            shape: BoxShape.rectangle,
            image: DecorationImage(image: AssetImage(path), fit: BoxFit.cover),
          ),
        )
      );
    }

    buildCollapsed1() {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(10.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text("Lorem Ipsum",
                  style: Theme.of(context).textTheme.body1,
                ),
              ],
            ),
          ),
        ]
      );
    }

    buildCollapsed2() {
      return buildImg('images/IMG_3897.JPG', 150.0);
    }

    buildCollapsed3() {
      return Container();
    }

    buildExpanded1() {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(10.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text("Lorem Ipsum",
                  style: Theme.of(context).textTheme.body1,
                ),
                Text("Lorem Ipsum",
                  style: Theme.of(context).textTheme.caption,
                ),
              ],
            ),
          ),
        ]
      );
    }

    buildExpanded2() {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: buildImg('images/IMG_4164.JPG', 100.0)
              ),
              Expanded(
                child: buildImg('images/IMG_2840.JPG', 100.0)
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: buildImg('images/IMG_4163.JPG', 100.0)
              ),
              Expanded(
                child: buildImg('images/IMG_4203.JPG', 100.0)
              ),
            ],
          ),
        ],
      );
    }

    buildExpanded3() {
      return Padding(
        padding: EdgeInsets.all(10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod", softWrap: true,),
          ],
        ),
      );
    }


    return Card(
      clipBehavior: Clip.antiAlias,
      child: ExpandableNotifier(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expandable(
              collapsed: buildCollapsed1(),
              expanded: buildExpanded1(),
            ),
            Expandable(
              collapsed: buildCollapsed2(),
              expanded: buildExpanded2(),
            ),
            Expandable(
              collapsed: buildCollapsed3(),
              expanded: buildExpanded3(),
            ),
            Divider(height: 0.0,),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Builder(
                  builder: (context) {
                    var exp = ExpandableController.of(context);
                    return MaterialButton(
                      child: Text(exp.expanded ? "COLLAPSE": "EXPAND",
                        style: Theme.of(context).textTheme.button.copyWith(
                          color: Colors.deepPurple
                        ),
                      ),
                      onPressed: () {
                        exp.toggle();
                      },
                    );
                  },
                ),
              ],
            ),
          ],
        ),
      )
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  expandable: ^2.0.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support 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:expandable/expandable.dart';
  
Version Uploaded Documentation Archive
2.0.0 Feb 14, 2019 Go to the documentation of expandable 2.0.0 Download expandable 2.0.0 archive
1.1.0+1 Feb 1, 2019 Go to the documentation of expandable 1.1.0+1 Download expandable 1.1.0+1 archive
1.0.0+1 Jan 29, 2019 Go to the documentation of expandable 1.0.0+1 Download expandable 1.0.0+1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
63
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

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

  • Dart: 2.1.0
  • pana: 0.12.13+1
  • Flutter: 1.2.1

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/expandable.dart.

Run flutter format to format lib/expandable.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
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