如何在flutter中创建动画浏览量?

xqk2d5yq  于 2023-02-05  发布在  Flutter
关注(0)|答案(2)|浏览(116)

enter image description here
如何在flutter中创建曲线页面动画页面视图

kqhtkvqz

kqhtkvqz1#

创建一个页面视图控制器,其中包含一个整型变量作为有状态小部件中的索引,然后像这样初始化它们

PageController pageController;
  int currentPageIndex = 0;

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: currentPage);
  }

然后,您可以在PageView小部件中将它们与自定义页面一起使用

PageView(
   controller: pageController,
      children: [
        Container(
         color: Colors.yellow,
        ),
        Container(
         color: Colors.red,
        ),
        Container(
         color: Colors.blue,
        ),
        ],
        onPageChanged: (index) {
          setState(() {
           currentPageIndex = index;
          });
        },
)
mctunoxg

mctunoxg2#

请尝试此代码:

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

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

  @override
  State<PageviewAnimation> createState() => _PageviewAnimationState();
}

class _PageviewAnimationState extends State<PageviewAnimation> {
  PageController controller = PageController();
  static dynamic currentPageValue = 0.0;

  List pageViewItem = [
    page(currentPageValue, Colors.tealAccent),
    page(2, Colors.red),
    page(3, Colors.cyan)
  ];

  @override
  void initState() {
    super.initState();
    controller.addListener(() {
      setState(() {
        currentPageValue = controller.page;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text("Animation"),
        ),
        body: PageView.builder(
            itemCount: pageViewItem.length,
            scrollDirection: Axis.horizontal,
            controller: controller,
            itemBuilder: (context, position) {
              return Transform(
                transform: Matrix4.identity()
                  ..rotateX(currentPageValue - position),
                child: pageViewItem[position],
              );
            }),
      ),
    );
  }
}

Widget page(var pageno, Color color) {
  return Container(
    width: double.infinity,
    height: double.infinity,
    color: color,
    child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Icon(
          Icons.pages,
          color: Colors.white,
        ),
        Text("${pageno}, Swipe Right or left"),
        Icon(Icons.arrow_right, color: Colors.white),
      ],
    ),
  );
}

Here is video

相关问题