首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用动态onPressed颤振创建新的按钮小部件

使用动态onPressed颤振创建新的按钮小部件
EN

Stack Overflow用户
提问于 2022-06-03 13:13:18
回答 1查看 30关注 0票数 1

我有一个带有onPressed函数的按钮小部件。现在,我想使用这个小部件来创建多个按钮,但当然也需要使用另一个函数来运行。

新的按钮小部件:

代码语言:javascript
复制
class RunderKreis extends StatelessWidget {
  final DecorationImage? image;
  final double size;
  Color? color;
  IconData? icon;
  Color? onPrimaryColors;
  Future<dynamic>? directTo;
  RunderKreis({
    Key? key,
    this.color = const Color.fromARGB(255, 95, 81, 136),
    this.size = 90,
    this.icon,
    this.onPrimaryColors,
    this.image,
    this.directTo,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: size,
      width: size,
      child: ElevatedButton(
        onPressed: () {
              directTo;
        },
        child: Icon(icon),
      ),
    );
  }
}

使用它:

代码语言:javascript
复制
RunderKreis(
                        icon: FeatherIcons.pieChart,
                        onPrimaryColors: AppColors.backColor,
                         directTo:  Future.delayed(Duration(seconds: 1),(){
                       Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => const Steckbrief()),
                      );}),
                      ),
RunderKreis(...),
RunderKreis(...),

现在我使用了3个按钮,但是我立即被重定向到使用第一个onPressed函数导航到的页面,而没有按a按钮:(我是不是以错误的方式实现了什么?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2022-06-03 13:45:26

我更喜欢这种款式。

代码语言:javascript
复制
 child: ElevatedButton(
        onPressed:directTo,
        child: Icon(icon),
      ),

用例将是

代码语言:javascript
复制
 directTo: () async {
await Future.delayed(Duration(seconds: 1), () {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => const Steckbrief()),
  );
});

您可以将VoidCallback用于tap事件。

代码语言:javascript
复制
class RunderKreis extends StatelessWidget {
  final DecorationImage? image;
  final double size;
  Color? color;
  IconData? icon;
  Color? onPrimaryColors;
  VoidCallback? onTap;
    RunderKreis({
    Key? key,
    this.color = const Color.fromARGB(255, 95, 81, 136),
    this.size = 90,
    this.icon,
    this.onPrimaryColors,
    this.image,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: size,
      width: size,
      child: ElevatedButton(
        onPressed: onTap,
        child: Icon(icon),
      ),
    );
  }
}

和使用

代码语言:javascript
复制
RunderKreis(
  onTap: () async {},
),
RunderKreis(
  onTap: () {},
),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72490162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档