1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 06:19:22 +00:00
comunicmobile/lib/ui/widgets/custom_list_tile.dart

88 lines
2.2 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
/// My custom list tile to add extra features to the default
/// implementation
///
/// @author Pierre HUBERT
class CustomListTile extends StatelessWidget {
final Widget? leading;
final Widget? title;
final Widget? subtitle;
final Widget? trailing;
final bool isThreeLine;
final bool? dense;
final EdgeInsetsGeometry? contentPadding;
final bool enabled;
final GestureTapCallback? onTap;
final GestureLongPressCallback? onLongPress;
final bool selected;
final Color? tileColor;
/// Custom onLongPress function
final Function(Size, Offset)? onLongPressWithInfo;
2020-05-07 17:04:58 +00:00
/// Show menu onLongPress
final Function(RelativeRect)? onLongPressOpenMenu;
2020-05-07 17:04:58 +00:00
const CustomListTile({
Key? key,
this.leading,
this.title,
this.subtitle,
this.trailing,
this.isThreeLine = false,
this.dense,
this.contentPadding,
this.enabled = true,
this.onTap,
this.onLongPress,
this.selected = false,
this.onLongPressWithInfo,
2020-05-07 17:04:58 +00:00
this.onLongPressOpenMenu,
2021-03-10 17:12:56 +00:00
this.tileColor,
2022-03-11 15:40:56 +00:00
}) : assert(!isThreeLine || subtitle != null),
super(key: key);
@override
Widget build(BuildContext context) {
return ListTile(
2021-03-10 17:12:56 +00:00
tileColor: tileColor,
leading: leading,
title: title,
subtitle: subtitle,
trailing: trailing,
isThreeLine: isThreeLine,
dense: dense,
contentPadding: contentPadding,
enabled: enabled,
onTap: onTap,
onLongPress: () => _longPress(context),
selected: selected,
);
}
void _longPress(BuildContext context) {
if (onLongPress != null) onLongPress!();
2020-05-07 17:04:58 +00:00
if (onLongPressWithInfo != null || onLongPressOpenMenu != null) {
RenderBox renderBox = context.findRenderObject() as RenderBox;
2020-05-07 17:04:58 +00:00
final size = renderBox.size;
final offset = renderBox.localToGlobal(Offset(size.width, size.height));
if (onLongPressWithInfo != null) onLongPressWithInfo!(size, offset);
2020-05-07 17:04:58 +00:00
if (onLongPressOpenMenu != null) {
final position = RelativeRect.fromLTRB(
offset.dx - size.width,
offset.dy,
offset.dx,
offset.dy + size.height,
);
onLongPressOpenMenu!(position);
2020-05-07 17:04:58 +00:00
}
}
}
}