当我使用flutter Dart中的小部件构造函数创建多个按钮时,如何将一个容器变暗

发布于 2025-01-27 01:45:11 字数 1992 浏览 6 评论 0原文

因此,我有一个代码,需要在同一页面上添加多个按钮。我使用的是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生殊途 2025-02-03 01:45:11

我认为您应该使用Flutter文档描述的方法来对容器的属性进行动画:您必须根据getruestector ontap属性修改实例属性。
这意味着:

  1. 您必须使其成为一个状态的小部件,以便允许它更改
  2. 使用其实例变量,以分别更改每个按钮的属性

,这是链接: 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:

  1. you have to make it a stateful widget, in order to allow it to change
  2. use its instance variables to change separately every single button's property

Here's the link: Flutter Documentation: Animate the properties of a container

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文