android list单选框,flutter笔记(九) 单选框Radio、RadioListTile

之前学习了复选框和开关,还有个单选框,这里看一下。

一、Radio

还是先看constructor

Radio({

Key key,

@required T value,

@required T groupValue,

@required ValueChanged onChanged,

Color activeColor,

MaterialTapTargetSize materialTapTargetSize

})

复制代码

这个Widget的constructor很简单,这几个属性前边都介绍过,怎么用看demo。

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

// This widget is the root of your application.

@override

Widget build(BuildContext context) {

return new MaterialApp(

home: Scaffold(

body: RadioDemo()

)

);

}

}

enum SingingCharacter { lafayette, jefferson }

class RadioDemo extends StatefulWidget {

@override

_RadioDemoState createState() => new _RadioDemoState();

}

class _RadioDemoState extends State {

SingingCharacter _character = SingingCharacter.lafayette;

@override

Widget build(BuildContext context) {

return Container(

padding: EdgeInsets.only(top: 20.0),

child: Column(

children: [

Radio(

value: SingingCharacter.lafayette,

groupValue: _character,

activeColor: Colors.red,

onChanged: (SingingCharacter value) {

print(value);

setState(() {

_character = value;

});

},

),

Radio(

value: SingingCharacter.jefferson,

groupValue: _character,

onChanged: (SingingCharacter value) {

print(value);

setState(() {

_character = value;

});

},

)

]

)

);

}

}

复制代码

这里有点陌生的东西,接下来看一下。

enum

Column

这里简单介绍一下,后边再详细说,这个Widget可以简单的理解成列表类似于html里边的ul,子节点是children一组Widget。

T

看constructor的时候应该就发现了,多了个,这个是泛型,<>里边是数据类型,例如想定义一个List里边只能放String,可以这样写

var str = new List();

复制代码

这里T是备用类型,实际上就是一个占位符。

二、RadioListTile

const RadioListTile({

Key key,

@required T value,

@required T groupValue,

@required ValueChanged onChanged,

Color activeColor,

Widget title,

Widget subtitle,

bool isThreeLine: false,

bool dense,

Widget secondary,

bool selected: false,

ListTitleControlAffinity controlAffinity: ListTileControlAffinity.platform

})

复制代码

demo

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

// This widget is the root of your application.

@override

Widget build(BuildContext context) {

return new MaterialApp(

home: Scaffold(

body: RadioDemo()

)

);

}

}

enum SingingCharacter { lafayette, jefferson }

class RadioDemo extends StatefulWidget {

@override

_RadioDemoState createState() => new _RadioDemoState();

}

class _RadioDemoState extends State {

SingingCharacter _character = SingingCharacter.lafayette;

@override

Widget build(BuildContext context) {

return Center(

child: Column(

children: [

RadioListTile(

value: SingingCharacter.lafayette,

title: Text('lafayette'),

groupValue: _character,

activeColor: Colors.red,

onChanged: (SingingCharacter value) {

print(value);

setState(() {

_character = value;

});

},

),

RadioListTile(

value: SingingCharacter.jefferson,

title: Text('jefferson'),

groupValue: _character,

onChanged: (SingingCharacter value) {

print(value);

setState(() {

_character = value;

});

},

)

]

)

);

}

}

复制代码

和单独的Radio相比基本上没啥区别。

相对于Radio多出来的属性看这里:flutter笔记(七)-----复选框CheckBox、CheckboxListTile

CheckboxList里边已经详细说过了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值