import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class GoodsInfo extends StatefulWidget {
var arguments;
GoodsInfo({this.arguments});
@override
_GoodsInfoState createState() => _GoodsInfoState(arguments:this.arguments);
}
class _GoodsInfoState extends State<GoodsInfo> {
var arguments;
bool checkBox;
var radioGroup;
var radioListTileGroup;
var switchValue;
TextEditingController _userNameController = TextEditingController(); //用于实现表单的默认值, TextField的controller参数需要接收这个类
String _userName = ""; //如果用userNameController.text 记录onChange的值, 表单会被不停的重置, 光标一直跑到首位, 所以用单个_userName记录onChange的值, 不用setState, 因为不用刷新页面
_GoodsInfoState({this.arguments});
@override
void initState() { //初始化函数, 在构造函数之后执行
// TODO: implement initState
super.initState();
this._userNameController.text = "表单的默认值";
this.switchValue = true;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GoodsInfo"),
),
body: Center(
child: Column(
children: [
Row(
children: [
Radio(
activeColor: Colors.lightBlueAccent,
value: 1, //当前这个radio的值
groupValue: this.radioGroup, //那个radio被选中就赋值radioGroup, Radio 通过对比groupValue和自己的value参数来决定自己是否被选中
onChanged: (value){
setState(() {
this.radioGroup = value;
});
}
),
Radio(
value: 2,
groupValue: this.radioGroup,
onChanged: (value){
setState(() {
this.radioGroup = value;
});
}
)
],
),
RadioListTile(
value: 1,
groupValue: this.radioListTileGroup,
onChanged: (value){
setState(() {
this.radioListTileGroup = value;
});
},
title: Text("标题"),
subtitle: Text("子标题"),
secondary: Icon(Icons.home),
),
RadioListTile(
value: 2,
groupValue: this.radioListTileGroup,
onChanged: (value){
setState(() {
this.radioListTileGroup = value;
});
},
title: Text("标-题"),
subtitle: Text("子-标-题"),
secondary: Icon(Icons.anchor),
selected: this.radioListTileGroup == 2 ? true : false,
),
Switch(
value: this.switchValue,
onChanged: (value){
setState(() {
this.switchValue = value;
});
}
),
SwitchListTile(
value: this.switchValue,
onChanged: (value){
setState(() {
this.switchValue = value;
});
},
title: Text("Switch标题"),
subtitle: Text("Switch副标题"),
activeThumbImage: NetworkImage("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3747190372,861288348&fm=26&gp=0.jpg"), //滑动按钮激活时的背景图片
inactiveThumbImage: NetworkImage("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3747190372,861288348&fm=26&gp=0.jpg"), //滑动按钮非激活时的背景图片
),
//沾满全屏幕
Container(
width: double.infinity, //宽度占满
child: RaisedButton(
color: Colors.orange,
child: Text("注册"),
onPressed: (){
print(this._userName);
},
),
),
],
),
),
);
}
}
Radio &Switch - flutter
最新推荐文章于 2023-11-24 09:32:54 发布