card_container.dart 4.7 KB

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