Флаттер: как сделать ширину контейнера равной ширине стека? - PullRequest
0 голосов
/ 16 мая 2019

У меня есть Container, завернутый в Stack.Поскольку Stack будет иметь переменную ширину, я хотел бы установить ширину Container равную ширине Stack.

Я пробовал приведенный ниже код, но он не работает.

     Stack(
      alignment: Alignment.center,
      children: <Widget>[
        new Container(
          child: new CachedNetworkImage(
            fit: BoxFit.fitWidth,
            height: 270.0,
            imageUrl: _imageURL,
            placeholder: (context, url) {
                new CircularProgressIndicator();
            },
        ),

      new Container(
        constraints:  BoxConstraints.expand(height: 270.0), 
        decoration: new BoxDecoration(
          color: Colors.black45
        ),
      ),

в журнале консоли написано

I/flutter (14660): The following assertion was thrown during performLayout():
I/flutter (14660): BoxConstraints forces an infinite width.
I/flutter (14660): These invalid constraints were provided to RenderDecoratedBox's layout() function by the following
I/flutter (14660): function, which probably computed the invalid constraints in question:
I/flutter (14660):   RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:259:13)
I/flutter (14660): The offending constraints were:
I/flutter (14660):   BoxConstraints(w=Infinity, h=148.1)

Контейнер отображается только тогда, когда я даю определенную ширину и высоту внутри кода, подобного этому.

Stack(
      alignment: Alignment.center,
      children: <Widget>[
        new Container(
          child: new CachedNetworkImage(
            fit: BoxFit.fitWidth,
            height: 270.0,
            imageUrl: _imageURL,
            placeholder: (context, url) {
                new CircularProgressIndicator();
            },
        ),

      new Container(
        width: 100.0,
        height: 100.0,
        decoration: new BoxDecoration(
          color: Colors.black45
        ),
      ),

1 Ответ

1 голос
/ 16 мая 2019

Просто установите StackFit.expand на fit свойство вашего Stack:

  SizedBox(
     width : 300,
     height: 300,
     child:    Stack(
            fit: StackFit.expand,
            alignment: Alignment.center,
            children: <Widget>[
              new Container(
                 decoration: new BoxDecoration(color: Colors.black45),
              )
            ...
...