【问题标题】:How to change CheckboxListTile height?如何更改 CheckboxListTile 高度?
【发布时间】:2020-12-19 01:45:57
【问题描述】:

简而言之,我需要选择区域的高度更小。

文本和复选框的大小很好,但周围的框对于我要创建的清单来说太大了。

CheckBoxListTile current height vs. desired height

尝试用 Transform.scale 包裹它,但文本变得太小:

Transform.scale(
  scale: 0.8,
  child: CheckboxListTile(
    title: const Text("Rinite alérgica"),
    value: timeDilation != 1.0,
    controlAffinity: ListTileControlAffinity.leading,
    onChanged: (bool value) {
      setState(() {
      timeDilation = value ? 2.0 : 1.0;
      });
    },
  ),
),

尝试用 Container 包装它,但我收到屏幕溢出警告。

谁有更好的解决方案?

【问题讨论】:

    标签: flutter dart widget


    【解决方案1】:

    在尝试调整 CheckboxListFile 的大小时,Google 似乎实际上建议创建自定义 Tile 小部件并使用 Checkbox 小部件。

    https://api.flutter.dev/flutter/material/CheckboxListTile-class.html#material.CheckboxListTile.3

    查看已创建的 LabeledCheckbox 小部件。您可以非常轻松地修改所有组件以满足您的需求。例如,如果您希望 Widget 本身更小,您现在可以将其包装在 Container 中

    /// Flutter code sample for CheckboxListTile
    
    // ![Custom checkbox list tile sample](https://flutter.github.io/assets-for-api-docs/assets/material/checkbox_list_tile_custom.png)
    //
    // Here is an example of a custom LabeledCheckbox widget, but you can easily
    // make your own configurable widget.
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    /// This is the main application widget.
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      static const String _title = 'Flutter Code Sample';
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: _title,
          home: Scaffold(
            appBar: AppBar(title: const Text(_title)),
            body: const Center(
              child: MyStatefulWidget(),
            ),
          ),
        );
      }
    }
    
    class LabeledCheckbox extends StatelessWidget {
      const LabeledCheckbox({
        Key key,
        @required this.label,
        @required this.padding,
        @required this.value,
        @required this.onChanged,
      }) : super(key: key);
    
      final String label;
      final EdgeInsets padding;
      final bool value;
      final Function onChanged;
    
      @override
      Widget build(BuildContext context) {
        return InkWell(
          onTap: () {
            onChanged(!value);
          },
          child: Container(
            padding: padding,
            child: Row(
              children: <Widget>[
                Expanded(child: Text(label)),
                Checkbox(
                  value: value,
                  onChanged: (bool newValue) {
                    onChanged(newValue);
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
    
    /// This is the stateful widget that the main application instantiates.
    class MyStatefulWidget extends StatefulWidget {
      const MyStatefulWidget({Key? key}) : super(key: key);
    
      @override
      State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
    }
    
    /// This is the private State class that goes with MyStatefulWidget.
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      bool _isSelected = false;
    
      @override
      Widget build(BuildContext context) {
        return LabeledCheckbox(
          label: 'This is the label text',
          padding: const EdgeInsets.symmetric(horizontal: 20.0),
          value: _isSelected,
          onChanged: (bool newValue) {
            setState(() {
              _isSelected = newValue;
            });
          },
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      你试过了吗?

      SizedBox(
        height: 50,
        width: 50,
        child: ......
      )
      

      【讨论】:

      • 您好!谢谢您的回答 :)。刚刚试了一下,this is the result。它接近我想要的,剩下的就是弄清楚如何垂直集中复选框和文本。用 Padding 包装 CheckboxListTile 并在 SizedBox provides the following outcome 中使用 EdgeInsets.fromLTRB(0, 0, 0, 20)。
      • 我只是无缘无故地添加了卡片。 `````` SizedBox(高度:60,宽度:230,子:Card(高度:10.0,子:CheckboxListTile(标题:const Text(“Rinite alérgica”),值:true,controlAffinity:ListTileControlAffinity.leading,onChanged : (bool value) {}, ), ), ), `````` 这看起来 link 像这样。
      • 感谢您的回答!很抱歉花了这么长时间给你反馈,我的一周有点艰难。更改高度值后,您的建议looks like this。仍然没有垂直居中:(
      • link 我的看起来不错;
      • 确实如此!但是假设你想使它成为this big。你会怎么做?这是我的问题:P
      猜你喜欢
      • 2020-02-12
      • 2014-09-21
      • 1970-01-01
      • 2021-08-22
      • 2021-09-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多