Flutter 如何从颤振中的值列表中获取值
我有三个容器,每个容器中有不同的信息(字符串和图标),我想把它们放在一列中 我想获得点击的值,并想在点击时更改容器的颜色,请问我该怎么做 另外,我想得到所选字符串的值 我认为这很简单,但我似乎没有找到一种方法来做到这一点 这是我的容器小部件:Flutter 如何从颤振中的值列表中获取值,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我有三个容器,每个容器中有不同的信息(字符串和图标),我想把它们放在一列中 我想获得点击的值,并想在点击时更改容器的颜色,请问我该怎么做 另外,我想得到所选字符串的值 我认为这很简单,但我似乎没有找到一种方法来做到这一点 这是我的容器小部件: class VisitTypeFees extends StatelessWidget { const VisitTypeFees({ Key key, this.onSelected = false, this.title,
class VisitTypeFees extends StatelessWidget {
const VisitTypeFees({
Key key,
this.onSelected = false,
this.title,
this.subTitle,
this.amount,
this.icon,
}) : super(key: key);
final bool onSelected;
final String title, subTitle, amount;
final IconData icon;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
height: getProportionateScreenHeight(65),
width: SizeConfig.screenWidth - 20,
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(10),
vertical: getProportionateScreenWidth(7.5),
),
decoration: BoxDecoration(
color: onSelected ? kSecondaryColor : Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [kDefaultShadow]
),
child: Row(
children: [
Container(
height: getProportionateScreenHeight(50),
width: getProportionateScreenHeight(50),
decoration: BoxDecoration(
color: onSelected
? Colors.white
: kPrimaryLightColor.withOpacity(0.6),
borderRadius: BorderRadius.circular(10)),
child: Icon(
icon,
color: onSelected ? kSecondaryColor : kPrimaryColor,
),
),
SizedBox(
width: getProportionateScreenWidth(7.5),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
title,
style: TextStyle(
color: onSelected ? Colors.white : Colors.black,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.w600),
),
Text(
subTitle,
style: TextStyle(
color: onSelected ? Colors.white : kTextColor,
fontSize: getProportionateScreenWidth(12),
fontWeight: FontWeight.w500),
),
],
),
Spacer(),
Text(
amount,
style: TextStyle(
color: onSelected ? Colors.white : kPrimaryColor,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.bold),
),
],
),
),
);
}
}
首先,您需要将小部件转换为StatefulWidget。然后,您需要为每个文本和图标小部件包装InkWell小部件。他们都有自己的财产 它将如下所示:
class VisitTypeFees extends StatefulWidget {
const VisitTypeFees({
Key key,
this.onSelected = false,
this.title,
this.subTitle,
this.amount,
this.icon,
}) : super(key: key);
final bool onSelected;
final String title, subTitle, amount;
final IconData icon;
@override
_VisitTypeFeesState createState() => _VisitTypeFeesState();
}
class _VisitTypeFeesState extends State<VisitTypeFees> {
bool iconSelected;
bool firstTextSelected;
bool seccondTextSelected;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
height: getProportionateScreenHeight(65),
width: SizeConfig.screenWidth - 20,
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(10),
vertical: getProportionateScreenWidth(7.5),
),
decoration: BoxDecoration(
color: widget.onSelected ? kSecondaryColor : Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [kDefaultShadow]),
child: Row(
children: [
InkWell(
onTap: () {
// icon selected!
setState(() {
iconSelected = true;
firstTextSelected = false;
seccondTextSelected = false;
});
},
child: Container(
height: getProportionateScreenHeight(50),
width: getProportionateScreenHeight(50),
decoration: BoxDecoration(
color: widget.onSelected
? Colors.white
: kPrimaryLightColor.withOpacity(0.6),
borderRadius: BorderRadius.circular(10)),
child: Icon(
widget.icon,
color: widget.onSelected ? kSecondaryColor : kPrimaryColor,
),
),
),
SizedBox(
width: getProportionateScreenWidth(7.5),
),
InkWell(
onTap: () {
// first text - title and subtitle selected!
setState(() {
iconSelected = false;
firstTextSelected = true;
seccondTextSelected = false;
});
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
widget.title,
style: TextStyle(
color: widget.onSelected ? Colors.white : Colors.black,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.w600),
),
Text(
widget.subTitle,
style: TextStyle(
color: widget.onSelected ? Colors.white : kTextColor,
fontSize: getProportionateScreenWidth(12),
fontWeight: FontWeight.w500),
),
],
),
),
Spacer(),
InkWell(
onTap: () {
// second text - amount selected.
setState(() {
iconSelected = false;
firstTextSelected = false;
seccondTextSelected = true;
});
},
child: Text(
widget.amount,
style: TextStyle(
color: widget.onSelected ? Colors.white : kPrimaryColor,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.bold),
),
),
],
),
),
);
}
}
class VisitTypeFees扩展StatefulWidget{
const VisitType费用({
关键点,
this.onSelected=false,
这个名字,
这个,副标题,,
这个金额,,
这个图标,
}):super(key:key);
最终选举产生;
最终字符串标题、副标题、金额;
最终的Iconda图标;
@凌驾
_VisitTypeFeesState createState()=>\u VisitTypeFeesState();
}
类_VisitTypeFeesState扩展状态{
我选择了布尔;
选择布尔第一文本;
选择布尔秒;
@凌驾
小部件构建(构建上下文){
回墨槽(
onTap:(){},
子:容器(
身高:65度,
宽度:SizeFig.screenWidth-20,
填充:EdgeInsets.symmetric(
水平方向:获取屏幕宽度(10),
垂直方向:获得筛宽的比例(7.5),
),
装饰:盒子装饰(
颜色:widget.onSelected?kSecondaryColor:Colors.white,
边界半径:边界半径。圆形(10),
boxShadow:[kDefaultShadow]),
孩子:排(
儿童:[
墨水池(
onTap:(){
//选择图标!
设置状态(){
i选择=真;
firstTextSelected=false;
secondtextselected=false;
});
},
子:容器(
高度:50英尺,
宽度:50毫米,
装饰:盒子装饰(
颜色:widget.onSelected
?颜色:白色
:kPrimary LightColor。不透明度(0.6),
边界半径:边界半径。圆形(10)),
子:图标(
widget.icon,
颜色:widget.onSelected?ksSecondaryColor:kPrimaryColor,
),
),
),
大小盒子(
宽度:宽度(7.5),
),
墨水池(
onTap:(){
//第一个文本-选择标题和副标题!
设置状态(){
i选择=假;
firstTextSelected=true;
secondtextselected=false;
});
},
子:列(
crossAxisAlignment:crossAxisAlignment.start,
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
widget.title,
样式:TextStyle(
颜色:widget.onSelected?颜色。白色:颜色。黑色,
fontSize:GetControluteScreenWidth(15),
fontWeight:fontWeight.w600),
),
正文(
widget.subTitle,
样式:TextStyle(
颜色:widget.onSelected?颜色。白色:kTextColor,
fontSize:GetControluteScreenWidth(12),
fontWeight:fontWeight.w500),
),
],
),
),
垫片(),
墨水池(
onTap:(){
//第二个文本-所选金额。
设置状态(){
i选择=假;
firstTextSelected=false;
secondtextselected=true;
});
},
子:文本(
金额,
样式:TextStyle(
颜色:widget.onSelected?颜色。白色:kPrimaryColor,
fontSize:GetControluteScreenWidth(15),
fontWeight:fontWeight.bold),
),
),
],
),
),
);
}
}
首先,您需要将小部件转换为StatefulWidget。然后,您需要为每个文本和图标小部件包装InkWell小部件。他们都有自己的财产
它将如下所示:
class VisitTypeFees extends StatefulWidget {
const VisitTypeFees({
Key key,
this.onSelected = false,
this.title,
this.subTitle,
this.amount,
this.icon,
}) : super(key: key);
final bool onSelected;
final String title, subTitle, amount;
final IconData icon;
@override
_VisitTypeFeesState createState() => _VisitTypeFeesState();
}
class _VisitTypeFeesState extends State<VisitTypeFees> {
bool iconSelected;
bool firstTextSelected;
bool seccondTextSelected;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
height: getProportionateScreenHeight(65),
width: SizeConfig.screenWidth - 20,
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(10),
vertical: getProportionateScreenWidth(7.5),
),
decoration: BoxDecoration(
color: widget.onSelected ? kSecondaryColor : Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [kDefaultShadow]),
child: Row(
children: [
InkWell(
onTap: () {
// icon selected!
setState(() {
iconSelected = true;
firstTextSelected = false;
seccondTextSelected = false;
});
},
child: Container(
height: getProportionateScreenHeight(50),
width: getProportionateScreenHeight(50),
decoration: BoxDecoration(
color: widget.onSelected
? Colors.white
: kPrimaryLightColor.withOpacity(0.6),
borderRadius: BorderRadius.circular(10)),
child: Icon(
widget.icon,
color: widget.onSelected ? kSecondaryColor : kPrimaryColor,
),
),
),
SizedBox(
width: getProportionateScreenWidth(7.5),
),
InkWell(
onTap: () {
// first text - title and subtitle selected!
setState(() {
iconSelected = false;
firstTextSelected = true;
seccondTextSelected = false;
});
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
widget.title,
style: TextStyle(
color: widget.onSelected ? Colors.white : Colors.black,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.w600),
),
Text(
widget.subTitle,
style: TextStyle(
color: widget.onSelected ? Colors.white : kTextColor,
fontSize: getProportionateScreenWidth(12),
fontWeight: FontWeight.w500),
),
],
),
),
Spacer(),
InkWell(
onTap: () {
// second text - amount selected.
setState(() {
iconSelected = false;
firstTextSelected = false;
seccondTextSelected = true;
});
},
child: Text(
widget.amount,
style: TextStyle(
color: widget.onSelected ? Colors.white : kPrimaryColor,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.bold),
),
),
],
),
),
);
}
}
class VisitTypeFees扩展StatefulWidget{
const VisitType费用({
关键点,
this.onSelected=false,
这个名字,
这个,副标题,,
这个金额,,
这个图标,
}):super(key:key);
最终选举产生;
最终字符串标题、副标题、金额;
最终的Iconda图标;
@凌驾
_VisitTypeFeesState createState()=>\u VisitTypeFeesState();
}
类_VisitTypeFeesState扩展状态{
我选择了布尔;
选择布尔第一文本;
选择布尔秒;
@凌驾
小部件构建(构建上下文){
回墨槽(
onTap:(){},
子:容器(
身高:65度,
宽度:SizeFig.screenWidth-20,
填充:EdgeInsets.symmetric(
水平方向:获取屏幕宽度(10),
垂直方向:获得筛宽的比例(7.5),
),
装饰:盒子装饰(
颜色:widget.onSelected?kSecondaryColor:Colors.white,
边界半径:边界半径。圆形(10),
boxShadow:[kDefaultShadow]),
孩子:排(
儿童:[
墨水池(
onTap:(){