|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +class Utils { |
| 4 | + /// Calculates width to prevent overflow by taking screen width into account. |
| 5 | + /// Ignores customWidths if toggle switch is vertical |
| 6 | + static double calculateWidth( |
| 7 | + {required BuildContext context, |
| 8 | + required int index, |
| 9 | + required int totalSwitches, |
| 10 | + List<double>? customWidths, |
| 11 | + required double minWidth}) { |
| 12 | + /// Extra width to prevent overflow and add padding |
| 13 | + double extraWidth = 0.10 * totalSwitches; |
| 14 | + |
| 15 | + /// Max screen width |
| 16 | + double screenWidth = MediaQuery.of(context).size.width; |
| 17 | + |
| 18 | + /// Returns width per label |
| 19 | + /// |
| 20 | + /// Returns passed minWidth per label if total requested width plus extra width is less than max screen width. |
| 21 | + /// Returns calculated width to fit within the max screen width if total requested width plus extra width is more than max screen width. |
| 22 | + return customWidths != null |
| 23 | + ? customWidths[index] |
| 24 | + : ((totalSwitches + extraWidth) * minWidth < screenWidth |
| 25 | + ? minWidth |
| 26 | + : screenWidth / (totalSwitches + extraWidth)); |
| 27 | + } |
| 28 | + |
| 29 | + /// Ignores customHeights if toggle switch is horizontal |
| 30 | + static double calculateHeight( |
| 31 | + {required BuildContext context, |
| 32 | + required int index, |
| 33 | + required int totalSwitches, |
| 34 | + List<double>? customHeights, |
| 35 | + required double minHeight}) { |
| 36 | + /// Extra height to prevent overflow and add padding |
| 37 | + double extraHeight = 0.10 * totalSwitches; |
| 38 | + |
| 39 | + /// Max screen height |
| 40 | + double screenHeight = MediaQuery.of(context).size.height; |
| 41 | + |
| 42 | + /// Returns width per label |
| 43 | + /// |
| 44 | + /// Returns passed minHeight per label if total requested width plus extra height is less than max screen height. |
| 45 | + /// Returns calculated width to fit within the max screen width if total requested width plus extra height is more than max screen height. |
| 46 | + return customHeights != null |
| 47 | + ? customHeights[index] |
| 48 | + : ((totalSwitches + extraHeight) * minHeight < screenHeight |
| 49 | + ? minHeight |
| 50 | + : screenHeight / (totalSwitches + extraHeight)); |
| 51 | + } |
| 52 | +} |
0 commit comments