如何删除RadioListTile上的内部填充,以便连续使用3个RadioListTiles?

时间:2018-05-13 14:19:29

标签: dart flutter flutter-layout

我对Flutter和Dart很新,我似乎无法找到这个特定主题的任何提示。我试图将3 RadioListTiles放入Row,如此:

Row(
        children: [
          Expanded(
            child:RadioListTile<GoalSelection>(
              title: Text(
                'Net',
                style: Theme.of(context).textTheme.body1,
              ),
              value: GoalSelection.net,
              groupValue: _goalSelection,
              onChanged: (GoalSelection value) {
                setState(() {
                  _goalSelection = value;
                });
              },
            ),
          ),
          Expanded(
            child: RadioListTile<GoalSelection>(
              title: Text(
                'Gross',
                style: Theme.of(context).textTheme.body1,
              ),
              value: GoalSelection.gross,
              groupValue: _goalSelection,
              onChanged: (GoalSelection value) {
                setState(() {
                  _goalSelection = value;
                });
              },
            ),
          ),
          Expanded(
            child: RadioListTile<GoalSelection>(
              title: Text(
                'Salary',
                style: Theme.of(context).textTheme.body1,
              ),
              value: GoalSelection.salary,
              groupValue: _goalSelection,
              onChanged: (GoalSelection value) {
                setState(() {
                  _goalSelection = value;
                });
              },
            ),
          ),
        ],
      ),

按钮布局很好,但标签似乎浪费了很多空间。我放下了目前下面的截图。我尝试将ExpandedRadioListTileText放在Padding窗口小部件中(一次一个)来手动将填充设置为0,但它没有&做任何事情。我也尝试将Expanded更改为Flexible,即使我认为这不会改变任何内容。我现在不知所措。有没有办法让这个布局工作?我有点假设我正在做的事情真的很蠢。

Layout inspector showing padding

7 个答案:

答案 0 :(得分:2)

您可以连续使用RadioText小部件。但是Radio也有相同的填充问题。要删除填充,您可以将Radio放在SizedBox的子项中。

例如:-SizedBox(height: 20, width: 20, child: Radio(.......))

答案 1 :(得分:1)

您只需要将“ dense”属性设置为true,例如:

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")

答案 2 :(得分:0)

我们可以使用Flexible小部件控制RadioListTile的填充。如要在“行”小部件中安排3个RadioListTiles。请尝试使用以下代码,它将起作用。

 Row(
      children: <Widget>[
        Flexible(
          fit: FlexFit.loose,
          child:
            RadioListTile(
              title: const Text('hello'),
              onChanged: (value) {
                setState(() {});
              },
            ),
          ),
        Flexible(
          fit: FlexFit.loose,
          child:
            RadioListTile(
              title: const Text('Lafayette'),
              onChanged: (value) {
                setState(() {});
              },
            ),
        )
      ],
    ),

做,让我知道。一旦尝试了上面的代码。如果它解决了您的问题,请接受我的回答是有用的,并提供您宝贵的意见。

答案 3 :(得分:0)

我遇到了同样的问题。您可以尝试使用Radio,Text,InkWell,Padding进行自定义。

class LabeledRadio extends StatelessWidget {
  const LabeledRadio({
    this.label,
    this.padding,
    this.groupValue,
    this.value,
    this.onChanged,
  });

  final String label;
  final EdgeInsets padding;
  final bool groupValue;
  final bool value;
  final Function onChanged;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (value != groupValue)
          onChanged(value);
      },
      child: Padding(
        padding: padding,
        child: Row(
          children: <Widget>[
            Radio<bool>(
              groupValue: groupValue,
              value: value,
              onChanged: (bool newValue) {
                onChanged(newValue);
              },
            ),
            Text(label),
          ],
        ),
      ),
    );
  }
}

// ...

bool _isRadioSelected = false;

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <LabeledRadio>[
        LabeledRadio(
          label: 'This is the first label text',
          padding: const EdgeInsets.symmetric(horizontal: 5.0),
          value: true,
          groupValue: _isRadioSelected,
          onChanged: (bool newValue) {
            setState(() {
              _isRadioSelected = newValue;
            });
          },
        ),
        LabeledRadio(
          label: 'This is the second label text',
          padding: const EdgeInsets.symmetric(horizontal: 5.0),
          value: false,
          groupValue: _isRadioSelected,
          onChanged: (bool newValue) {
            setState(() {
              _isRadioSelected = newValue;
            });
          },
        ),
      ],
    ),
  );
}

文档:https://api.flutter.dev/flutter/material/RadioListTile-class.html#material.RadioListTile.3

这是我修复填充的方法:

enum ContactSex { nam, nu, khac }

class CreateContactScreen extends StatefulWidget {
  static const routeName = './create_contact';
  @override
  _CreateContactScreenState createState() => _CreateContactScreenState();
}

class _CreateContactScreenState extends State<CreateContactScreen> {
  ContactSex _contaxtSex = ContactSex.nu;
  final _form = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(
          'TẠO LIÊN HỆ',
          style: kHeaderTextStyle,
        ),
        actions: <Widget>[
          FlatButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text('XONG', style: TextStyle(color: Colors.white),),
          )
        ],
      ),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0),
        child: Form(
          key: _form,
          child: SingleChildScrollView(
              child: Column(
            children: <Widget>[
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'Tên*',
                ),
                textInputAction: TextInputAction.next,
                onFieldSubmitted: (_) {
                  // TODO: when submit this text field
                },
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Hãy nhập tên cho liên hệ.';
                  }
                  return null;
                },
                onSaved: (value) {
                  // TODO : when save the whole form
                },
              ),
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'Họ',
                ),
                textInputAction: TextInputAction.next,
                onFieldSubmitted: (_) {
                  // TODO: when submit this text field
                },
                // validator: (value) {
                //   if (value.isEmpty) {
                //     return null;
                //   }
                //   return null;
                // },
                onSaved: (value) {
                  // TODO : when save the whole form
                },
              ),
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'Số điện thoại*',
                ),
                keyboardType: TextInputType.phone,
                textInputAction: TextInputAction.next,
                onFieldSubmitted: (_) {
                  // TODO: when submit this text field
                },
                validator: (value) {
                  if (value.isEmpty) {
                    return 'Hãy nhập số điện thoại cho liên hệ.';
                  }
                  return null;
                },
                onSaved: (value) {
                  // TODO : when save the whole form
                },
              ),
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'Email',
                ),
                textInputAction: TextInputAction.next,
                onFieldSubmitted: (_) {
                  // TODO: when submit this text field
                },
                // validator: (value) {
                //   if (value.isEmpty) {
                //     return null;
                //   }
                //   return null;
                // },
                onSaved: (value) {
                  // TODO : when save the whole form
                },
              ),
              Container(
                padding: EdgeInsets.only(top: 15.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      'Giới tính',
                      style: TextStyle(fontSize: 14.0),
                    ),
                    Row(
                      mainAxisSize: MainAxisSize.max,
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        LabeledRadio(
                          label: 'Nữ',
                          padding: EdgeInsets.all(0),
                          groupValue: _contaxtSex,
                          value: ContactSex.nu,
                          onChanged: (ContactSex newValue) {
                            setState(() {
                              _contaxtSex = newValue;
                            });
                          },

                        ),LabeledRadio(
                          label: 'Nam',
                          padding: EdgeInsets.all(0),
                          groupValue: _contaxtSex,
                          value: ContactSex.nam,
                          onChanged: (ContactSex newValue) {
                            setState(() {
                              _contaxtSex = newValue;
                            });
                          },

                        ),LabeledRadio(
                          label: 'Khác',
                          padding: EdgeInsets.all(0),
                          groupValue: _contaxtSex,
                          value: ContactSex.khac,
                          onChanged: (ContactSex newValue) {
                            setState(() {
                              _contaxtSex = newValue;
                            });
                          },

                        ),
                      ],
                    ),
                  ],
                ),
              )
            ],
          )),
        ),
      ),
    );
  }
}

class LabeledRadio extends StatelessWidget {
  final String label;
  final EdgeInsets padding;
  final ContactSex groupValue;
  final ContactSex value;
  final Function onChanged;

  const LabeledRadio(
      {this.label, this.padding, this.groupValue, this.value, this.onChanged});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (value != groupValue) {
          onChanged(value);
        }
      },
      child: Padding(
        padding: padding,
        child: Row(
          children: <Widget>[
            Radio<ContactSex>(
              groupValue: groupValue,
              value: value,
              onChanged: (ContactSex newValue) {
                onChanged(newValue);
              },
            ),
            Text(label),
          ],
        ),
      ),
    );
  }
}

答案 4 :(得分:0)

复制RadioListTile代码并在新文件上创建您的代码并将其粘贴到其中。

删除引起错误的导入:

import 'package:flutter/widgets.dart'; // leave it
import 'package:flutter/material.dart'; //add
import 'list_tile.dart'; //remove
import 'radio.dart'; //remove
import 'theme.dart'; //remove
import 'theme_data.dart'; //remove

然后向其添加以下填充,如下所示:

//Inside the file locate this widget and Add the padding or remove it. I needed to remove it and add 5. 
   return MergeSemantics(
      child: ListTileTheme.merge(
        contentPadding: EdgeInsets.only( // Add this  
          left: 5,
          right: 0,
          bottom: 0,
          top: 0
        ),
        selectedColor: activeColor ?? Theme.of(context).accentColor,
        child: ListTile(
          leading: leading,
          title: title,
          subtitle: subtitle,
          trailing: trailing,
          isThreeLine: isThreeLine,
          dense: dense,
          enabled: onChanged != null,
          onTap: onChanged != null  && !checked ? () { onChanged(value); } : null,
          selected: selected,
        ),
      ),
    );

然后按如下所示将文件导入您的项目:

import 'package:Project_Name/common/customComponets/custom_radio_list_tile.dart' as CustomRadioListTile;

然后像这样使用它:

CustomRadioListTile.RadioListTile(); // and that's how I managed to do it. Thought I should share.

答案 5 :(得分:0)

你应该像这样手动实现 制作一组 Radio()Text() 并用 InkWell() 包裹以进行状态处理。现在通过 materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, 删除多余的收音机空间就是这样。通过示例代码获得灵感。

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    InkWell(
      onTap: () {
        setState(() {
          _radioVenue = 0;
        });
      },
      child: Row(
        children: [
          Radio(
            materialTapTargetSize:
                MaterialTapTargetSize.shrinkWrap,
            activeColor: primaryColor,
            groupValue: _radioVenue,
            onChanged: (value) {},
            value: 0,
          ),
          Text('From our list')
        ],
      ),
    ),
    InkWell(
      onTap: () {
        setState(() {
          _radioVenue = 1;
        });
      },
      child: Row(
        children: [
          Radio(
            materialTapTargetSize:
                MaterialTapTargetSize.shrinkWrap,
            activeColor: primaryColor,
            groupValue: _radioVenue,
            onChanged: (value) {},
            value: 1,
          ),
          Text('From our list')
        ],
      ),
    ),
  ],
),

我们在本示例中涵盖了这两个问题。

  1. 删除了多余的空格。
  2. 整个组是可选的单选 + 文本,现在它的行为类似于 RadioListTile()

答案 6 :(得分:-1)

只需将 SizedBox() 小部件包裹起来即可。

        SizedBox(
          height: 40,
          child: RadioListTile(
            value: user,
            groupValue: selectedUser,
            title: "First User",
            onChanged: (currentUser) {
             
            },
            selected: selectedUser == user,
            activeColor: Colors.green,
          ),