-
-
Save herveGuigoz/47fabc3a438ad269ffcf50eab6eb7f29 to your computer and use it in GitHub Desktop.
Expading cards - by Simon Lightfoot 21/12/2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2020 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'dart:math' as math; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp( | |
MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Example(), | |
), | |
); | |
} | |
@immutable | |
class Example extends StatelessWidget { | |
const Example({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.grey.shade300, | |
body: DefaultTextStyle.merge( | |
style: TextStyle( | |
color: Colors.grey.shade700, | |
), | |
child: ListView.builder( | |
padding: const EdgeInsets.symmetric(horizontal: 24.0), | |
itemCount: 30, | |
itemBuilder: (BuildContext context, int index) { | |
return ExpandingCard( | |
key: Key('item-$index'), | |
leading: SizedBox( | |
width: 56.0, | |
child: DecoratedBox( | |
decoration: BoxDecoration( | |
color: Colors.grey, | |
shape: BoxShape.circle, | |
), | |
child: AspectRatio( | |
aspectRatio: 1.0, | |
child: Center( | |
child: Text( | |
'Hello', | |
style: TextStyle(color: Colors.white), | |
), | |
), | |
), | |
), | |
), | |
title: 'Item #$index', | |
subtitle: 'Sub title $index', | |
children: [ | |
InfoPage(index: 3), | |
InfoPage(index: 1), | |
InfoPage(index: 2), | |
], | |
); | |
}, | |
), | |
), | |
); | |
} | |
} | |
@immutable | |
class ExpandingCard extends StatefulWidget { | |
const ExpandingCard({ | |
Key? key, | |
this.leading, | |
required this.title, | |
this.subtitle, | |
this.expandHeight = 156.0, | |
required this.children, | |
}) : super(key: key); | |
final Widget? leading; | |
final String title; | |
final String? subtitle; | |
final double expandHeight; | |
final List<Widget> children; | |
@override | |
_ExpandingCardState createState() => _ExpandingCardState(); | |
} | |
class _ExpandingCardState extends State<ExpandingCard> with AutomaticKeepAliveClientMixin { | |
late final PageController _pageController; | |
bool _expanded = false; | |
@override | |
bool get wantKeepAlive => _expanded; | |
@override | |
void initState() { | |
super.initState(); | |
_pageController = PageController(); | |
} | |
@override | |
void dispose() { | |
_pageController.dispose(); | |
super.dispose(); | |
} | |
void _onTap() { | |
setState(() => _expanded = !_expanded); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final style = DefaultTextStyle.of(context).style; | |
final color = style.color ?? Colors.grey; | |
super.build(context); // AutomaticKeepAliveClientMixin | |
return Card( | |
child: DefaultTextStyle.merge( | |
style: style, | |
child: IconTheme.merge( | |
data: IconThemeData( | |
color: color, | |
), | |
child: InkWell( | |
onTap: _onTap, | |
child: Stack( | |
alignment: Alignment.bottomCenter, | |
children: [ | |
Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Row( | |
children: [ | |
if (widget.leading != null) widget.leading!, | |
const SizedBox(width: 8.0), | |
Column( | |
mainAxisSize: MainAxisSize.min, | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Text( | |
widget.title, | |
style: TextStyle( | |
fontSize: 18.0, | |
), | |
), | |
if (widget.subtitle != null) | |
Text( | |
widget.subtitle!, | |
style: TextStyle( | |
fontSize: 16.0, | |
), | |
), | |
], | |
), | |
], | |
), | |
), | |
AnimatedContainer( | |
duration: const Duration(milliseconds: 300), | |
curve: Curves.easeInOut, | |
height: _expanded ? widget.expandHeight : 0.0, | |
child: ShaderMask( | |
shaderCallback: (Rect bounds) { | |
final height = math.min(16.0, bounds.height); | |
return LinearGradient( | |
begin: Alignment.topCenter, | |
end: Alignment.bottomCenter, | |
colors: [Colors.white, Colors.transparent], | |
).createShader(Rect.fromLTWH( | |
bounds.left, | |
bounds.bottom - height, | |
bounds.width, | |
height, | |
)); | |
}, | |
child: ClipRect( | |
child: OverflowBox( | |
alignment: Alignment.topCenter, | |
minHeight: widget.expandHeight, | |
maxHeight: widget.expandHeight, | |
child: Column( | |
children: [ | |
Expanded( | |
child: PageView.builder( | |
controller: _pageController, | |
itemCount: widget.children.length, | |
itemBuilder: (BuildContext context, int index) { | |
return Padding( | |
padding: const EdgeInsets.fromLTRB(16.0, 8.0, 16.0, 0.0), | |
child: widget.children[index], | |
); | |
}, | |
), | |
), | |
AnimatedBuilder( | |
animation: _pageController, | |
builder: (BuildContext context, Widget? child) { | |
return LayoutBuilder( | |
builder: (BuildContext context, BoxConstraints constraints) { | |
final pos = _pageController.position; | |
if (!pos.hasContentDimensions) { | |
return SizedBox(height: 24.0); | |
} | |
final total = | |
(pos.maxScrollExtent + pos.viewportDimension) / | |
pos.viewportDimension; | |
final page = _pageController.page!.round(); | |
return Padding( | |
padding: const EdgeInsets.all(6.0), | |
child: SizedBox( | |
width: double.infinity, | |
height: 12.0, | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
for (int i = 0; i < total; i++) ...[ | |
DecoratedBox( | |
decoration: ShapeDecoration( | |
shape: CircleBorder( | |
side: BorderSide(color: color, width: 2.0), | |
), | |
color: i == page ? color : null, | |
), | |
child: AspectRatio(aspectRatio: 1.0), | |
), | |
if (i < total - 1) const SizedBox(width: 8.0), | |
], | |
], | |
), | |
), | |
); | |
}, | |
); | |
}, | |
), | |
const SizedBox(height: 16.0), | |
], | |
), | |
), | |
), | |
), | |
), | |
], | |
), | |
Positioned( | |
width: 64.0, | |
height: 6.0, | |
bottom: 6.0, | |
child: DecoratedBox( | |
decoration: ShapeDecoration( | |
shape: StadiumBorder(), | |
color: color, | |
), | |
), | |
), | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
@immutable | |
class InfoPage extends StatelessWidget { | |
const InfoPage({ | |
Key? key, | |
required this.index, | |
}) : super(key: key); | |
final int index; | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Text( | |
'Info $index', | |
style: TextStyle( | |
fontWeight: FontWeight.bold, | |
), | |
), | |
const SizedBox(height: 6.0), | |
Expanded( | |
child: Text( | |
'Lorem ipsum dolor sit amet,\nconsectetur adipiscin elit. Pellentesque laoreet dictum viverra. In in enim sed urna auctor posuere eu at dui. Donec magna odio, ultricies ac ipsum non, aliquet consectetur nisl. Cras.', | |
overflow: TextOverflow.ellipsis, | |
maxLines: 5, | |
), | |
), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment