如何在Flutter中使用PageView创建转盘(滑动动画)?

时间:2018-12-20 10:37:39

标签: flutter widget flutter-animation

我想使用“图像”创建滑块动画,并且还希望允许用户使用滑动手势来回移动。另一个要求是页面指示器。为此,我使用了

  

page_indicator:^ 0.1.3

目前,我可以使用带有页面指示器的滑动手势在图像之间滑动,现在我想以x的持续时间重复对幻灯片进行动画处理。 我的代码在下面。

final PageController controller = new PageController();

@override
Widget build(BuildContext context) {
List<Widget> list = new List<Widget>();
list.add(new SliderBox(image: 'assets/shirt.png'));
list.add(new SliderBox(image: 'assets/laptops.png'));
list.add(new SliderBox(image: 'assets/bags.png'));

PageIndicatorContainer container = new PageIndicatorContainer(
  pageView: new PageView(
    children: list,
    controller: controller,
  ),
  length: 3,
  padding: EdgeInsets.fromLTRB(10, 40, 10, 10),
  indicatorSpace: 10,
  indicatorColor: Colors.grey[350],
  indicatorSelectorColor: Colors.grey,
);

return Stack(children: <Widget>[
  Container(color: Colors.grey[100], height: double.infinity),
  Container(
      color: Colors.white,
      child: container,
      margin: EdgeInsets.only(bottom: 50)),Text('$moveToPage')
]);


class SliderBox extends StatelessWidget {
final image;

const SliderBox({Key key, this.image}) : super(key: key);

@override
Widget build(BuildContext context) {
// TODO: implement build
return Container(
    padding: EdgeInsets.all(10),
    child: Image.asset(
      image,
      height: 300,
      fit: BoxFit.fill,
    ));
}
}

2 个答案:

答案 0 :(得分:2)

为了给您提供完整的示例,我对您的小部件进行了一些修改。您可以单独使用AnimationController甚至与自定义Animation结合使用多种方式,或者可以以最快的方式实现您想要实现的目标:使用递归方法等待斧头持续时间(时间停留在单个页面上),然后以新的持续时间为新页面设置动画。为此,您可以例如:

  1. 使List内的state可用,以获取其长度。

  2. 创建将处理动画本身的递归方法。

  3. 请确保在屏幕上呈现第一帧之后调用它,以防止在屏幕上呈现PageController之前访问PageView,而您可能不会这样做。不想。为此,您可以利用WidgetsBinding.instance.addPostFrameCallback

demo

class Carousel extends StatefulWidget {
  _CarouselState createState() => _CarouselState();
}

class _CarouselState extends State<Carousel> with SingleTickerProviderStateMixin {
  final PageController _controller = PageController();

  List<Widget> _list = [
    SliderBox(
        child: FlutterLogo(
      colors: Colors.red,
    )),
    SliderBox(
        child: FlutterLogo(
      colors: Colors.green,
    )),
    SliderBox(
        child: FlutterLogo(
      colors: Colors.blue,
    ))
  ];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) => _animateSlider());
  }

  void _animateSlider() {
    Future.delayed(Duration(seconds: 2)).then((_) {
      int nextPage = _controller.page.round() + 1;

      if (nextPage == _list.length) {
        nextPage = 0;
      }

      _controller
          .animateToPage(nextPage, duration: Duration(seconds: 1), curve: Curves.linear)
          .then((_) => _animateSlider());
    });
  }

  @override
  Widget build(BuildContext context) {
    PageIndicatorContainer container = new PageIndicatorContainer(
      pageView: new PageView(
        children: _list,
        controller: _controller,
      ),
      length: _list.length,
      padding: EdgeInsets.fromLTRB(10, 40, 10, 10),
      indicatorSpace: 10,
      indicatorColor: Colors.grey[350],
      indicatorSelectorColor: Colors.grey,
    );

    return Stack(
      children: <Widget>[
        Container(color: Colors.grey[100], height: double.infinity),
        Container(color: Colors.white, child: container, margin: EdgeInsets.only(bottom: 50)),
      ],
    );
  }
}

class SliderBox extends StatelessWidget {
  final Widget child;
  const SliderBox({Key key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(padding: EdgeInsets.all(10), child: child);
  }
}

答案 1 :(得分:0)

Worm Indicator与PageView一起使用以显示滚动进度

在flutter项目中添加lib依赖项。

dependencies:
  worm_indicator: 0.1.1

使用情况

WormIndicator(
  length: PAGE_VIEW_SIZE,
  controller: PAGE_VIEW_CONTROLLER,
  size: DOT_SIZE_IN_PT,
  spacing: DOT_SPACING_IN_PT,
),

Circle和正方形DotShape需要大小。 Rectangle DotShape需要宽度和高度。

WormIndicator(
  length: 3,
  controller: _controller,
  shape: Shape(
    size: 16,
    spacing: 8,
    shape: DotShape.Circle  // Similar for Square
  ),
),
WormIndicator(
  length: 3,
  controller: _controller,
  shape: Shape(
    width: 16,
    height: 24,
    spacing: 8,
    shape: DotShape.Rectangle
  ),
),

enter image description here