card_container.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. final void Function(BuildContext) onTap;
  10. const BoardCardContainer({
  11. required this.child,
  12. required this.onTap,
  13. this.accessoryBuilder,
  14. Key? key,
  15. }) : super(key: key);
  16. @override
  17. Widget build(BuildContext context) {
  18. return ChangeNotifierProvider(
  19. create: (_) => _CardContainerNotifier(),
  20. child: Consumer<_CardContainerNotifier>(
  21. builder: (context, notifier, _) {
  22. Widget container = Center(child: child);
  23. if (accessoryBuilder != null) {
  24. final accessories = accessoryBuilder!(context);
  25. if (accessories.isNotEmpty) {
  26. container = _CardEnterRegion(
  27. child: container,
  28. accessories: accessories,
  29. );
  30. }
  31. }
  32. return GestureDetector(
  33. onTap: () => onTap(context),
  34. child: Padding(
  35. padding: const EdgeInsets.all(8),
  36. child: ConstrainedBox(
  37. constraints: const BoxConstraints(minHeight: 30),
  38. child: container,
  39. ),
  40. ),
  41. );
  42. },
  43. ),
  44. );
  45. }
  46. }
  47. abstract class CardAccessory implements Widget {
  48. void onTap(BuildContext context);
  49. }
  50. typedef CardAccessoryBuilder = List<CardAccessory> Function(
  51. BuildContext buildContext,
  52. );
  53. class CardAccessoryContainer extends StatelessWidget {
  54. final List<CardAccessory> accessories;
  55. const CardAccessoryContainer({required this.accessories, Key? key})
  56. : super(key: key);
  57. @override
  58. Widget build(BuildContext context) {
  59. final theme = context.read<AppTheme>();
  60. final children = accessories.map((accessory) {
  61. final hover = FlowyHover(
  62. style: HoverStyle(
  63. hoverColor: theme.hover,
  64. backgroundColor: theme.surface,
  65. ),
  66. builder: (_, onHover) => Container(
  67. width: 26,
  68. height: 26,
  69. padding: const EdgeInsets.all(3),
  70. decoration: _makeBoxDecoration(context),
  71. child: accessory,
  72. ),
  73. );
  74. return GestureDetector(
  75. child: hover,
  76. behavior: HitTestBehavior.opaque,
  77. onTap: () => accessory.onTap(context),
  78. );
  79. }).toList();
  80. return Wrap(children: children, spacing: 6);
  81. }
  82. }
  83. BoxDecoration _makeBoxDecoration(BuildContext context) {
  84. final theme = context.read<AppTheme>();
  85. final borderSide = BorderSide(color: theme.shader6, width: 1.0);
  86. return BoxDecoration(
  87. color: Colors.transparent,
  88. border: Border.fromBorderSide(borderSide),
  89. boxShadow: const [
  90. BoxShadow(
  91. color: Colors.transparent,
  92. spreadRadius: 0,
  93. blurRadius: 2,
  94. offset: Offset.zero,
  95. )
  96. ],
  97. borderRadius: const BorderRadius.all(Radius.circular(6)),
  98. );
  99. }
  100. class _CardEnterRegion extends StatelessWidget {
  101. final Widget child;
  102. final List<CardAccessory> accessories;
  103. const _CardEnterRegion(
  104. {required this.child, required this.accessories, Key? key})
  105. : super(key: key);
  106. @override
  107. Widget build(BuildContext context) {
  108. return Selector<_CardContainerNotifier, bool>(
  109. selector: (context, notifier) => notifier.onEnter,
  110. builder: (context, onEnter, _) {
  111. List<Widget> children = [child];
  112. if (onEnter) {
  113. children.add(CardAccessoryContainer(
  114. accessories: accessories,
  115. ).positioned(right: 0));
  116. }
  117. return MouseRegion(
  118. cursor: SystemMouseCursors.click,
  119. onEnter: (p) =>
  120. Provider.of<_CardContainerNotifier>(context, listen: false)
  121. .onEnter = true,
  122. onExit: (p) =>
  123. Provider.of<_CardContainerNotifier>(context, listen: false)
  124. .onEnter = false,
  125. child: IntrinsicHeight(
  126. child: Stack(
  127. alignment: AlignmentDirectional.topEnd,
  128. fit: StackFit.expand,
  129. children: children,
  130. )),
  131. );
  132. },
  133. );
  134. }
  135. }
  136. class _CardContainerNotifier extends ChangeNotifier {
  137. bool _onEnter = false;
  138. _CardContainerNotifier();
  139. set onEnter(bool value) {
  140. if (_onEnter != value) {
  141. _onEnter = value;
  142. notifyListeners();
  143. }
  144. }
  145. bool get onEnter => _onEnter;
  146. }