栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Flutter:自定义单选按钮

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter:自定义单选按钮

这是完整的代码

class CustomRadio extends StatefulWidget {  @override  createState() {    return new CustomRadioState();  }}class CustomRadioState extends State<CustomRadio> {  List<RadioModel> sampleData = new List<RadioModel>();  @override  void initState() {    // TODO: implement 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: new ListView.builder(        itemCount: sampleData.length,        itemBuilder: (BuildContext context, int index) {          return new InkWell( //highlightColor: Colors.red, splashColor: Colors.blueAccent, onTap: () {   setState(() {     sampleData.forEach((element) => element.isSelected = false);     sampleData[index].isSelected = true;   }); }, child: new RadioItem(sampleData[index]),          );        },      ),    );  }}class RadioItem extends StatelessWidget {  final RadioModel _item;  RadioItem(this._item);  @override  Widget build(BuildContext context) {    return new Container(      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);}

To use :

void main() {  runApp(new MaterialApp(    home: new CustomRadio(),  ));}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366909.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号