123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import 'package:flutter/material.dart';
- import 'package:flutter/rendering.dart';
- import 'package:luojigou_thinking_core/luojigou_thinking_core.dart';
- class Demo3 extends StatefulWidget {
- const Demo3({Key? key}) : super(key: key);
- @override
- State<Demo3> createState() => _Demo3State();
- }
- class _Demo3State extends State<Demo3> {
- @override
- Widget build(BuildContext context) {
- return ColoredBox(
- color: Colors.black,
- child: SingleChildScrollView(
- child: Padding(
- padding: const EdgeInsets.only(top: 20, left: 20, right: 20),
- child: Column(
- children: [
- const SizedBox(height: 40),
- AspectRatio(
- aspectRatio: 3,
- child: ColoredBox(
- color: Colors.white,
- child: Trapezoid2Box(
- spacing: 20,
- diff: 15,
- child1: const _HitTestBox(
- size: Size(double.infinity, 300),
- ),
- child2: const _HitTestBox(
- size: Size(double.infinity, 300),
- ),
- ),
- ),
- ),
- TrapezoidBox(
- mainAxisSpacing: 16,
- padding: const EdgeInsets.symmetric(
- horizontal: 40,
- vertical: 60,
- ),
- background: Colors.white,
- children: const [
- _HitTestBox(
- size: Size(double.infinity, 300),
- ),
- _HitTestBox(
- size: Size(double.infinity, 100),
- ),
- _HitTestBox(
- size: Size(double.infinity, 200),
- ),
- _HitTestBox(
- size: Size(double.infinity, 400),
- ),
- _HitTestBox(
- size: Size(double.infinity, 200),
- ),
- _HitTestBox(
- size: Size(double.infinity, 200),
- ),
- ],
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
- class _HitTestBox extends LeafRenderObjectWidget {
- final Size size;
- const _HitTestBox({
- this.size = Size.zero,
- });
- @override
- RenderObject createRenderObject(BuildContext context) {
- return _RenderHitTestBox(size);
- }
- @override
- void updateRenderObject(
- BuildContext context, covariant _RenderHitTestBox renderObject) {
- renderObject.setSize(size);
- }
- }
- class _RenderHitTestBox extends RenderBox {
- _RenderHitTestBox(this._size);
- Color _color = Colors.deepPurple;
- Size _size;
- void setSize(Size value) {
- _size = value;
- markNeedsLayout();
- }
- @override
- void performResize() {
- assert(false);
- }
- @override
- void performLayout() {
- size = constraints.constrain(_size);
- }
- @override
- bool hitTestSelf(Offset position) => size.contains(position);
- @override
- void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
- super.handleEvent(event, entry);
- if (event is PointerDownEvent) {
- _color = Colors.yellow;
- } else if (event is PointerUpEvent) {
- _color = Colors.deepPurple;
- }
- markNeedsPaint();
- }
- @override
- void paint(PaintingContext context, Offset offset) {
- context.canvas.drawRect(offset & size, Paint()..color = _color);
- var tOffset = offset+Offset(10,10);
- var tSize = size + Offset(-20,-20);
- context.canvas.drawRect(tOffset & tSize, Paint()..color = Colors.green);
- }
- }
|