import 'package:flutter/material.dart';

class PageTransformer extends StatelessWidget {
  final List<Widget> pages;
  final PageController controller;

  const PageTransformer({
    super.key,
    required this.pages,
    required this.controller,
  });

  @override
  Widget build(BuildContext context) {
    return PageView.builder(
      controller: controller,
      itemCount: pages.length,
      itemBuilder: (context, index) {
        return _buildPage(pages[index], index);
      },
    );
  }

  Widget _buildPage(Widget page, int index) {
    return AnimatedBuilder(
      animation: controller,
      builder: (context, child) {
        double value = 1.0;
        if (controller.position.haveDimensions) {
          value = controller.page! - index;
          value = (1 - (value.abs() * 0.5)).clamp(0.0, 1.0);
        }
        return Transform(
          transform: Matrix4.identity()..scale(value, value),
          alignment: Alignment.center,
          child: child,
        );
      },
      child: page,
    );
  }
}
