【问题标题】:Flutter RadioListTile change Radio(icon) colorFlutter RadioListTile 更改 Radio(icon) 颜色
【发布时间】:2021-05-26 10:34:24
【问题描述】:

我无法更改 Radio List Tile 图标颜色。 我尝试使用 ListTileTheme 但没有奏效。 顺便说一句,我在对话框中使用 RadioListTile,但我认为这不会影响它。

代码。

ListTileTheme(
   iconColor: AppColors.green,
   textColor: AppColors.green,
   child: SimpleDialog(
   shape:
   RoundedRectangleBorder(
   borderRadius:
   BorderRadius
   .circular(10),
   ),
   title: Text(
   "Select Restaurant",
   style: Theme.of(context)
   .textTheme
   .headline3,
   textAlign:
   TextAlign.center,
   ),
   children: [
   Divider(),
   RadioListTile(
   title: const Text(
   'Name and Address'),
   value: 1,
   groupValue:
   _isRadioSelected,
   onChanged: (v) {
   setState(() {
   _isRadioSelected =
   v;
   });
   },
   ),
   ],
   ),
   );

【问题讨论】:

    标签: flutter radio


    【解决方案1】:

    您可以使用以下代码构建自定义的简单单选列表小部件。

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(new MaterialApp(
        home: new MyApp(),
      ));
    }
    
    class GroupModel {
      String text;
      int index;
      bool selected;
    
      GroupModel({this.text, this.index, this.selected});
    }
    
    class MyApp extends StatefulWidget {
      @override
      _State createState() => new _State();
    }
    
    class _State extends State<MyApp> {
      int _value2 = 0;
      List<GroupModel> _group = [
        GroupModel(text: "Item 1", index: 1, selected: true),
        GroupModel(text: "Item 2", index: 2, selected: false),
        GroupModel(text: "Item 3", index: 3, selected: false),
      ];
    
      Widget makeRadioTileList() {
        List<Widget> list = new List<Widget>();
    
        for (int i = 0; i < _group.length; i++) {
          list.add(new RadioListTile(
            value: _group[i].index,
            groupValue: _value2,
            selected: _group[i].selected,
            onChanged: (val) {
              setState(() {
                for (int i = 0; i < _group.length; i++) {
                  _group[i].selected = false;
                }
                _value2 = val;
                _group[i].selected = true;
              });
            },
            activeColor: Colors.purple,
            controlAffinity: ListTileControlAffinity.trailing,
            title: new Text(
              ' ${_group[i].text}',
              style: TextStyle(
                  color: _group[i].selected ? Colors.black : Colors.grey,
                  fontWeight:
                      _group[i].selected ? FontWeight.bold : FontWeight.normal),
            ),
          ));
        }
    
        Column column = new Column(
          children: list,
        );
        return column;
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('RadioListTile Example'),
          ),
          body: new Container(
            padding: new EdgeInsets.all(30.0),
            child: new Center(
              child: new Column(
                children: <Widget>[makeRadioTileList()],
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:
       Theme(
              data: Theme.of(context).copyWith(
                  unselectedWidgetColor: Colors.green,
                  disabledColor: Colors.green
              ),
              child: RadioListTile(
                title: const Text('Name and Address'),
                value: 1,
                groupValue: _isRadioSelected,
                onChanged: (v) {
                  setState(() {
                    _isRadioSelected = v;
                  });
                },
              ),
            )
      

      使用主题更改当前小部件中的一些主题参数。

      【讨论】:

      • unselectedWidgetColor 和 disabledColor 不起作用。实际上,它适用于更改 toggleableActiveColor 属性。
      猜你喜欢
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 2019-06-23
      • 2021-04-24
      • 2021-05-18
      相关资源
      最近更新 更多