How to make ListWheelScrollView horizontal

后端 未结 3 1735
有刺的猬
有刺的猬 2021-01-18 04:07

I Am trying to have a horizontal ListView Widget that magnifies the center items. I tried using the normal ListView but I couldn\'t get the center

相关标签:
3条回答
  • 2021-01-18 04:34

    You can use this flutter package https://pub.dev/packages/carousel_slider. It also has a very helpful description and few samples to see how it looks. And it's compatible with dart 2.0 too.

    0 讨论(0)
  • 2021-01-18 04:35

    You can make this work with the help of ListView and PageView along with NotificationListener. Below is my code for the same-

    import 'dart:math';
    import 'package:flutter/material.dart';        
    const SCALE_FRACTION = 0.9;
            const FULL_SCALE = 1.0;
            final PAGER_HEIGHT = SizeConfig.screenHeight*0.32;
            const PAGER_WIDTH = double.infinity;
    
            class PaymentWidget extends StatefulWidget {
              @override
              State<StatefulWidget> createState() => _PaymentState();
            }
    
            class _PaymentState extends State<PaymentWidget> {
              double viewPortFraction = 0.9;
              int currentPage = 1;
              double page = 2.0;
    
              PageController pageController;
    
              final List<String> _cardsImages = ['image/path1', 'image/path2',
                'image/path3', 'image/path4'];
    
              @override
              void initState() {
                pageController = PageController(
                    initialPage: currentPage, viewportFraction: viewPortFraction);
                super.initState();
              }
    
              @override
              Widget build(BuildContext context) {
                return Scaffold(
                  appBar: null,
                  body: _creditCardsList()
            );
            }
    
           Widget _creditCardsList() {
                return ListView(
                  shrinkWrap: true,
                      children: <Widget>[
                        Container(
                          height: PAGER_HEIGHT,
                          child: NotificationListener<ScrollNotification>(
                            onNotification: (ScrollNotification notification) {
                              if (notification is ScrollUpdateNotification) {
                                setState(() {
                                  page = pageController.page;
                                });
                              }
                            },
                            child: PageView.builder(
                              onPageChanged: (pos) {
                                setState(() {
                                  currentPage = pos;
                                });
                              },
                              physics: BouncingScrollPhysics(),
                              controller: pageController,
                              itemCount: _cardsImages.length,
                              itemBuilder: (context, index) {
                                final scale =
                                max(SCALE_FRACTION, (FULL_SCALE - (index - page).abs()) + viewPortFraction);
                                return CreditCardTile(
                                    _cardsImages[index], scale);
                              },
                            ),
                          ),
                        ),
                      ],
                );
    
              }
    
            Widget CreditCardTile(String image, double scale) {
                return Align(
                  alignment: Alignment.bottomCenter,
                  child:Container(
                      height: PAGER_HEIGHT * scale,
                      width: PAGER_WIDTH * scale,
                      child: Card(
                        elevation: 5,
                        shadowColor: constColors.blueWhiteShade,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10.0)
                        ),
                        clipBehavior: Clip.antiAlias,
                        child: Image.asset(
                          image,
                          fit: BoxFit.cover,
                        ),
                      )
                  ) ,
                );
    
              }
    
    0 讨论(0)
  • 2021-01-18 04:42

    Edit: I have published package based on this.

    pub.dev/packages/list_wheel_scroll_view_x

    Here's my workaround.

    import 'package:flutter/material.dart';
    import 'package:flutter/rendering.dart';
    
    class ListWheelScrollViewX extends StatelessWidget {
      final Widget Function(BuildContext, int) builder;
      final Axis scrollDirection;
      final FixedExtentScrollController controller;
      final double itemExtent;
      final double diameterRatio;
      final void Function(int) onSelectedItemChanged;
      const ListWheelScrollViewX({
        Key key,
        @required this.builder,
        @required this.itemExtent,
        this.controller,
        this.onSelectedItemChanged,
        this.scrollDirection = Axis.vertical,
        this.diameterRatio = 100000,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return RotatedBox(
          quarterTurns: scrollDirection == Axis.horizontal ? 3 : 0,
          child: ListWheelScrollView.useDelegate(
            onSelectedItemChanged: onSelectedItemChanged,
            controller: controller,
            itemExtent: itemExtent,
            diameterRatio: diameterRatio,
            physics: FixedExtentScrollPhysics(),
            childDelegate: ListWheelChildBuilderDelegate(
              builder: (context, index) {
                return RotatedBox(
                  quarterTurns: scrollDirection == Axis.horizontal ? 1 : 0,
                  child: builder(context, index),
                );
              },
            ),
          ),
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题