Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tapToSelect not working in some cases #13

Open
feinstein opened this issue Dec 31, 2024 · 0 comments
Open

tapToSelect not working in some cases #13

feinstein opened this issue Dec 31, 2024 · 0 comments

Comments

@feinstein
Copy link

feinstein commented Dec 31, 2024

tapToSelect is not working in some cases, I can't figure out what it could be, here's a simple reproducible example:

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_custom_carousel/flutter_custom_carousel.dart';

class Carousel extends StatelessWidget {
  const Carousel({
    required this.children,
    super.key,
  });

  final List<Widget> children;

  @override
  Widget build(BuildContext context) {
   double heightToTranslation(double scrollRatio) => scrollRatio * 60;

    double scrollToScale(double scrollRatio) {
      const min = 0.8;
      const max = 1.0;

      return (min + (scrollRatio + 1) * (max - min)).clamp(min, max);
    }

    return LayoutBuilder(
      builder: (context, constraints) {
        return CustomCarousel(
          onSelectedItemChanged: (item) => print('item: $item'),
          itemCountBefore: 2,
          itemCountAfter: 0,
          loop: true,
          effectsBuilder: (int index, double scrollRatio, Widget child) {
            final translation = heightToTranslation(scrollRatio);
            final scale = scrollToScale(scrollRatio);
            print('$index $scale $translation $scrollRatio');
            return Transform(
              transform: Matrix4.translationValues(0, translation, 0)..scale(scale, scale, 1),
              alignment: Alignment.topCenter,
              child: child,
            );
          },
          children: children,
        );
      },
    );
  }
}

class DemoCarouselPage extends StatelessWidget {
  const DemoCarouselPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(24),
      child: Center(
        child: SizedBox(
          height: 250,
          child: Carousel(
            children: [
              for (int i = 0; i < 15; i++)
                Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20),
                    border: Border.all(
                      color: Colors.black,
                      width: 1,
                    ),
                  ),
                  child: Center(
                    child: Text(
                      'Card $i',
                      style: TextStyle(color: Colors.black),
                    ),
                  ),
                )
            ],
          ),
        ),
      ),
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant