Horizontally scrollable cards with Snap effect in flutter

前端 未结 3 726
广开言路
广开言路 2020-12-04 07:57

I want to create a list of cards scrolling horizontally with snap to fit effect when swiped either from left or right.

Each card has some spacing between them and fi

3条回答
  •  甜味超标
    2020-12-04 08:38

    Screenshot:


    If you don't want to use any 3rd party packages, you can simply try this:

    class _HomePageState extends State {
      int _index = 0;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: Center(
            child: SizedBox(
              height: 200, // card height
              child: PageView.builder(
                itemCount: 10,
                controller: PageController(viewportFraction: 0.7),
                onPageChanged: (int index) => setState(() => _index = index),
                itemBuilder: (_, i) {
                  return Transform.scale(
                    scale: i == _index ? 1 : 0.9,
                    child: Card(
                      elevation: 6,
                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                      child: Center(
                        child: Text(
                          "Card ${i + 1}",
                          style: TextStyle(fontSize: 32),
                        ),
                      ),
                    ),
                  );
                },
              ),
            ),
          ),
        );
      }
    }
    

提交回复
热议问题