card_container.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import 'package:flowy_infra/theme.dart';
  2. import 'package:flowy_infra_ui/style_widget/hover.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:styled_widget/styled_widget.dart';
  6. class BoardCardContainer extends StatelessWidget {
  7. final Widget child;
  8. final CardAccessoryBuilder? accessoryBuilder;
  9. const BoardCardContainer({
  10. required this.child,
  11. this.accessoryBuilder,
  12. Key? key,
  13. }) : super(key: key);
  14. @override
  15. Widget build(BuildContext context) {
  16. return ChangeNotifierProvider(
  17. create: (_) => _CardContainerNotifier(),
  18. child: Consumer<_CardContainerNotifier>(
  19. builder: (context, notifier, _) {
  20. Widget container = Center(child: child);
  21. if (accessoryBuilder != null) {
  22. final accessories = accessoryBuilder!(context);
  23. if (accessories.isNotEmpty) {
  24. container = _CardEnterRegion(
  25. child: container,
  26. accessories: accessories,
  27. );
  28. }
  29. }
  30. return Padding(
  31. padding: const EdgeInsets.all(8),
  32. child: container,
  33. );
  34. },
  35. ),
  36. );
  37. }
  38. }
  39. abstract class CardAccessory implements Widget {
  40. void onTap(BuildContext context);
  41. }
  42. typedef CardAccessoryBuilder = List<CardAccessory> Function(
  43. BuildContext buildContext,
  44. );
  45. class CardAccessoryContainer extends StatelessWidget {
  46. final List<CardAccessory> accessories;
  47. const CardAccessoryContainer({required this.accessories, Key? key})
  48. : super(key: key);
  49. @override
  50. Widget build(BuildContext context) {
  51. final theme = context.read<AppTheme>();
  52. final children = accessories.map((accessory) {
  53. final hover = FlowyHover(
  54. style: HoverStyle(
  55. hoverColor: theme.hover,
  56. backgroundColor: theme.surface,
  57. ),
  58. builder: (_, onHover) => Container(
  59. width: 26,
  60. height: 26,
  61. padding: const EdgeInsets.all(3),
  62. child: accessory,
  63. ),
  64. );
  65. return GestureDetector(
  66. child: hover,
  67. behavior: HitTestBehavior.opaque,
  68. onTap: () => accessory.onTap(context),
  69. );
  70. }).toList();
  71. return Wrap(children: children, spacing: 6);
  72. }
  73. }
  74. class _CardEnterRegion extends StatelessWidget {
  75. final Widget child;
  76. final List<CardAccessory> accessories;
  77. const _CardEnterRegion(
  78. {required this.child, required this.accessories, Key? key})
  79. : super(key: key);
  80. @override
  81. Widget build(BuildContext context) {
  82. return Selector<_CardContainerNotifier, bool>(
  83. selector: (context, notifier) => notifier.onEnter,
  84. builder: (context, onEnter, _) {
  85. List<Widget> children = [child];
  86. if (onEnter) {
  87. children.add(CardAccessoryContainer(accessories: accessories)
  88. .positioned(right: 0));
  89. }
  90. return MouseRegion(
  91. cursor: SystemMouseCursors.click,
  92. onEnter: (p) =>
  93. Provider.of<_CardContainerNotifier>(context, listen: false)
  94. .onEnter = true,
  95. onExit: (p) =>
  96. Provider.of<_CardContainerNotifier>(context, listen: false)
  97. .onEnter = false,
  98. child: IntrinsicHeight(
  99. child: Stack(
  100. alignment: AlignmentDirectional.center,
  101. fit: StackFit.expand,
  102. children: children,
  103. )),
  104. );
  105. },
  106. );
  107. }
  108. }
  109. class _CardContainerNotifier extends ChangeNotifier {
  110. bool _onEnter = false;
  111. _CardContainerNotifier();
  112. set onEnter(bool value) {
  113. if (_onEnter != value) {
  114. _onEnter = value;
  115. notifyListeners();
  116. }
  117. }
  118. bool get onEnter => _onEnter;
  119. }