card_container.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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: theme.surface,
  88. border: Border.fromBorderSide(borderSide),
  89. boxShadow: [
  90. BoxShadow(
  91. color: theme.shader6,
  92. spreadRadius: 0,
  93. blurRadius: 2,
  94. offset: Offset.zero)
  95. ],
  96. borderRadius: const BorderRadius.all(Radius.circular(6)),
  97. );
  98. }
  99. class _CardEnterRegion extends StatelessWidget {
  100. final Widget child;
  101. final List<CardAccessory> accessories;
  102. const _CardEnterRegion(
  103. {required this.child, required this.accessories, Key? key})
  104. : super(key: key);
  105. @override
  106. Widget build(BuildContext context) {
  107. return Selector<_CardContainerNotifier, bool>(
  108. selector: (context, notifier) => notifier.onEnter,
  109. builder: (context, onEnter, _) {
  110. List<Widget> children = [child];
  111. if (onEnter) {
  112. children.add(CardAccessoryContainer(accessories: accessories)
  113. .positioned(right: 0));
  114. }
  115. return MouseRegion(
  116. cursor: SystemMouseCursors.click,
  117. onEnter: (p) =>
  118. Provider.of<_CardContainerNotifier>(context, listen: false)
  119. .onEnter = true,
  120. onExit: (p) =>
  121. Provider.of<_CardContainerNotifier>(context, listen: false)
  122. .onEnter = false,
  123. child: IntrinsicHeight(
  124. child: Stack(
  125. alignment: AlignmentDirectional.topEnd,
  126. fit: StackFit.expand,
  127. children: children,
  128. )),
  129. );
  130. },
  131. );
  132. }
  133. }
  134. class _CardContainerNotifier extends ChangeNotifier {
  135. bool _onEnter = false;
  136. _CardContainerNotifier();
  137. set onEnter(bool value) {
  138. if (_onEnter != value) {
  139. _onEnter = value;
  140. notifyListeners();
  141. }
  142. }
  143. bool get onEnter => _onEnter;
  144. }