mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
115 lines
4.5 KiB
Dart
115 lines
4.5 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:flutter/rendering.dart';
|
|
import 'storage.dart';
|
|
|
|
class Grid {
|
|
static double maxRowWidth = GStorage.setting
|
|
.get(SettingBoxKey.maxRowWidth, defaultValue: 240.0) as double;
|
|
//
|
|
// static double calculateActualWidth(BuildContext context, double maxCrossAxisExtent, double crossAxisSpacing, {double? screenWidthOffset}) {
|
|
// double screenWidth = MediaQuery.of(context).size.width;
|
|
// if (screenWidthOffset != null) {
|
|
// screenWidth -= screenWidthOffset;
|
|
// }
|
|
// if (GStorage.setting.get(SettingBoxKey.useSideBar, defaultValue: false) as bool) {
|
|
// screenWidth -= 55;
|
|
// }
|
|
// int columnCount = ((screenWidth - crossAxisSpacing) / (maxCrossAxisExtent + crossAxisSpacing)).ceil();
|
|
// if (columnCount < 1){
|
|
// columnCount = 1;
|
|
// }
|
|
// double columnWidth = (screenWidth - crossAxisSpacing) ~/ columnCount - crossAxisSpacing;
|
|
// return columnWidth;
|
|
// }
|
|
}
|
|
|
|
class SliverGridDelegateWithExtentAndRatio extends SliverGridDelegate {
|
|
/// Creates a delegate that makes grid layouts with tiles that have a maximum
|
|
/// cross-axis extent.
|
|
///
|
|
/// The [maxCrossAxisExtent], [mainAxisExtent], [mainAxisSpacing],
|
|
/// and [crossAxisSpacing] arguments must not be negative.
|
|
/// The [childAspectRatio] argument must be greater than zero.
|
|
const SliverGridDelegateWithExtentAndRatio({
|
|
required this.maxCrossAxisExtent,
|
|
this.mainAxisSpacing = 0.0,
|
|
this.crossAxisSpacing = 0.0,
|
|
this.childAspectRatio = 1.0,
|
|
this.mainAxisExtent = 0.0,
|
|
}) : assert(maxCrossAxisExtent > 0),
|
|
assert(mainAxisSpacing >= 0),
|
|
assert(crossAxisSpacing >= 0),
|
|
assert(childAspectRatio > 0);
|
|
|
|
/// The maximum extent of tiles in the cross axis.
|
|
///
|
|
/// This delegate will select a cross-axis extent for the tiles that is as
|
|
/// large as possible subject to the following conditions:
|
|
///
|
|
/// - The extent evenly divides the cross-axis extent of the grid.
|
|
/// - The extent is at most [maxCrossAxisExtent].
|
|
///
|
|
/// For example, if the grid is vertical, the grid is 500.0 pixels wide, and
|
|
/// [maxCrossAxisExtent] is 150.0, this delegate will create a grid with 4
|
|
/// columns that are 125.0 pixels wide.
|
|
final double maxCrossAxisExtent;
|
|
|
|
/// The number of logical pixels between each child along the main axis.
|
|
final double mainAxisSpacing;
|
|
|
|
/// The number of logical pixels between each child along the cross axis.
|
|
final double crossAxisSpacing;
|
|
|
|
/// The ratio of the cross-axis to the main-axis extent of each child.
|
|
final double childAspectRatio;
|
|
|
|
/// The extent of each tile in the main axis. If provided, it would add
|
|
/// after [childAspectRatio] is used.
|
|
final double mainAxisExtent;
|
|
|
|
bool _debugAssertIsValid(double crossAxisExtent) {
|
|
assert(crossAxisExtent > 0.0);
|
|
assert(maxCrossAxisExtent > 0.0);
|
|
assert(mainAxisSpacing >= 0.0);
|
|
assert(crossAxisSpacing >= 0.0);
|
|
assert(childAspectRatio > 0.0);
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
SliverGridLayout getLayout(SliverConstraints constraints) {
|
|
assert(_debugAssertIsValid(constraints.crossAxisExtent));
|
|
int crossAxisCount = ((constraints.crossAxisExtent - crossAxisSpacing) /
|
|
(maxCrossAxisExtent + crossAxisSpacing))
|
|
.ceil();
|
|
// Ensure a minimum count of 1, can be zero and result in an infinite extent
|
|
// below when the window size is 0.
|
|
crossAxisCount = max(1, crossAxisCount);
|
|
final double usableCrossAxisExtent = max(
|
|
0.0,
|
|
constraints.crossAxisExtent - crossAxisSpacing * (crossAxisCount - 1),
|
|
);
|
|
final double childCrossAxisExtent = usableCrossAxisExtent / crossAxisCount;
|
|
final double childMainAxisExtent =
|
|
childCrossAxisExtent / childAspectRatio + mainAxisExtent;
|
|
return SliverGridRegularTileLayout(
|
|
crossAxisCount: crossAxisCount,
|
|
mainAxisStride: childMainAxisExtent + mainAxisSpacing,
|
|
crossAxisStride: childCrossAxisExtent + crossAxisSpacing,
|
|
childMainAxisExtent: childMainAxisExtent,
|
|
childCrossAxisExtent: childCrossAxisExtent,
|
|
reverseCrossAxis: axisDirectionIsReversed(constraints.crossAxisDirection),
|
|
);
|
|
}
|
|
|
|
@override
|
|
bool shouldRelayout(SliverGridDelegateWithExtentAndRatio oldDelegate) {
|
|
return oldDelegate.maxCrossAxisExtent != maxCrossAxisExtent ||
|
|
oldDelegate.mainAxisSpacing != mainAxisSpacing ||
|
|
oldDelegate.crossAxisSpacing != crossAxisSpacing ||
|
|
oldDelegate.childAspectRatio != childAspectRatio ||
|
|
oldDelegate.mainAxisExtent != mainAxisExtent;
|
|
}
|
|
}
|