5

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

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,
    ));
}
}
4

2 回答 2

9

我对您的小部件进行了一些修改,以便为您提供一个完整的示例。您可以通过多种方式执行此操作,使用AnimationController本身,甚至与自定义动画结合使用,或者,您可以采用最快的方式来实现您想要实现的目标:使用等待 ax 持续时间的递归方法(时间站在单页),然后以一些新的持续时间为新页面设置动画。为此,您可以例如:

  1. 使您List在其state自身中可用以检索其长度。

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

  3. 确保在屏幕上渲染第一帧之后调用它,以防止在屏幕上渲染PageController之前访问它PageView,这可能是您不想要的。为此,您可以利用WidgetsBinding.instance.addPostFrameCallback

演示

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);
  }
}
于 2018-12-20T11:37:41.567 回答
0

使用带有 PageView 的蠕虫指示器来显示滚动进度

在您的颤振项目中添加 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和 SquareDotShape需要大小。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
  ),
),

在此处输入图像描述

于 2020-12-12T03:37:02.100 回答