calculateWidth static method

double calculateWidth({
  1. required BuildContext context,
  2. required int index,
  3. required int totalSwitches,
  4. List<double>? customWidths,
  5. required double minWidth,
})

Calculates width to prevent overflow by taking screen width into account. Ignores customWidths if toggle switch is vertical

Implementation

static double calculateWidth(
    {required BuildContext context,
    required int index,
    required int totalSwitches,
    List<double>? customWidths,
    required double minWidth}) {
  /// Extra width to prevent overflow and add padding
  double extraWidth = 0.10 * totalSwitches;

  /// Max screen width
  double screenWidth = MediaQuery.of(context).size.width;

  /// Returns width per label
  ///
  /// Returns passed minWidth per label if total requested width plus extra width is less than max screen width.
  /// Returns calculated width to fit within the max screen width if total requested width plus extra width is more than max screen width.
  return customWidths != null
      ? customWidths[index]
      : ((totalSwitches + extraWidth) * minWidth < screenWidth
          ? minWidth
          : screenWidth / (totalSwitches + extraWidth));
}