mirror of
https://github.com/gabehf/Fladder.git
synced 2026-03-07 21:48:14 -08:00
feat: Android TV support (#503)
Co-authored-by: PartyDonut <PartyDonut@users.noreply.github.com>
This commit is contained in:
parent
7ab8c015b9
commit
c299492d6d
168 changed files with 12019 additions and 3073 deletions
16
lib/widgets/shared/ensure_visible.dart
Normal file
16
lib/widgets/shared/ensure_visible.dart
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
extension EnsureVisibleHelper on BuildContext {
|
||||
Future<void> ensureVisible({
|
||||
Duration duration = const Duration(milliseconds: 300),
|
||||
double? alignment,
|
||||
Curve curve = Curves.fastOutSlowIn,
|
||||
}) {
|
||||
return Scrollable.ensureVisible(
|
||||
this,
|
||||
duration: duration,
|
||||
alignment: alignment ?? 0.5,
|
||||
curve: curve,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,14 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:fladder/screens/shared/flat_button.dart';
|
||||
import 'package:fladder/util/adaptive_layout/adaptive_layout.dart';
|
||||
import 'package:fladder/util/focus_provider.dart';
|
||||
import 'package:fladder/widgets/shared/ensure_visible.dart';
|
||||
import 'package:fladder/widgets/shared/item_actions.dart';
|
||||
import 'package:fladder/widgets/shared/modal_bottom_sheet.dart';
|
||||
|
||||
class EnumBox<T> extends StatelessWidget {
|
||||
final String current;
|
||||
final List<PopupMenuEntry<T>> Function(BuildContext context) itemBuilder;
|
||||
final List<ItemAction> Function(BuildContext context) itemBuilder;
|
||||
|
||||
const EnumBox({required this.current, required this.itemBuilder, super.key});
|
||||
|
||||
|
|
@ -15,7 +17,7 @@ class EnumBox<T> extends StatelessWidget {
|
|||
final textStyle = Theme.of(context).textTheme.titleMedium;
|
||||
const padding = EdgeInsets.symmetric(horizontal: 12, vertical: 6);
|
||||
final itemList = itemBuilder(context);
|
||||
final useBottomSheet = AdaptiveLayout.viewSizeOf(context) <= ViewSize.phone;
|
||||
final useBottomSheet = AdaptiveLayout.inputDeviceOf(context) != InputDevice.pointer;
|
||||
|
||||
final labelWidget = Padding(
|
||||
padding: padding,
|
||||
|
|
@ -46,29 +48,39 @@ class EnumBox<T> extends StatelessWidget {
|
|||
),
|
||||
);
|
||||
return Card(
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
color: itemList.length > 1 ? Theme.of(context).colorScheme.primaryContainer : Colors.transparent,
|
||||
shadowColor: Colors.transparent,
|
||||
elevation: 0,
|
||||
child: useBottomSheet
|
||||
? FlatButton(
|
||||
? FocusButton(
|
||||
child: labelWidget,
|
||||
onTap: () => showBottomSheetPill(
|
||||
context: context,
|
||||
content: (context, scrollController) => ListView(
|
||||
shrinkWrap: true,
|
||||
controller: scrollController,
|
||||
children: [
|
||||
const SizedBox(height: 6),
|
||||
...itemBuilder(context),
|
||||
],
|
||||
),
|
||||
),
|
||||
darkOverlay: false,
|
||||
onFocusChanged: (value) {
|
||||
if (value) {
|
||||
context.ensureVisible();
|
||||
}
|
||||
},
|
||||
onTap: itemList.length > 1
|
||||
? () => showBottomSheetPill(
|
||||
context: context,
|
||||
content: (context, scrollController) => ListView(
|
||||
shrinkWrap: true,
|
||||
controller: scrollController,
|
||||
children: [
|
||||
const SizedBox(height: 6),
|
||||
...itemList.map(
|
||||
(e) => e.toListItem(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: null,
|
||||
)
|
||||
: PopupMenuButton(
|
||||
tooltip: '',
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
enabled: itemList.length > 1,
|
||||
itemBuilder: itemBuilder,
|
||||
itemBuilder: (context) => itemList.map((e) => e.toPopupMenuItem()).toList(),
|
||||
padding: padding,
|
||||
child: labelWidget,
|
||||
),
|
||||
|
|
@ -79,7 +91,7 @@ class EnumBox<T> extends StatelessWidget {
|
|||
class EnumSelection<T> extends StatelessWidget {
|
||||
final Text label;
|
||||
final String current;
|
||||
final List<PopupMenuEntry<T>> Function(BuildContext context) itemBuilder;
|
||||
final List<ItemAction> Function(BuildContext context) itemBuilder;
|
||||
const EnumSelection({
|
||||
super.key,
|
||||
required this.label,
|
||||
|
|
|
|||
186
lib/widgets/shared/grid_focus_traveler.dart
Normal file
186
lib/widgets/shared/grid_focus_traveler.dart
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import 'package:fladder/util/focus_provider.dart';
|
||||
import 'package:fladder/widgets/navigation_scaffold/components/side_navigation_bar.dart';
|
||||
|
||||
class GridFocusTraveler extends ConsumerStatefulWidget {
|
||||
final int currentIndex;
|
||||
final int itemCount;
|
||||
final int crossAxisCount;
|
||||
final Function(BuildContext context, int selectedIndex, int index) itemBuilder;
|
||||
final SliverGridDelegate gridDelegate;
|
||||
|
||||
const GridFocusTraveler({
|
||||
this.currentIndex = 0,
|
||||
required this.itemCount,
|
||||
required this.crossAxisCount,
|
||||
required this.itemBuilder,
|
||||
required this.gridDelegate,
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
ConsumerState<GridFocusTraveler> createState() => _GridFocusTravelerState();
|
||||
}
|
||||
|
||||
class _GridFocusTravelerState extends ConsumerState<GridFocusTraveler> {
|
||||
late int selectedIndex = widget.currentIndex;
|
||||
|
||||
late final List<FocusNode> _focusNodes;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_focusNodes = List.generate(widget.itemCount, (index) => FocusNode());
|
||||
_focusNodes.mapIndexed(
|
||||
(index, element) {
|
||||
element.addListener(() {
|
||||
if (element.hasFocus) {
|
||||
setState(() {
|
||||
selectedIndex = index;
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
if (!FocusProvider.autoFocusOf(context)) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_focusNodes.firstOrNull?.requestFocus();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(GridFocusTraveler oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.itemCount != oldWidget.itemCount) {
|
||||
for (var node in _focusNodes) {
|
||||
node.dispose();
|
||||
}
|
||||
_focusNodes = List.generate(widget.itemCount, (index) => FocusNode());
|
||||
if (selectedIndex >= widget.itemCount) {
|
||||
selectedIndex = widget.itemCount - 1;
|
||||
if (selectedIndex >= 0) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_focusNodes[selectedIndex].requestFocus();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (var node in _focusNodes) {
|
||||
node.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FocusTraversalGroup(
|
||||
policy: GridFocusTravelerPolicy(
|
||||
navBarNode: navBarNode,
|
||||
nodes: _focusNodes,
|
||||
crossAxisCount: widget.crossAxisCount,
|
||||
onChanged: (value) {
|
||||
selectedIndex = value;
|
||||
_focusNodes[value].requestFocus();
|
||||
},
|
||||
),
|
||||
child: SliverGrid.builder(
|
||||
gridDelegate: widget.gridDelegate,
|
||||
itemCount: widget.itemCount,
|
||||
itemBuilder: (context, index) {
|
||||
return FocusProvider(
|
||||
focusNode: _focusNodes[index],
|
||||
child: Builder(
|
||||
builder: (context) => widget.itemBuilder(context, selectedIndex, index),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class GridFocusTravelerPolicy extends ReadingOrderTraversalPolicy {
|
||||
/// The complete list of FocusNodes for the grid.
|
||||
final List<FocusNode> nodes;
|
||||
|
||||
/// The number of items in each row.
|
||||
final int crossAxisCount;
|
||||
|
||||
/// Callback to notify the parent which node index should be focused next.
|
||||
final Function(int value) onChanged;
|
||||
|
||||
/// The navigation bar node to focus when navigating left from the first column.
|
||||
final FocusNode navBarNode;
|
||||
|
||||
GridFocusTravelerPolicy({
|
||||
required this.nodes,
|
||||
required this.crossAxisCount,
|
||||
required this.onChanged,
|
||||
required this.navBarNode,
|
||||
});
|
||||
|
||||
@override
|
||||
bool inDirection(FocusNode currentNode, TraversalDirection direction) {
|
||||
final int current = nodes.indexOf(currentNode);
|
||||
if (current == -1) {
|
||||
return super.inDirection(currentNode, direction);
|
||||
}
|
||||
|
||||
final int itemCount = nodes.length;
|
||||
final int row = current ~/ crossAxisCount;
|
||||
final int col = current % crossAxisCount;
|
||||
final int rowCount = (itemCount / crossAxisCount).ceil();
|
||||
int? next;
|
||||
|
||||
switch (direction) {
|
||||
case TraversalDirection.left:
|
||||
if (col > 0) {
|
||||
next = current - 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case TraversalDirection.right:
|
||||
if (col < crossAxisCount - 1 && current + 1 < itemCount) {
|
||||
next = current + 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case TraversalDirection.up:
|
||||
if (row > 0) {
|
||||
next = current - crossAxisCount;
|
||||
}
|
||||
break;
|
||||
|
||||
case TraversalDirection.down:
|
||||
if (row < rowCount - 1) {
|
||||
final int candidate = current + crossAxisCount;
|
||||
if (candidate < itemCount) {
|
||||
next = candidate;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (next != null) {
|
||||
onChanged(next);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (direction == TraversalDirection.left && col == 0) {
|
||||
navBarNode.requestFocus();
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.inDirection(currentNode, direction);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import 'dart:math';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
|
@ -7,26 +7,32 @@ import 'package:iconsax_plus/iconsax_plus.dart';
|
|||
|
||||
import 'package:fladder/providers/settings/client_settings_provider.dart';
|
||||
import 'package:fladder/util/adaptive_layout/adaptive_layout.dart';
|
||||
import 'package:fladder/util/disable_keypad_focus.dart';
|
||||
import 'package:fladder/util/focus_provider.dart';
|
||||
import 'package:fladder/util/list_padding.dart';
|
||||
import 'package:fladder/util/sticky_header_text.dart';
|
||||
import 'package:fladder/widgets/navigation_scaffold/components/side_navigation_bar.dart';
|
||||
import 'package:fladder/widgets/shared/ensure_visible.dart';
|
||||
|
||||
class HorizontalList<T> extends ConsumerStatefulWidget {
|
||||
final bool autoFocus;
|
||||
final String? label;
|
||||
final List<Widget> titleActions;
|
||||
final Function()? onLabelClick;
|
||||
final String? subtext;
|
||||
final List<T> items;
|
||||
final int? startIndex;
|
||||
final Widget Function(BuildContext context, int index) itemBuilder;
|
||||
final Widget Function(BuildContext context, int index, int selected) itemBuilder;
|
||||
final Function(int index)? onFocused;
|
||||
final bool scrollToEnd;
|
||||
final EdgeInsets contentPadding;
|
||||
final double? dominantRatio;
|
||||
final double? height;
|
||||
final bool shrinkWrap;
|
||||
const HorizontalList({
|
||||
this.autoFocus = false,
|
||||
required this.items,
|
||||
required this.itemBuilder,
|
||||
this.onFocused,
|
||||
this.startIndex,
|
||||
this.height,
|
||||
this.label,
|
||||
|
|
@ -45,44 +51,100 @@ class HorizontalList<T> extends ConsumerStatefulWidget {
|
|||
}
|
||||
|
||||
class _HorizontalListState extends ConsumerState<HorizontalList> {
|
||||
final FocusNode parentNode = FocusNode();
|
||||
late int currentIndex = 0;
|
||||
final GlobalKey _firstItemKey = GlobalKey();
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
final contentPadding = 8.0;
|
||||
double? contentWidth;
|
||||
double? _firstItemWidth;
|
||||
bool hasFocus = false;
|
||||
|
||||
late List<FocusNode> _focusNodes;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_measureFirstItem(scrollTo: true);
|
||||
_initFocusNodes();
|
||||
_measureFirstItem();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _measureFirstItem({bool scrollTo = false}) {
|
||||
void _measureFirstItem() {
|
||||
if (_firstItemWidth != null) return;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (widget.startIndex != null) {
|
||||
final context = _firstItemKey.currentContext;
|
||||
if (context != null) {
|
||||
final box = context.findRenderObject() as RenderBox;
|
||||
_firstItemWidth = box.size.width;
|
||||
if (scrollTo) {
|
||||
_scrollToPosition(widget.startIndex!);
|
||||
}
|
||||
}
|
||||
final context = _firstItemKey.currentContext;
|
||||
if (context != null) {
|
||||
final box = context.findRenderObject() as RenderBox;
|
||||
_firstItemWidth = box.size.width;
|
||||
_scrollToPosition(widget.startIndex ?? 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _scrollToPosition(int index) {
|
||||
final offset = index * _firstItemWidth! + index * contentPadding;
|
||||
_scrollController.animateTo(
|
||||
offset,
|
||||
void _initFocusNodes() {
|
||||
_focusNodes = List.generate(widget.items.length, (i) {
|
||||
final node = FocusNode();
|
||||
node.addListener(() {
|
||||
if (node.hasFocus) {
|
||||
_scrollToPosition(i);
|
||||
if (widget.onFocused != null) {
|
||||
widget.onFocused?.call(i);
|
||||
} else {
|
||||
context.ensureVisible();
|
||||
}
|
||||
}
|
||||
});
|
||||
return node;
|
||||
});
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (widget.autoFocus) {
|
||||
_focusNodes[currentIndex].requestFocus();
|
||||
context.ensureVisible();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (var node in _focusNodes) {
|
||||
node.dispose();
|
||||
}
|
||||
parentNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(HorizontalList oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
|
||||
if (widget.items.length != oldWidget.items.length) {
|
||||
for (var node in _focusNodes) {
|
||||
node.dispose();
|
||||
}
|
||||
_initFocusNodes();
|
||||
|
||||
if (currentIndex >= widget.items.length) {
|
||||
currentIndex = widget.items.isEmpty ? 0 : widget.items.length - 1;
|
||||
}
|
||||
|
||||
if (widget.items.isNotEmpty && parentNode.hasFocus) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_focusNodes[currentIndex].requestFocus();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _scrollToPosition(int index) async {
|
||||
if (_firstItemWidth == null) return;
|
||||
|
||||
final offset = index * (_firstItemWidth! + contentPadding);
|
||||
final clamped = math.min(offset, _scrollController.position.maxScrollExtent);
|
||||
|
||||
await _scrollController.animateTo(
|
||||
clamped,
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOut,
|
||||
curve: Curves.fastOutSlowIn,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -90,34 +152,35 @@ class _HorizontalListState extends ConsumerState<HorizontalList> {
|
|||
_scrollController.animateTo(
|
||||
0,
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOut,
|
||||
curve: Curves.fastOutSlowIn,
|
||||
);
|
||||
}
|
||||
|
||||
void _scrollToEnd() {
|
||||
Future<void> _scrollToEnd() async {
|
||||
final offset = (_firstItemWidth ?? 200) * widget.items.length + 200;
|
||||
_scrollController.animateTo(
|
||||
(_firstItemWidth ?? 200) * widget.items.length + 200,
|
||||
math.min(offset, _scrollController.position.maxScrollExtent),
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOut,
|
||||
curve: Curves.fastOutSlowIn,
|
||||
);
|
||||
}
|
||||
|
||||
int getFirstVisibleIndex() {
|
||||
if (widget.startIndex == null) return 0;
|
||||
if (!_scrollController.hasClients || _firstItemWidth == null) return 0;
|
||||
return (_scrollController.offset / _firstItemWidth!).floor().clamp(0, widget.items.length - 1);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final hasPointer = AdaptiveLayout.of(context).inputDevice == InputDevice.pointer;
|
||||
final content = Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
DisableFocus(
|
||||
child: Padding(
|
||||
return Focus(
|
||||
focusNode: parentNode,
|
||||
onFocusChange: (value) {
|
||||
if (value) {
|
||||
_focusNodes[currentIndex].requestFocus();
|
||||
}
|
||||
},
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Padding(
|
||||
padding: widget.contentPadding,
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
|
|
@ -128,18 +191,22 @@ class _HorizontalListState extends ConsumerState<HorizontalList> {
|
|||
children: [
|
||||
if (widget.label != null)
|
||||
Flexible(
|
||||
child: StickyHeaderText(
|
||||
label: widget.label ?? "",
|
||||
onClick: widget.onLabelClick,
|
||||
child: ExcludeFocus(
|
||||
child: StickyHeaderText(
|
||||
label: widget.label ?? "",
|
||||
onClick: widget.onLabelClick,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (widget.subtext != null)
|
||||
Flexible(
|
||||
child: Opacity(
|
||||
opacity: 0.5,
|
||||
child: Text(
|
||||
widget.subtext!,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
child: ExcludeFocus(
|
||||
child: Opacity(
|
||||
opacity: 0.5,
|
||||
child: Text(
|
||||
widget.subtext!,
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
|
@ -148,89 +215,135 @@ class _HorizontalListState extends ConsumerState<HorizontalList> {
|
|||
),
|
||||
),
|
||||
if (widget.items.length > 1)
|
||||
Card(
|
||||
elevation: 5,
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
if (hasPointer)
|
||||
GestureDetector(
|
||||
onLongPress: () => _scrollToStart(),
|
||||
child: IconButton(
|
||||
ExcludeFocus(
|
||||
child: Card(
|
||||
elevation: 5,
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
if (hasPointer)
|
||||
GestureDetector(
|
||||
onLongPress: () => _scrollToStart(),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.offset + -(MediaQuery.of(context).size.width / 1.75),
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOut);
|
||||
},
|
||||
icon: const Icon(
|
||||
IconsaxPlusLinear.arrow_left_1,
|
||||
size: 20,
|
||||
)),
|
||||
),
|
||||
if (widget.startIndex != null)
|
||||
IconButton(
|
||||
tooltip: "Scroll to current",
|
||||
onPressed: () {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.offset + -(MediaQuery.of(context).size.width / 1.75),
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOut);
|
||||
},
|
||||
icon: const Icon(
|
||||
IconsaxPlusLinear.arrow_left_1,
|
||||
size: 20,
|
||||
)),
|
||||
),
|
||||
if (widget.startIndex != null)
|
||||
IconButton(
|
||||
tooltip: "Scroll to current",
|
||||
onPressed: () {
|
||||
if (_firstItemWidth != null && widget.startIndex != null) {
|
||||
_scrollToPosition(widget.startIndex!);
|
||||
}
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.circle,
|
||||
size: 16,
|
||||
)),
|
||||
if (hasPointer)
|
||||
GestureDetector(
|
||||
onLongPress: () => _scrollToEnd(),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.offset + (MediaQuery.of(context).size.width / 1.75),
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOut);
|
||||
},
|
||||
icon: const Icon(
|
||||
IconsaxPlusLinear.arrow_right_3,
|
||||
size: 20,
|
||||
Icons.circle,
|
||||
size: 16,
|
||||
)),
|
||||
),
|
||||
],
|
||||
if (hasPointer)
|
||||
GestureDetector(
|
||||
onLongPress: () => _scrollToEnd(),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
_scrollController.animateTo(
|
||||
_scrollController.offset + (MediaQuery.of(context).size.width / 1.75),
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeInOut);
|
||||
},
|
||||
icon: const Icon(
|
||||
IconsaxPlusLinear.arrow_right_3,
|
||||
size: 20,
|
||||
)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
].addPadding(const EdgeInsets.symmetric(horizontal: 6)),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
SizedBox(
|
||||
height: widget.height ??
|
||||
((AdaptiveLayout.poster(context).size *
|
||||
ref.watch(clientSettingsProvider.select((value) => value.posterSize))) /
|
||||
pow((widget.dominantRatio ?? 1.0), 0.55)) *
|
||||
0.72,
|
||||
child: ListView.separated(
|
||||
controller: _scrollController,
|
||||
scrollDirection: Axis.horizontal,
|
||||
padding: widget.contentPadding,
|
||||
itemBuilder: (context, index) => index == getFirstVisibleIndex()
|
||||
? Container(
|
||||
key: _firstItemKey,
|
||||
child: widget.itemBuilder(context, index),
|
||||
)
|
||||
: widget.itemBuilder(context, index),
|
||||
separatorBuilder: (context, index) => SizedBox(width: contentPadding),
|
||||
itemCount: widget.items.length,
|
||||
const SizedBox(height: 8),
|
||||
SizedBox(
|
||||
height: widget.height ??
|
||||
((AdaptiveLayout.poster(context).size *
|
||||
ref.watch(clientSettingsProvider.select((value) => value.posterSize))) /
|
||||
math.pow((widget.dominantRatio ?? 1.0), 0.55)) *
|
||||
0.72,
|
||||
child: FocusTraversalGroup(
|
||||
policy: HorizontalRailFocus(
|
||||
parentNode: parentNode,
|
||||
nodes: _focusNodes,
|
||||
onChanged: (value) {
|
||||
currentIndex = value;
|
||||
_focusNodes[value].requestFocus();
|
||||
}),
|
||||
child: ExcludeFocusTraversal(
|
||||
child: ListView.separated(
|
||||
controller: _scrollController,
|
||||
scrollDirection: Axis.horizontal,
|
||||
padding: widget.contentPadding,
|
||||
itemBuilder: (context, index) {
|
||||
return FocusProvider(
|
||||
focusNode: _focusNodes[index],
|
||||
hasFocus: hasFocus && index == currentIndex,
|
||||
key: index == 0 ? _firstItemKey : null,
|
||||
child: widget.itemBuilder(context, index, hasFocus ? currentIndex : -1),
|
||||
);
|
||||
},
|
||||
separatorBuilder: (context, index) => SizedBox(width: contentPadding),
|
||||
itemCount: widget.items.length,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
return widget.startIndex == null
|
||||
? content
|
||||
: LayoutBuilder(builder: (context, constraints) {
|
||||
_measureFirstItem();
|
||||
return content;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class HorizontalRailFocus extends WidgetOrderTraversalPolicy {
|
||||
final FocusNode parentNode;
|
||||
final List<FocusNode> nodes;
|
||||
final Function(int value) onChanged;
|
||||
HorizontalRailFocus({
|
||||
required this.parentNode,
|
||||
required this.nodes,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
bool inDirection(FocusNode currentNode, TraversalDirection direction) {
|
||||
// Find the index of the currently focused node
|
||||
final int current = nodes.indexWhere((node) => node.hasFocus);
|
||||
// If nothing is focused, default to 0
|
||||
final int currentIndex = current == -1 ? 0 : current;
|
||||
|
||||
if (direction == TraversalDirection.left) {
|
||||
if (currentIndex <= 0) {
|
||||
navBarNode.requestFocus();
|
||||
return true;
|
||||
} else {
|
||||
onChanged(math.max(currentIndex - 1, 0));
|
||||
return true;
|
||||
}
|
||||
} else if (direction == TraversalDirection.right) {
|
||||
if (currentIndex >= nodes.length - 1) {
|
||||
// Corrected boundary check
|
||||
return super.inDirection(parentNode, direction);
|
||||
} else {
|
||||
onChanged(math.min(currentIndex + 1, nodes.length - 1));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
parentNode.requestFocus();
|
||||
return super.inDirection(parentNode, direction);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,21 +33,25 @@ class ItemActionDivider extends ItemAction {
|
|||
}
|
||||
|
||||
class ItemActionButton extends ItemAction {
|
||||
final bool selected;
|
||||
final Widget? icon;
|
||||
final Widget? label;
|
||||
final FutureOr<void> Function()? action;
|
||||
ItemActionButton({
|
||||
this.selected = false,
|
||||
this.icon,
|
||||
this.label,
|
||||
this.action,
|
||||
});
|
||||
|
||||
ItemActionButton copyWith({
|
||||
bool? selected,
|
||||
Widget? icon,
|
||||
Widget? label,
|
||||
Future<void> Function()? action,
|
||||
}) {
|
||||
return ItemActionButton(
|
||||
selected: selected ?? this.selected,
|
||||
icon: icon ?? this.icon,
|
||||
label: label ?? this.label,
|
||||
action: action ?? this.action,
|
||||
|
|
@ -93,14 +97,19 @@ class ItemActionButton extends ItemAction {
|
|||
|
||||
@override
|
||||
Widget toListItem(BuildContext context, {bool useIcons = false, bool shouldPop = true}) {
|
||||
final foregroundColor =
|
||||
selected ? Theme.of(context).colorScheme.onPrimaryContainer : Theme.of(context).colorScheme.onSurface;
|
||||
return ElevatedButton(
|
||||
autofocus: selected,
|
||||
style: ButtonStyle(
|
||||
backgroundColor: const WidgetStatePropertyAll(Colors.transparent),
|
||||
backgroundColor: WidgetStatePropertyAll(
|
||||
selected ? Theme.of(context).colorScheme.primaryContainer : Colors.transparent,
|
||||
),
|
||||
padding: const WidgetStatePropertyAll(EdgeInsets.symmetric(horizontal: 12)),
|
||||
minimumSize: const WidgetStatePropertyAll(Size(50, 50)),
|
||||
elevation: const WidgetStatePropertyAll(0),
|
||||
foregroundColor: WidgetStatePropertyAll(Theme.of(context).colorScheme.onSurface),
|
||||
iconColor: WidgetStatePropertyAll(Theme.of(context).colorScheme.onSurface),
|
||||
foregroundColor: WidgetStatePropertyAll(foregroundColor),
|
||||
iconColor: WidgetStatePropertyAll(foregroundColor),
|
||||
),
|
||||
onPressed: () {
|
||||
if (shouldPop) {
|
||||
|
|
@ -113,7 +122,7 @@ class ItemActionButton extends ItemAction {
|
|||
builder: (context) {
|
||||
return Theme(
|
||||
data: ThemeData(
|
||||
iconTheme: IconThemeData(color: Theme.of(context).colorScheme.onSurface),
|
||||
iconTheme: IconThemeData(color: foregroundColor),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
|
|
@ -134,10 +143,13 @@ extension ItemActionExtension on List<ItemAction> {
|
|||
List<PopupMenuEntry> popupMenuItems({bool useIcons = false}) => map((e) => e.toPopupMenuItem(useIcons: useIcons))
|
||||
.whereNotIndexed((index, element) => (index == 0 && element is PopupMenuDivider))
|
||||
.toList();
|
||||
|
||||
List<Widget> menuItemButtonItems() =>
|
||||
map((e) => e.toMenuItemButton()).whereNotIndexed((index, element) => (index == 0 && element is Divider)).toList();
|
||||
List<Widget> listTileItems(BuildContext context, {bool useIcons = false, bool shouldPop = true}) =>
|
||||
map((e) => e.toListItem(context, useIcons: useIcons, shouldPop: shouldPop))
|
||||
.whereNotIndexed((index, element) => (index == 0 && element is Divider))
|
||||
.toList();
|
||||
|
||||
List<Widget> listTileItems(BuildContext context, {bool useIcons = false, bool shouldPop = true}) {
|
||||
return map((e) => e.toListItem(context, useIcons: useIcons, shouldPop: shouldPop))
|
||||
.whereNotIndexed((index, element) => (index == 0 && element is Divider))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|||
|
||||
import 'package:fladder/screens/shared/animated_fade_size.dart';
|
||||
import 'package:fladder/util/refresh_state.dart';
|
||||
import 'package:fladder/widgets/shared/ensure_visible.dart';
|
||||
|
||||
class SelectableIconButton extends ConsumerStatefulWidget {
|
||||
final FutureOr<dynamic> Function() onPressed;
|
||||
|
|
@ -33,6 +34,7 @@ class SelectableIconButton extends ConsumerStatefulWidget {
|
|||
|
||||
class _SelectableIconButtonState extends ConsumerState<SelectableIconButton> {
|
||||
bool loading = false;
|
||||
bool focused = false;
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const duration = Duration(milliseconds: 250);
|
||||
|
|
@ -51,6 +53,16 @@ class _SelectableIconButtonState extends ConsumerState<SelectableIconButton> {
|
|||
widget.iconColor ?? (widget.selected ? Theme.of(context).colorScheme.onPrimary : null)),
|
||||
padding: const WidgetStatePropertyAll(EdgeInsets.zero),
|
||||
),
|
||||
onFocusChange: (value) {
|
||||
setState(() {
|
||||
focused = value;
|
||||
});
|
||||
if (value) {
|
||||
context.ensureVisible(
|
||||
alignment: 1.0,
|
||||
);
|
||||
}
|
||||
},
|
||||
onPressed: loading
|
||||
? null
|
||||
: () async {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue