animated_advanced_blend.dart 2.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

class _MultiplyPainter extends CustomPainter {
  _MultiplyPainter(this._color);

  final Color _color;

  @override
  void paint(Canvas canvas, Size size) {
    const int xDenominator = 2;
    const int yDenominator = 10;
    final double width = size.width / xDenominator;
    final double height = size.height / yDenominator;

    for (int y = 0; y < yDenominator; y++) {
      for (int x = 0; x < xDenominator; x++) {
        final Rect rect = Offset(x * width, y * height) & Size(width, height);
        final Paint basePaint = Paint()
          ..color = Color.fromARGB(
              (((x + 1) * width) / size.width * 255.0).floor(),
              (((y + 1) * height) / size.height * 255.0).floor(),
              255,
              127);
        canvas.drawRect(rect, basePaint);

        final Paint multiplyPaint = Paint()
          ..color = _color
          ..blendMode = BlendMode.multiply;
        canvas.drawRect(rect, multiplyPaint);
      }
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

class AnimatedAdvancedBlend extends StatefulWidget {
  const AnimatedAdvancedBlend({super.key});

  @override
  State<AnimatedAdvancedBlend> createState() => _AnimatedAdvancedBlendState();
}

class _AnimatedAdvancedBlendState extends State<AnimatedAdvancedBlend> with SingleTickerProviderStateMixin {
  late final AnimationController controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 5000));
  late final Animation<double> animation = controller.drive(Tween<double>(begin: 0.0, end: 1.0));
  Color _color = const Color.fromARGB(255, 255, 0, 255);

  @override
  void initState() {
    super.initState();
    controller.repeat();
    animation.addListener(() {
      setState(() {
        _color = Color.fromARGB((animation.value * 255).floor(), 255, 0, 255);
      });
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      body: CustomPaint(
        painter: _MultiplyPainter(_color),
        child: Container(),
      ),
    ));
  }
}