2

I am using PageView.builder and gradually increasing the viewportFraction after detecting scroll from _scrollController. video demonstrating the same

I want the left and right screen elements to fade away in a smoother manner, as we scroll downwards . Here, as I am changing the viewport gradually, the text is re-aligning which is giving a bad appearance and the way the left and right screen contents are going out of focus is giving a sudden expansion. I want this to be a smooth transition / animation.

Any idea how I can achieve this ? or any better way to achieve this ?

This is the PageView.builder with Pagecontroller :

class CarouselHolder extends StatefulWidget {
  const CarouselHolder({Key? key}) : super(key: key);

  @override
  _CarouselHolderState createState() => _CarouselHolderState();
}

class _CarouselHolderState extends State<CarouselHolder> {
  int currentPage = 0;

  double factor = 0.90;

  bool fullScreen = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Stack(
        children: [
          PageView.builder(
            itemBuilder: (context, i) {
              return Opacity(
                opacity: currentPage == i ? 1.0 : 0.8,
                child: DetailViewScreen(
                  onViewPortFactorChanged: (double val) {
                    setState(() {
                      // fullScreen = val == 1.0 ? true : false;
                      factor = val;
                    });
                  },
                ),
              );
            },
            itemCount: data.length,
            controller:
                PageController(initialPage: 0, viewportFraction: factor),
            onPageChanged: (i) {
              setState(() {
                factor = 0.90;
                currentPage = i;
              });
            },
          ),
        ],
      )),
    );
  }
}

And here, the scroll is detected and viewport is gradually increased

import 'package:flutter/material.dart';

class DetailViewScreen extends StatefulWidget {
  const DetailViewScreen({
    Key? key,
    this.onViewPortFactorChanged,
  }) : super(key: key);

  final Function(double)? onViewPortFactorChanged;

  @override
  State<DetailViewScreen> createState() => _DetailViewScreenState();
}

class _DetailViewScreenState extends State<DetailViewScreen> {
  ScrollController? _scrollController;

  _scrollListener() {
    for (int i = 1; i <= 20; i++) {
      if (_scrollController!.offset >= i * 20 &&
          _scrollController!.offset < 401.0) {
        widget.onViewPortFactorChanged!(0.90 + (i / 200));
      }
    }
  }

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    _scrollController!.addListener(_scrollListener);
  }

  @override
  void dispose() {
    _scrollController!.dispose();
    // _controller!.removeListener(_scrollListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      controller: _scrollController,
      child: detailViewScreenContent(),
    );
  }

  Widget detailViewScreenContent() {
    const verticalDividerSpace = SizedBox(height: 24);
    const String text =
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum';

    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: 400,
            color: Colors.yellow,
          ),
          verticalDividerSpace,
          const Text(text),
          verticalDividerSpace,
          Container(height: 300, color: Colors.pinkAccent),
          verticalDividerSpace,
          Container(
            height: 400,
            color: Colors.amberAccent,
          ),
          verticalDividerSpace,
          Container(
            height: 400,
            color: Colors.greenAccent,
          )
        ],
      ),
    );
  }
}

arnav_786
  • 21
  • 1

0 Answers0