Last active
July 30, 2021 06:47
-
-
Save mrvincenzo/2fa5d82033eafc3e37c54be5c4a67113 to your computer and use it in GitHub Desktop.
Flexible vs Expanded in popup that covers part of the screen in Flutter
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
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomeWidget(), | |
); | |
} | |
} | |
enum ParentWidgetType { | |
expanded, | |
flexible, | |
} | |
const int itemsCount = 15; // <-- items count in grid | |
class MyHomeWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: SafeArea( | |
child: Container( | |
color: Colors.grey, | |
child: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
RaisedButton( | |
onPressed: () => _presentGridPopup(context, ParentWidgetType.expanded), | |
child: Text('Expanded'), | |
), | |
const SizedBox(height: 5), | |
RaisedButton( | |
onPressed: () => _presentGridPopup(context, ParentWidgetType.flexible), | |
child: Text('Flexible'), | |
) | |
], | |
)), | |
), | |
), | |
); | |
} | |
void _presentGridPopup(BuildContext context, ParentWidgetType parentWidgetType) { | |
Navigator.of(context).push<void>(PageRouteBuilder( | |
opaque: false, | |
barrierColor: null, | |
barrierDismissible: true, | |
barrierLabel: 'barrier', | |
pageBuilder: (context, animation, secondaryAnimation) => GridPopup( | |
parentWidgetType: parentWidgetType, | |
itemsCount: itemsCount, | |
), | |
transitionDuration: Duration(milliseconds: 300), | |
transitionsBuilder: (context, animation, secondaryAnimation, child) => SlideTransition( | |
position: Tween<Offset>(begin: Offset(0.0, 1.0), end: Offset.zero).animate(animation), | |
child: child, | |
), | |
)); | |
} | |
} | |
class GridPopup extends StatelessWidget { | |
final ParentWidgetType parentWidgetType; | |
final int itemsCount; | |
const GridPopup({ | |
Key? key, | |
required this.parentWidgetType, | |
required this.itemsCount, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return SafeArea( | |
child: Align( | |
alignment: Alignment.bottomCenter, | |
child: Container( | |
color: Colors.blue, | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Padding( | |
padding: EdgeInsets.symmetric(vertical: 5), | |
child: RaisedButton( | |
child: Text('Close popup'), | |
onPressed: () => Navigator.of(context).pop(), | |
), | |
), | |
_buildGrid(), | |
], | |
), | |
), | |
), | |
); | |
} | |
Widget _buildGrid() { | |
final _internal = () { | |
return GridView.count( | |
crossAxisCount: 5, | |
crossAxisSpacing: 2.0, | |
mainAxisSpacing: 2.0, | |
childAspectRatio: 1.0, | |
shrinkWrap: true, | |
children: List.generate( | |
itemsCount, | |
(index) => Container(color: Colors.green), | |
), | |
); | |
}; | |
switch (parentWidgetType) { | |
case ParentWidgetType.expanded: | |
return Expanded(child: _internal()); | |
case ParentWidgetType.flexible: | |
return Flexible(child: _internal()); | |
} | |
return Container(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment