如何使用RaisedButton为RadioListTile提供背景颜色?

时间:2018-09-18 16:07:13

标签: flutter flutter-layout

我一直在尝试通过单击RadioListTileRaisedButton赋予背景颜色,并创建一个函数来将该颜色分配给包含特定RadioListTile的{​​{1}} ,例如value。我正在使用value: 2
代码:

StatefulWidget

2 个答案:

答案 0 :(得分:0)

查看以下代码。

import 'package:flutter/material.dart';

  void main() {
    runApp(new MyApp());
  }

  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        title: 'Firebase Auth Demo',
        home: new CustomRadio(),
      );
    }
  }

  class CustomRadio extends StatefulWidget {
    @override
    createState() {
      return new CustomRadioState();
    }
  }

  class CustomRadioState extends State<CustomRadio> {
    List<RadioModel> sampleData = new List<RadioModel>();

    @override
    void initState() {
      super.initState();
      sampleData.add(new RadioModel(false, 'A', 'April 18'));
      sampleData.add(new RadioModel(false, 'B', 'April 17'));
      sampleData.add(new RadioModel(false, 'C', 'April 16'));
      sampleData.add(new RadioModel(false, 'D', 'April 15'));
    }


    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text("ListItem"),
        ),
        body: Scaffold(

          body: Column(
            children: <Widget>[
              RadioItem(sampleData[0]),
              RadioItem(sampleData[1]),
              RadioItem(sampleData[2]),
              RadioItem(sampleData[3]),
              RaisedButton(
                  child: Icon(Icons.add),
                  onPressed:(){
                    setState(() {
                      sampleData.forEach((element) => element.isSelected = false);
                      sampleData[0].isSelected = true;
                    });
                  }
              )
            ],
          ),
        ),
      );
    }
  }

  class RadioItem extends StatelessWidget {
    final RadioModel _item;
    RadioItem(this._item);
    @override
    Widget build(BuildContext context) {
      return new Container(
        color:  _item.isSelected ? Colors.green : Colors.white,
        margin: new EdgeInsets.all(15.0),
        child: new Row(
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
            new Container(
              height: 50.0,
              width: 50.0,
              child: new Center(
                child: new Text(_item.buttonText,
                    style: new TextStyle(
                        color:
                        _item.isSelected ? Colors.white : Colors.black,
                        //fontWeight: FontWeight.bold,
                        fontSize: 18.0)),
              ),
              decoration: new BoxDecoration(
                color: _item.isSelected
                    ? Colors.blueAccent
                    : Colors.transparent,
                border: new Border.all(
                    width: 1.0,
                    color: _item.isSelected
                        ? Colors.blueAccent
                        : Colors.grey),
                borderRadius: const BorderRadius.all(const Radius.circular(2.0)),
              ),
            ),
            new Container(
              margin: new EdgeInsets.only(left: 10.0),
              child: new Text(_item.text),
            )
          ],
        ),
      );
    }
  }

  class RadioModel {
    bool isSelected;
    final String buttonText;
    final String text;

    RadioModel(this.isSelected, this.buttonText, this.text);
  }

答案 1 :(得分:0)

RadioListTileContainer包裹在一起并为Container赋予颜色可以解决您的问题吗?

enter image description here