当我使用flutter Dart中的小部件构造函数创建多个按钮时,如何将一个容器变暗
因此,我有一个代码,需要在同一页面上添加多个按钮。我使用的是fenuredetector,因为我不喜欢实际按钮的OnPress动画。
触摸时,我希望使颜色变黑。只要我不必为我添加的每个按钮添加不同的变量,它的发生方式就不会真正重要。 我当前的代码是对每个按钮都使用一个变量,不用说,当变量更新时,它会更改所有按钮。
GestureDetector(
onTap: () {
// Funtion to change color of PageButton child
setState(() {
colorTapped = !colorTapped;
});
},
child: PageButton(
text: 'BUTTON 1',
color: colorTapped,
),
),
SizedBox(
height: 10,
),
GestureDetector(
onTap: () {
// Funtion to change color of PageButton child
setState(() {
colorTapped = !colorTapped;
});
},
child: PageButton(
text: 'BUTTON 2',
color: colorTapped,
),
),
PageButton小部件:
class PageButton extends StatelessWidget {
PageButton(
{required this.text, this.width, this.height, required this.color});
final String text;
bool color;
// final IconData icon;
final double? width;
final double? height;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: color ? Color(0x66DE4B4D) : Color(0xFFEF4B4D),
),
child: Center(
child: Text(
text,
style: kLargeButtonTextStyle,
),
),
width: width ?? MediaQuery.of(context).size.width * 0.9,
height: height ?? 50.0,
padding: EdgeInsets.fromLTRB(30.0, 5.0, 10.0, 5.0),
);
}
}
长话短说:我想更改儿童小部件的颜色,而无需每次添加一个按钮时都不必创建一个变量
So I have a piece of code where I need to add multiple buttons to the same page. I am using a GestureDetector since I don't like the onPress animation for the actual buttons.
I wish to darken the color when it's touched. The way it happens doesn't really matter, as long as I don't have to add different variables for every button I add.
My current code is using a single variable for each of the buttons, which, needless to say, changes all the buttons when the variable is updated.
GestureDetector(
onTap: () {
// Funtion to change color of PageButton child
setState(() {
colorTapped = !colorTapped;
});
},
child: PageButton(
text: 'BUTTON 1',
color: colorTapped,
),
),
SizedBox(
height: 10,
),
GestureDetector(
onTap: () {
// Funtion to change color of PageButton child
setState(() {
colorTapped = !colorTapped;
});
},
child: PageButton(
text: 'BUTTON 2',
color: colorTapped,
),
),
PageButton widget:
class PageButton extends StatelessWidget {
PageButton(
{required this.text, this.width, this.height, required this.color});
final String text;
bool color;
// final IconData icon;
final double? width;
final double? height;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: color ? Color(0x66DE4B4D) : Color(0xFFEF4B4D),
),
child: Center(
child: Text(
text,
style: kLargeButtonTextStyle,
),
),
width: width ?? MediaQuery.of(context).size.width * 0.9,
height: height ?? 50.0,
padding: EdgeInsets.fromLTRB(30.0, 5.0, 10.0, 5.0),
);
}
}
Long story short: I want to change the color of the child Widget onTap without having to create a variable each time I add a button
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该使用Flutter文档描述的方法来对容器的属性进行动画:您必须根据getruestector
ontap
属性修改实例属性。这意味着:
,这是链接: flutter文档:动画容器的属性
I think you should use the approach described by flutter documentation to animate the properties of a container: you have to modify the instance properties based on the GestureDetector
onTap
property.This means:
Here's the link: Flutter Documentation: Animate the properties of a container