demo3.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/rendering.dart';
  3. import 'package:luojigou_thinking_core/luojigou_thinking_core.dart';
  4. class Demo3 extends StatefulWidget {
  5. const Demo3({Key? key}) : super(key: key);
  6. @override
  7. State<Demo3> createState() => _Demo3State();
  8. }
  9. class _Demo3State extends State<Demo3> {
  10. @override
  11. Widget build(BuildContext context) {
  12. return ColoredBox(
  13. color: Colors.black,
  14. child: SingleChildScrollView(
  15. child: Padding(
  16. padding: const EdgeInsets.only(top: 20, left: 20, right: 20),
  17. child: Column(
  18. children: [
  19. const SizedBox(height: 40),
  20. AspectRatio(
  21. aspectRatio: 3,
  22. child: ColoredBox(
  23. color: Colors.white,
  24. child: Trapezoid2Box(
  25. spacing: 20,
  26. diff: 15,
  27. child1: const _HitTestBox(
  28. size: Size(double.infinity, 300),
  29. ),
  30. child2: const _HitTestBox(
  31. size: Size(double.infinity, 300),
  32. ),
  33. ),
  34. ),
  35. ),
  36. TrapezoidBox(
  37. mainAxisSpacing: 16,
  38. padding: const EdgeInsets.symmetric(
  39. horizontal: 40,
  40. vertical: 60,
  41. ),
  42. background: Colors.white,
  43. children: const [
  44. _HitTestBox(
  45. size: Size(double.infinity, 300),
  46. ),
  47. _HitTestBox(
  48. size: Size(double.infinity, 100),
  49. ),
  50. _HitTestBox(
  51. size: Size(double.infinity, 200),
  52. ),
  53. _HitTestBox(
  54. size: Size(double.infinity, 400),
  55. ),
  56. _HitTestBox(
  57. size: Size(double.infinity, 200),
  58. ),
  59. _HitTestBox(
  60. size: Size(double.infinity, 200),
  61. ),
  62. ],
  63. ),
  64. ],
  65. ),
  66. ),
  67. ),
  68. );
  69. }
  70. }
  71. class _HitTestBox extends LeafRenderObjectWidget {
  72. final Size size;
  73. const _HitTestBox({
  74. this.size = Size.zero,
  75. });
  76. @override
  77. RenderObject createRenderObject(BuildContext context) {
  78. return _RenderHitTestBox(size);
  79. }
  80. @override
  81. void updateRenderObject(
  82. BuildContext context, covariant _RenderHitTestBox renderObject) {
  83. renderObject.setSize(size);
  84. }
  85. }
  86. class _RenderHitTestBox extends RenderBox {
  87. _RenderHitTestBox(this._size);
  88. Color _color = Colors.deepPurple;
  89. Size _size;
  90. void setSize(Size value) {
  91. _size = value;
  92. markNeedsLayout();
  93. }
  94. @override
  95. void performResize() {
  96. assert(false);
  97. }
  98. @override
  99. void performLayout() {
  100. size = constraints.constrain(_size);
  101. }
  102. @override
  103. bool hitTestSelf(Offset position) => size.contains(position);
  104. @override
  105. void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
  106. super.handleEvent(event, entry);
  107. if (event is PointerDownEvent) {
  108. _color = Colors.yellow;
  109. } else if (event is PointerUpEvent) {
  110. _color = Colors.deepPurple;
  111. }
  112. markNeedsPaint();
  113. }
  114. @override
  115. void paint(PaintingContext context, Offset offset) {
  116. context.canvas.drawRect(offset & size, Paint()..color = _color);
  117. var tOffset = offset+Offset(10,10);
  118. var tSize = size + Offset(-20,-20);
  119. context.canvas.drawRect(tOffset & tSize, Paint()..color = Colors.green);
  120. }
  121. }