【问题标题】:How to set dynamic height to a PageView to show images in flutter?如何将动态高度设置为 PageView 以在颤动中显示图像?
【发布时间】:2020-11-23 18:23:08
【问题描述】:

我有一个显示图像的 PageView:

  int _immaginiIndice = 0;
  final PageController _immaginiContr = PageController();

Container(
                height: MediaQuery.of(context).size.height * 0.5,
                child: PageView.builder(
                  controller: _immaginiContr,
                  onPageChanged: (i) {
                    setState(() => _immaginiIndice = i);
                  },
                  itemCount: _questoLuogo.immagini.length,
                  itemBuilder: (_, int j) => Align(
                    alignment: Alignment.topLeft,
                    child: Container(
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          fit: BoxFit.cover,
                          image: NetworkImage(applicazione['urlimm'] + 
                              _questoLuogo.immagini.elementAt(j)),
                        ),
                      ),
                    ),
                  ),
                ),
              )

抱歉用我自己的语言命名变量。

容器的高度固定为设备的一半,但图像具有各种纵横比,因此有些看起来很糟糕。我想要的是将所有图像的宽度固定为 100%,但高度将成比例,因此纵向和横向图像看起来都很好。

我该怎么办?

【问题讨论】:

    标签: image flutter dart


    【解决方案1】:

    我认为这是因为您没有为第二个容器指定大小,而是尝试了类似的方法:

    对于 flex 为 1 的扩展小部件,两者都应该将您的屏幕一分为二。

    Column(
      children: <Widget>[
        Expanded(
            flex: 1,
            child: Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                    fit: BoxFit.cover,
                    image: AssetImage(
                      'images/backgroundImage.PNG',
                    )),
              ),
            )),
        Expanded(flex: 1, child: Container()),
      ],
    );
    

    【讨论】:

    • 第一个容器是 ListView 的子容器。我想删除它,但我收到错误。使用您的代码,我可以看到图像不是颗粒状的,而是被切割的。如果图像的高度超过了容器的高度,我想减小宽度以看到图像没有颗粒感和没有切割。
    猜你喜欢
    • 2020-01-10
    • 1970-01-01
    • 2023-04-07
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多