如何用颤振将行项目包装在卡片中

use*_*311 14 row word-wrap dart flutter

我有一张卡片,其中包含一行项目(文本和复选框小部件)。问题是卡片每行只能填满有限的空间,但不会进入该行的下一行。我尝试使用 wrap 小部件,但没有效果。我不断得到这个:

在此处输入图片说明

正如您所看到的,它并没有换行到下一行,而是试图将所有内容都放在那一行中。这是我的代码:

Widget _buildCategories() {
return Card(
    margin: const EdgeInsets.only(top: 20.0),
    child: Padding(
      padding: const EdgeInsets.all(20.0),
      child: Column(
        children: <Widget>[
          Text(
            'Categories',
            style: TextStyle(fontFamily: 'MonteSerrat', fontSize: 16.0),
          ),
          Wrap(
            children: <Widget>[
              Row(
            children: <Widget>[
              _checkBox('Gaming'),
              _checkBox('Sports'),
              _checkBox('Casual'),
              _checkBox('21 +'),
              _checkBox('Adult'),
              _checkBox('Food'),
              _checkBox('Club'),
              _checkBox('Activities'),
              _checkBox('Shopping'),
            ],
          )
            ],
          )
          
        ],
      ),
    ));
 }


Widget _checkBox(String category) {
return Expanded(
    child: Column(
  children: <Widget>[
    Text(
      '$category',
      textAlign: TextAlign.center,
      style: TextStyle(fontFamily: 'MonteSerrat'),
    ),
    Checkbox(
      value: false,
      onChanged: (value) {
        // We will update the value to the firebase data here
        print('updated value to: $value');
      },
    )
  ],
));
 }
Run Code Online (Sandbox Code Playgroud)

die*_*per 33

我通过以下更改修复了您的代码:

  • 删除了Row里面的小部件Wrap
  • 删除了Expanded小部件。
  • 将该属性添加maxLines到您的Text小部件。

         Widget _buildCategories() {
            return Card(
                margin: const EdgeInsets.only(top: 20.0),
                child: Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Column(
                    children: <Widget>[
                      Text(
                        'Categories',
                        style: TextStyle(fontFamily: 'MonteSerrat', fontSize: 16.0),
                      ),
                      Wrap(
                        children: <Widget>[
                          _checkBox('Gaming'),
                          _checkBox('Sports'),
                          _checkBox('Casual'),
                          _checkBox('21 +'),
                          _checkBox('Adult'),
                          _checkBox('Food'),
                          _checkBox('Club'),
                          _checkBox('Activities'),
                          _checkBox('Shopping')
                        ],
                      )
                    ],
                  ),
                ));
          }
    
          Widget _checkBox(String category) {
            return Column(
                  children: <Widget>[
                    Text(
                      '$category',
                      maxLines: 1,
                      textAlign: TextAlign.center,
                      style: TextStyle(fontFamily: 'MonteSerrat'),
                    ),
                    Checkbox(
                      value: false,
                      onChanged: (value) {
                        // We will update the value to the firebase data here
                        print('updated value to: $value');
                      },
                    )
                  ],
                );
          }
        } 
    
    Run Code Online (Sandbox Code Playgroud)

您还可以将属性runSpacing和添加spacing到您的Wrap小部件,以在水平和垂直方向的项目之间留出更多空间。

     Wrap(
            runSpacing: 5.0,
            spacing: 5.0,
Run Code Online (Sandbox Code Playgroud)

更多信息在这里:https : //docs.flutter.io/flutter/widgets/Wrap-class.html