Last active
June 24, 2025 21:11
-
-
Save esDotDev/35339d9f42a2a95eab220be115abcf94 to your computer and use it in GitHub Desktop.
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 'dart:math' as math; | |
| import 'package:flutter/material.dart'; | |
| class DropExtraChildren extends StatelessWidget { | |
| final double spacing; | |
| final List<Widget> children; | |
| const DropExtraChildren({ | |
| super.key, | |
| required this.spacing, | |
| required this.children, | |
| }); | |
| @override | |
| Widget build(BuildContext context) { | |
| return CustomMultiChildLayout( | |
| delegate: _DropExtraChildrenDelegate( | |
| spacing: spacing, | |
| childrenCount: children.length, | |
| ), | |
| children: [ | |
| for (int i = 0; i < children.length; i++) | |
| LayoutId(id: i, child: children[i]), | |
| ], | |
| ); | |
| } | |
| } | |
| class _DropExtraChildrenDelegate extends MultiChildLayoutDelegate { | |
| final double spacing; | |
| final int childrenCount; | |
| _DropExtraChildrenDelegate({ | |
| required this.spacing, | |
| required this.childrenCount, | |
| }); | |
| @override | |
| void performLayout(Size size) { | |
| double xOffset = 0.0; | |
| // Measure and position children until we run out of horizontal space | |
| for (int i = 0; i < childrenCount; i++) { | |
| if (hasChild(i)) { | |
| final childSize = layoutChild(i, BoxConstraints.loose(size)); | |
| final newOffset = xOffset + (i > 0 ? spacing : 0) + childSize.width; | |
| if (newOffset <= size.width) { | |
| xOffset += (i > 0 ? spacing : 0); | |
| positionChild(i, Offset(xOffset, 0)); | |
| xOffset += childSize.width; | |
| } else { | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| @override | |
| bool shouldRelayout(covariant _DropExtraChildrenDelegate oldDelegate) { | |
| return oldDelegate.spacing != spacing || | |
| oldDelegate.childrenCount != childrenCount; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment