简体   繁体   English

如何将 onPressed 值发送到另一个小部件 Flutter

[英]How to send onPressed value to another widget Flutter

I want to send data from widget to another widget, in my example I want to send onPressed value as a variable.我想将数据从小部件发送到另一个小部件,在我的示例中,我想将 onPressed 值作为变量发送。

appBar: CancelAppBar(onPressed: ),

What should I write after onPressed (in the above code) to send the value: Navigator.of(context).push(MaterialPageRoute(builder: (context) => UnderBuild()));我应该在 onPressed 之后写什么(在上面的代码中)来发送值: Navigator.of(context).push(MaterialPageRoute(builder: (context) => UnderBuild()));

to a widget in a seperate dart file?到单独的 dart 文件中的小部件?

Following is the other file:以下是另一个文件:

class CancelAppBar extends StatefulWidget implements PreferredSizeWidget {
  CancelAppBar({Key? key, required this.onPressed}) : super(key: key);

  final ValueGetter<String> onPressed;
  static final _appBar = AppBar();
  @override
  Size get preferredSize => _appBar.preferredSize;

  @override
  _CancelAppBarState createState() => _CancelAppBarState();
}

class _CancelAppBarState extends State<CancelAppBar> {
  get onPressed => null;

  @override
  Widget build(BuildContext context) {
    return AppBar(
      titleSpacing: 0.0,
      title: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Padding(padding: EdgeInsets.only(left: 8.w)),
          IconButton(
            onPressed: ,
            icon: Icon(Icons.close),
          )
        ],
      ),
      backgroundColor: AppColors.dark,
    );
  }
}

You can access any StatefulWidget variable in the State class with widget getter, like:您可以使用widget getter 访问State类中的任何StatefulWidget变量,例如:

class CancelAppBar extends StatefulWidget implements PreferredSizeWidget {
  CancelAppBar({Key? key, required this.onPressed}) : super(key: key);

  final ValueGetter<String> onPressed;
  static final _appBar = AppBar();
  @override
  Size get preferredSize => _appBar.preferredSize;

  @override
  _CancelAppBarState createState() => _CancelAppBarState();
}

class _CancelAppBarState extends State<CancelAppBar> {
  @override
  Widget build(BuildContext context) {
    return AppBar(
      titleSpacing: 0.0,
      title: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Padding(padding: EdgeInsets.only(left: 8.w)),
          IconButton(
            onPressed: widget.onPressed, /// widget.yourVariable
            icon: Icon(Icons.close),
          )
        ],
      ),
      backgroundColor: AppColors.dark,
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter onPressed 触发另一个小部件的 State - Flutter onPressed trigger State of another widget 如何使用按钮的 onPressed 属性将小部件文本的值复制到另一个 - How to copy the value of a Widget Text to another using onPressed property of a button 在 flutter 中使用 onPressed 事件从另一个小部件调用小部件以打开 alertDialog - Calling widget from another widget with onPressed event in flutter to open alertDialog 如何将一个小部件的值发送到同一屏幕上的另一个小部件? (扑) - how to send a value from a widget to another on the same screen ? (flutter) 将 OnPressed 操作添加到小部件 flutter - Add OnPressed action to a widget flutter 如何在按下的 flutter 上使用幻灯片 animation 在树上添加小部件 - How to add widget on tree using slide animation on onpressed flutter Flutter:如何从单独的Widget方法中调用onPressed()中的setState() - Flutter: how to call setState() in onPressed() from separate Widget method 如何为文本小部件实现 OnPressed 回调,Flutter - How can I implement OnPressed callback for Text widget, Flutter 如何在不按动颤动的情况下从一个屏幕导航到另一个屏幕? - How to navigate from one screen to another without onpressed in flutter? 如何通过 function 用于在 flutter 的另一页中的按钮的 Onpressed? - How to pass the function for Onpressed for buttons in another page in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM