【问题标题】:Flutter Web sliver: customscrollviewFlutter Web 条子:customscrollview
【发布时间】:2021-05-16 02:30:12
【问题描述】:

我正在制作一个颤动的网络。 我想在 CustomScrollView 中调整水平图像的大小。

例如:在产品详细信息页面上,我想将 600x400 宽度的顶部图像放置在布局的中心。

无论图像大小如何,水平扩展至 900 倍或更多都是一个问题。

布局:

代码:

class ServiceProductDetailPage extends StatelessWidget {
  final String productId;
  final ServiceProductModel product;
  ServiceProductDetailPage({required this.product, required this.productId});    
  @override
  Widget build(BuildContext context) {    
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.transparent,
      body: CustomScrollView(
        slivers: [      
          SliverAppBar(
            pinned: true,
            expandedHeight: 50,
            title: Text('Title'),
            stretch: true,
            backgroundColor: Colors.black,
          ),      
          SliverToBoxAdapter(child: _Body(context, _product)),        
        ],
      ),
    );
  }
}

Widget _Body(BuildContext context, ServiceProductModel product) {
  return Card(
    child: ConstrainedBox(
      constraints: BoxConstraints.tight(Size(900,400)
      ),
      child: LoadingImage(url: product.image),
    ),
  );
}

class LoadingImage extends StatelessWidget {
  const LoadingImage({
    Key? key,
    required this.url,
  }) : super(key: key);

  final String url;
  @override
  Widget build(BuildContext context) {
    return Image.network(
      url,
      fit: BoxFit.cover,
      width: double.infinity,
      height: double.infinity,
      errorBuilder: (BuildContext c, Object err, StackTrace? stack) {
        return const Center(child: Text('error'));},
      frameBuilder: (BuildContext c, Widget image, int? frame, bool sync){
        if (!sync && frame == null) {
          return const Center(child: CircularProgressIndicator());
        }
        return image;
      },
    );
  }
}

【问题讨论】:

  • 您好。您能否更好地解释您正在努力实现的目标以及您目前正在获得什么?
  • 我想创建一个中心宽度为 900x 的布局。当我运行这段代码时,图像宽度被扩大到最大。
  • 所以你希望你的图像只有 600*400 而不是扩展到 900。这就是你的意思吗?
  • 我想放大到 900x400 大小,但是图片会占据整个浏览器。扩展到 900 以上是个问题。我只希望它扩展到布局宽度的大小。
  • 你想让你的_Body小部件只有900,然后里面的图像需要扩展到900宽度,对吗?

标签: flutter layout


【解决方案1】:

您正在寻找的是UnconstrainedBox。根据文档,UnconstrainedBox 使其自身占据其父级的全部宽度,但它允许其 child 具有 child 决定的任何宽度。

所以,你可以这样使用它。

Widget _Body(BuildContext context, ServiceProductModel product) {
  return Card(
    child: UnconstrainedBox(
      child: Container(
        color: Colors.deepOrange,
        width: 900,
        height: 400,
        child: LoadingImage(url: product.image),
      ),
    ),
  );
}

接下来,你给了你一些属性Image.network,这使它扩展。

所以去掉下面的

fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,

然后添加这个

fit: BoxFit.contain,

我给你的900 宽度Container 一个color 以查看它保持在900,尽管窗口大于900

【讨论】:

  • UntrainedBox() 是一个很好的建议,但在其上方和下方放置其他小部件会引发错误。我想将包括图像在内的所有小部件放在布局的中心。它也应该是一个响应式网络。我会再测试一下。
  • 这应该仍然可以使用SliverLists 或其他Sliver 普通小部件的变体。您要放置什么以及即将发生什么错误?
  • 我想创建一个类似于 Figma 社区详情页面(figma.com/community/file/973059007705843962)的 UI。我还使用 SliverLists 测试了几个小部件的放置。该问题没有解决小部件偏离中心的问题。我想你会发现如果你多测试一点
猜你喜欢
  • 2019-02-07
  • 2021-03-08
  • 2020-06-19
  • 2018-12-21
  • 2022-12-28
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多