【问题标题】:Flutter ListWheelScrollView.useDelegate Make Auto Scroll for few secondsFlutter ListWheelScrollView.useDelegate 使自动滚动几秒钟
【发布时间】:2021-09-07 10:18:42
【问题描述】:

我想在 Flutter ListWheelScrollView.useDelegate 中添加一个自动滚动系统几秒钟。我的 ListWheelChildLoopingListDelegate 正在生成一个无限小部件循环。

是否可以通过单击按钮来滚动此循环几秒钟?

我的代码在这里:

return Container(
  height: 125,
  width: ScreenSize.width * 0.3,
  child: ListWheelScrollView.useDelegate(
    diameterRatio: 1,
    squeeze: 1.8,
    itemExtent: 75,
    physics: FixedExtentScrollPhysics(),
    onSelectedItemChanged: (index) => print(index),
    childDelegate: ListWheelChildLoopingListDelegate(
      children: List<Widget>.generate(
        slotNames.length,
        (index) => Padding(
          padding: const EdgeInsets.all(3.0),
          child: Container(
            decoration: BoxDecoration(
              border: Border.all(
                color: Colors.blue,
              ),
              borderRadius: BorderRadius.circular(10.0),
            ),
            child: Image.asset(
              "assets/$index.png",
              width: double.infinity,
              height: double.infinity,
            ),
          ),
        ),
      ),
    ),
  ),
);

当前状态示例:

【问题讨论】:

    标签: flutter mobile-development flutter-design


    【解决方案1】:

    这很简单,你只需要为滚动控制器设置 Timer 和一个预定义的函数 animateToItem

    用初始项声明控制器

    final _controller = FixedExtentScrollController(initialItem: 0);
    

    声明计时器

     Timer upperSliderTimer;
    

    也为计时器导入 'dart:async'

    在我的例子中创建一个启动动画的函数,我只是在启动函数中调用它

     void startController() async {
    int totalitems = 4; //total length of items
    int counter = 0;
    if (counter <= totalitems) {
      upperSliderTimer = Timer.periodic(Duration(seconds: 3), (timer) {
        _controller.animateToItem(counter,
            duration: Duration(seconds: 1), curve: Curves.easeInCubic);
        counter++;
        if (counter == totalitems) counter = 0;
      });
    }
    

    在initState中调用上述函数

    @override
    void initState() {
        super.initState();
        startController();
      }
    

    最后是 ListWheelScrollView 和 Delegate

    ListWheelScrollView.useDelegate(
                          itemExtent: size.height * 0.4,
                          renderChildrenOutsideViewport: false,
                          controller: _controller,
                          squeeze: 0.7,
                          //useMagnifier: true,
                          childDelegate: ListWheelChildBuilderDelegate(
                              childCount: totalitems,
                              builder: (BuildContext context, int itemIndex) {
                                try {
                                  return Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Container(
                                      decoration: BoxDecoration(
                                        boxShadow: [
                                          BoxShadow(
                                            color: Colors.black12,
                                            offset: Offset(2, 3),
                                            blurRadius: 3,
                                            spreadRadius: 3,
                                          )
                                        ],
                                        borderRadius: BorderRadius.circular(5),
                                        image: DecorationImage(
                                          fit: BoxFit.fill,
                                          image: NetworkImage(
                                              'image url'),
                                        ),
                                      ),
                                    ),
                                  );
                                } catch (e) {
                                  return Container(
                                    decoration: BoxDecoration(
                                      boxShadow: [
                                        BoxShadow(
                                          color: Colors.black12,
                                          offset: Offset(2, 3),
                                          blurRadius: 3,
                                          spreadRadius: 3,
                                        )
                                      ],
                                      borderRadius: BorderRadius.circular(5),
                                      image: DecorationImage(
                                        fit: BoxFit.fill,
                                        image: AssetImage(
                                            'defualt assets image'),
                                      ),
                                    ),
                                  );
                                }
                              }),
                        );
    

    【讨论】:

    • 感谢您的回复....我还找到了一个更简单的解决方案,使用提供者并随机创建列表。
    猜你喜欢
    • 1970-01-01
    • 2015-06-03
    • 2015-06-28
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多