首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在flutter中实现这种类型的图像滑块

如何在flutter中实现这种类型的图像滑块
EN

Stack Overflow用户
提问于 2020-08-17 15:43:55
回答 1查看 1.6K关注 0票数 0

我需要像在图像中一样实现图像轮播。我已经完成了图像轮播的实现,没有在我的code.When循环部分中的循环头像部分被点击图像必须被更改

代码语言:javascript
复制
 Stack(
        children: [
          Container(
            width: width * 1,
            height: height * 1,

            child: PageView.builder(
                controller: _controller,
                scrollDirection: Axis.horizontal,
                itemCount: photos.length,
                itemBuilder: (context, photoIndex) {
                  return _buildImageState(photoIndex, width, height);
                }),
          ),
          SelectedPhoto(photoIndex: photoIndex,numberOfDots: photos.length,)
        ],
      ),
    );
  }

  Widget _buildImageState(int photoIndex, double width, double height) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.transparent,
        image: DecorationImage(
          image: AssetImage(photos[photoIndex]),
          fit: BoxFit.fill,
        ),
      ),
      child: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,



                  colors: [Colors.transparent, Colors.white],
                  stops: [
                  0.5,
                  0.75,
                  ]
              )
          )),
    );
  }
}
EN

回答 1

Stack Overflow用户

发布于 2020-08-17 16:00:18

让我给你一个想法。

在StateFul小部件中声明一个变量。int selectedIndex = 0;

而不是PageView构建器,尝试如下所示。

代码语言:javascript
复制
PageView( 
     controller: _controller,
     scrollDirection: Axis.horizontal,
     children: [for(int i=0; i< photos.length; i++) _buildImageState(i, width, height)],
),

SelectedPhoto小部件中,您可以这样做,

代码语言:javascript
复制
SelectedPhoto(photoIndex: selectedIndex ,numberOfDots: photos.length,)

在您的方法中,您可以使用GestureDetector小部件来处理点击事件。

代码语言:javascript
复制
Widget _buildImageState(int photoIndex, double width, double height) {
    return GestureDetector(
    onTap: () => {
        if (_controller.hasClients) {
            _controller.jumpToPage(photoIndex);
        }
       setState(() {
          selectedIndex = photoIndex
       });
    },
    child: Container(
      decoration: BoxDecoration(
        color: Colors.transparent,
        image: DecorationImage(
          image: AssetImage(photos[photoIndex]),
          fit: BoxFit.fill,
        ),
      ),
      child: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [Colors.transparent, Colors.white],
                  stops: [
                  0.5,
                  0.75,
                  ]
              )
           )
         ),
       )
    );
  }
}

希望这适合你的情况。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63446628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档