转盘滑块宽度高度间距控制Flutter

o8x7eapl  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(104)

转盘滑块viewportfraction和纵横比。如何计算纵横比以获得预期的宽度和高度。我用的是carousel包。
我想这样实现(



)。我尝试了宽高比,视口分数,但我的尝试是不完美的工作。
下面是我的结果

。我在Chrome浏览器上运行。当viewportfractionaspect ratio不能达到预期的宽度和高度值时。如何计算aspect ratioviewportfraction
这是我的代码。

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

  @override
  State<UiDesignSlider> createState() => _UiDesignSliderState();
}

class _UiDesignSliderState extends State<UiDesignSlider> {
  final u = Get.find<UtilityController>();
  DateTime dateTime = DateTime.now();
  int current = 0;
  final CarouselController _controller = CarouselController();

  final List<String> sliderScreenList = [
    'challenge2',
    'challenge3',
    'challenge4',
  ];

  @override
  Widget build(BuildContext context) {
    bool isMobile = Responsive.isMobile();
    return Container(
      height: isMobile ? 250 : 350,
      width: u.size.width,
      color: Colors.green,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          const SizedBox(height: 30),
          Expanded(
            child: CarouselSlider(
              items: sliderScreenList.map((i) {
                return Builder(
                  builder: (BuildContext context) {
                    return Column(
                      children: [
                        Expanded(
                          child: Image.asset(
                            'assets/images/carousel/$i.png',
                          ),
                        ),
                      ],
                    );
                  },
                );
              }).toList(),
              carouselController: _controller,
              options: CarouselOptions(
                onPageChanged: (index, reason) {
                  setState(() {
                    current = index;
                  });
                },
              ),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: sliderScreenList.asMap().entries.map((entry) {
              return GestureDetector(
                onTap: () => _controller.animateToPage(entry.key),
                child: SlideOnOff(
                  isOn: current == entry.key ? true : false,
                  u: u,
                ),
              );
            }).toList(),
          ),
        ],
      ),
    );
  }
}
pcww981p

pcww981p1#

我自己有办法。这是我的解决方案。

CarouselSlider(
          items: sliderScreenList.map((i) {
            return Builder(
              builder: (BuildContext context) {
                return Padding(
                  padding:
                      EdgeInsets.symmetric(horizontal: isMobile ? 4 : 8),
                  child: Image.asset(
                    'assets/images/carousel/$i.png',
                  ),
                );
              },
            );
          }).toList(),
          carouselController: _controller,
          options: CarouselOptions(
            viewportFraction: 0.5,
            enlargeCenterPage: false,
            aspectRatio: 2.4,
            onPageChanged: (index, reason) {
              setState(() {
                current = index;
              });
            },
          ),
        )

相关问题