mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
mod: show all the staffs on intro panel
This commit is contained in:
67
lib/common/widgets/self_sized_horizontal_list.dart
Normal file
67
lib/common/widgets/self_sized_horizontal_list.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// https://stackoverflow.com/a/76605401
|
||||
|
||||
class SelfSizedHorizontalList extends StatefulWidget {
|
||||
final Widget Function(int) childBuilder;
|
||||
final int itemCount;
|
||||
final double gapSize;
|
||||
final EdgeInsetsGeometry? padding;
|
||||
const SelfSizedHorizontalList({
|
||||
super.key,
|
||||
required this.childBuilder,
|
||||
required this.itemCount,
|
||||
this.gapSize = 5,
|
||||
this.padding,
|
||||
});
|
||||
|
||||
@override
|
||||
State<SelfSizedHorizontalList> createState() =>
|
||||
_SelfSizedHorizontalListState();
|
||||
}
|
||||
|
||||
class _SelfSizedHorizontalListState extends State<SelfSizedHorizontalList> {
|
||||
final infoKey = GlobalKey();
|
||||
|
||||
double? prevHeight;
|
||||
double? get height {
|
||||
if (prevHeight != null) return prevHeight;
|
||||
prevHeight = infoKey.globalPaintBounds?.height;
|
||||
return prevHeight;
|
||||
}
|
||||
|
||||
bool get isInit => height == null;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (height == null) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((v) => setState(() {}));
|
||||
}
|
||||
if (widget.itemCount == 0) return const SizedBox();
|
||||
if (isInit) return Container(key: infoKey, child: widget.childBuilder(0));
|
||||
|
||||
return SizedBox(
|
||||
height: height,
|
||||
child: ListView.separated(
|
||||
padding: widget.padding,
|
||||
scrollDirection: Axis.horizontal,
|
||||
itemCount: widget.itemCount,
|
||||
itemBuilder: (c, i) => widget.childBuilder.call(i),
|
||||
separatorBuilder: (c, i) => SizedBox(width: widget.gapSize),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension GlobalKeyExtension on GlobalKey {
|
||||
Rect? get globalPaintBounds {
|
||||
final renderObject = currentContext?.findRenderObject();
|
||||
final translation = renderObject?.getTransformTo(null).getTranslation();
|
||||
if (translation != null && renderObject?.paintBounds != null) {
|
||||
final offset = Offset(translation.x, translation.y);
|
||||
return renderObject!.paintBounds.shift(offset);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user