Skip to content

Instantly share code, notes, and snippets.

@AdamJonsson
Created April 30, 2019 06:32
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save AdamJonsson/57caa39ec3584343833ece556198187c to your computer and use it in GitHub Desktop.
Save AdamJonsson/57caa39ec3584343833ece556198187c to your computer and use it in GitHub Desktop.
An example how different widget can be used to expand and collapse content in Flutter
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Expanding Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ExpandExample(),
);
}
}
class ExpandExample extends StatefulWidget {
@override
_ExpandExampleState createState() => _ExpandExampleState();
}
class _ExpandExampleState extends State<ExpandExample> {
///If the box is expanded
bool _isExpanded = false;
///Toogle the box to expand or collapse
void _toogleExpand() {
setState(() {
_isExpanded = !_isExpanded;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: 200.0,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Material(
color: Colors.blue,
child: InkWell(
onTap: _toogleExpand,
child: Container(
padding: EdgeInsets.all(25.0),
width: double.infinity,
child: Text('Click me to ' + (_isExpanded ? 'collapse' : 'expand')),
),
),
),
ExpandedSection(
expand: _isExpanded,
child: Container(
width: double.infinity,
color: Colors.red,
padding: EdgeInsets.all(25.0),
child: Text('Hello there'),
),
)
],
),
),
),
);
}
}
class ExpandedSection extends StatefulWidget {
final Widget child;
final bool expand;
ExpandedSection({this.expand = false, this.child});
@override
_ExpandedSectionState createState() => _ExpandedSectionState();
}
class _ExpandedSectionState extends State<ExpandedSection> with SingleTickerProviderStateMixin {
AnimationController expandController;
Animation<double> animation;
@override
void initState() {
super.initState();
prepareAnimations();
}
///Setting up the animation
void prepareAnimations() {
expandController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 500)
);
Animation curve = CurvedAnimation(
parent: expandController,
curve: Curves.fastOutSlowIn,
);
animation = Tween(begin: 0.0, end: 1.0).animate(curve)
..addListener(() {
setState(() {
});
}
);
}
@override
void didUpdateWidget(ExpandedSection oldWidget) {
super.didUpdateWidget(oldWidget);
if(widget.expand) {
expandController.forward();
}
else {
expandController.reverse();
}
}
@override
void dispose() {
expandController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SizeTransition(
axisAlignment: 1.0,
sizeFactor: animation,
child: widget.child
);
}
}
@BurierAbdalwahhabElia
Copy link

thanks man,
i have a list of card..
where I am tap on a card, all cards expand a child , i want to expand only child of card i tapped on it.

@AdamJonsson
Copy link
Author

@BurierAbdalwahhabElia Hmm, seems that you are sharing the same "open" state for all cards. Maybe you need a state/widget for every individual card?

@BurierAbdalwahhabElia
Copy link

BurierAbdalwahhabElia commented Sep 23, 2020 via email

@iamir4g
Copy link

iamir4g commented Dec 23, 2020

@BurierAbdalwahhabElia
hi can you use this code?

@irfanshidqi
Copy link

irfanshidqi commented Jan 20, 2021

your work is awesome bro thankyouu.
i need help . i want to keep the widget Expanded at First Launch . how to do that ?
i modify state _isExpanded = true . but it gets me nothing . still collapsed
thanks for your help

@wlvoon93
Copy link

wlvoon93 commented Feb 4, 2021

perfect ! this code is so good. thanks bro

@amanokerim
Copy link

Flutter 2.* version:

import 'package:flutter/material.dart';

class ExpandedSection extends StatefulWidget {
  final Widget child;
  final bool expand;
  ExpandedSection({this.expand = false, required this.child});

  @override
  _ExpandedSectionState createState() => _ExpandedSectionState();
}

class _ExpandedSectionState extends State<ExpandedSection>
    with SingleTickerProviderStateMixin {
  late AnimationController expandController;
  late Animation<double> animation;

  @override
  void initState() {
    super.initState();
    prepareAnimations();
  }

  ///Setting up the animation
  void prepareAnimations() {
    expandController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    Animation<double> curve = CurvedAnimation(
      parent: expandController,
      curve: Curves.fastOutSlowIn,
    );
    animation = Tween(begin: 0.0, end: 1.0).animate(curve)
      ..addListener(() {
        setState(() {});
      });
  }

  @override
  void didUpdateWidget(ExpandedSection oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.expand) {
      expandController.forward();
    } else {
      expandController.reverse();
    }
  }

  @override
  void dispose() {
    expandController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizeTransition(
        axisAlignment: 1.0, sizeFactor: animation, child: widget.child);
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment