【问题标题】:Dynamic height for ListView Items in FlutterFlutter 中 ListView 项的动态高度
【发布时间】:2019-08-01 13:45:51
【问题描述】:

所以我正在尝试使用动态单元格高度的列表视图的颤动布局。

我已阅读https://medium.com/flutter-community/a-deep-dive-into-hero-widgets-in-flutter-d34f441eb026,据我了解,我已尝试实施。

Widget listLayout() {
    List<String> entries = <String>[
      'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
      'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using',
      'Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections',
      'If you use this site regularly and would like to help keep the site on the Internet, please consider donating a small sum to help pay for the hosting and bandwidth bill. There is no minimum donation, any sum is appreciated - click here to donate using PayPal. Thank you for your support.',
      'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum0',
      'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?',
      'But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?'
    ];
    return ListView.separated(
        padding: const EdgeInsets.all(8.0),
        itemCount: entries.length,
        separatorBuilder: (context, index) => Divider(
              color: Colors.black,
              height: 4.0,
            ),
        itemBuilder: (BuildContext context, int index) {
          return Stack(
            children: <Widget>[
              Row(
                children: <Widget>[
                  //getTheImageLayout(childHeight),
                  getTheTextLayout(entries[index])],
              )
            ],
          );
        });
  }

  Widget getTheTextLayout( String text) {
    return Expanded(
      flex: 8,
      child: Padding(
            padding: EdgeInsets.fromLTRB(10, 2.5, 10, 2),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Expanded(
                  child: Text(
                    text,
                    style: TextStyle(
                      background: Paint()..color = Colors.orange,
                    ),
                  ),
                  flex: 9,
                ),
                Expanded(
                  child: Text(
                    'Date of sending',
                    style: TextStyle(
                      background: Paint()..color = Colors.blue,
                    ),
                  ),
                  flex: 1,
                )
              ],
            ),
          ),
    );
  }

不幸的是,这给了我一个错误,that the RenderFlex children have non-zero flex but incoming height constraints are unbounded.

我正在尝试模仿这种布局(图像部分尚未开始,我正在尝试创建类似于自定义 UITableViewCell 的内容)。

** 这个想法是 tableViewCell 将根据提供给黄色标签的文本增加它的高度,底部标签将主要包含日期文本。

【问题讨论】:

    标签: flutter dart widget


    【解决方案1】:

    只需删除 Column 中的 Expanded 小部件,我注意到您使用的是 Stack,您不需要这种情况:

      Widget listLayout() {
        List<String> entries = <String>[
          'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
          'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using',
          'Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections',
          'If you use this site regularly and would like to help keep the site on the Internet, please consider donating a small sum to help pay for the hosting and bandwidth bill. There is no minimum donation, any sum is appreciated - click here to donate using PayPal. Thank you for your support.',
          'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum0',
          'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?',
          'But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?'
        ];
        return ListView.separated(
          padding: const EdgeInsets.all(8.0),
          itemCount: entries.length,
          separatorBuilder: (context, index) => Divider(
            color: Colors.black,
            height: 4.0,
          ),
          itemBuilder: (BuildContext context, int index) {
            return Row(
              children: <Widget>[
                Expanded(
                  flex: 2,
                  child: IntrinsicHeight(
                    child: Container(
                      color: Colors.blue,
                      child: Text("Image here"),
                    ),
                  ),
                ),
                getTheTextLayout(entries[index])
              ],
            );
          },
        );
      }
    
      Widget getTheTextLayout(String text) {
        return Expanded(
          flex: 8,
          child: Padding(
            padding: EdgeInsets.fromLTRB(10, 2.5, 10, 2),
            child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      Text(
                        text,
                        style: TextStyle(
                          background: Paint()..color = Colors.orange,
                        ),
                      ),
                      Text(
                        'Date of sending',
                        textAlign: TextAlign.end,
                        style: TextStyle(
                          background: Paint()..color = Colors.blue,
                        ),
                      )
                    ],
                  ),
          ),
        );
      }
    
    
    

    【讨论】:

    • 谢谢。所以我猜想,Column 的 Expanded Child 和 Stack 当时正在制造冲突。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 2019-09-27
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2020-06-28
    相关资源
    最近更新 更多