简体   繁体   English

从 Flutter 中的父小部件访问 const 子小部件变量

[英]Access const child widget variable from parent widget in Flutter

I want to access child widget variable value in parent widget whereas child widget is a const widget which does not allow me to use GlobalKey for const child widget.我想访问父窗口小部件中的子窗口小部件变量值,而子窗口小部件是一个 const 窗口小部件,它不允许我将 GlobalKey 用于 const 子窗口小部件。

My question is how can I access child widget without GlobalKey.我的问题是如何在没有 GlobalKey 的情况下访问子窗口小部件。 Here is my code snippet:这是我的代码片段:

 class ParentWidget extends StatefulWidget { @override State<StatefulWidget> createState() { // TODO: implement createState return ParentWidgetState(); } } class ParentWidgetState extends State<ParentWidget> { @override Widget build(BuildContext context) { return Container( child: const LeftSidePanel(),); } } class LeftSidePanel extends StatefulWidget { @override State<StatefulWidget> createState() { // TODO: implement createState return LeftPanelWidgetState(); } const LeftSidePanel( ); } class LeftSidePanelState extends State<LeftSidePanel> { }

Anyone there to answer my query please.请有人回答我的问题。 Thanks谢谢

In this example none of widgets have to be Statefull ( try to use as few statefull widgets as you can)在这个例子中,没有一个小部件必须是有状态的(尽量使用尽可能少的有状态小部件)

import 'package:flutter/material.dart';

class ParentWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ChildWidget(
        getWidgetTextCallback: (someParameter) {
          print(someParameter);
          // here you can have the value from the Child widget use it wisely
        },
      ),
    );
  }
}

class ChildWidget extends StatelessWidget {
  final widgetText = 'widgetText';

  final Function(String) getWidgetTextCallback;

  const ChildWidget({required this.getWidgetTextCallback});
  
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(widgetText),
        ElevatedButton(
          onPressed:
              getWidgetTextCallback(widgetText),
          child: Text(widgetText),
        )
      ],
    );
  }
}

暂无
暂无

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

相关问题 在父窗口小部件中访问子窗口小部件的变量(Flutter with Dart) - Access child widget's variable in parent widget (Flutter with Dart) 我可以从 dart 的子小部件中的父小部件访问变量吗? - Can I access a variable from the parent widget in a child widget in dart? 在颤动中从父小部件重置子小部件 - Reset child widget from parent widget in flutter 如何从 flutter 中的父小部件访问所有孩子的 state? - How to access all of child's state from Parent Widget in flutter? 如何在Flutter中访问父级中的子控件的数据? - How to access data of child widget in parent in flutter? Flutter:如何从子小部件更新子(和父)小部件的 state(父小部件得到更新,子小部件没有) - Flutter: How to update state of child (and parent) widget from child widget (parent widget gets update, child does not) Flutter - 如何从子小部件调用父小部件函数,同时还使用变量保持状态? - Flutter - How do I call a Parent widget function from child widget while also maintaining the state with a variable? Flutter:从子小部件设置父小部件状态 - Flutter: set parent widget state from child widget 如何从子小部件调用父小部件中的 function | Flutter - How to Call a function in parent widget from child widget | Flutter 如何将数据从子状态小部件传递到 Flutter 中的父小部件 - How to pass data from a child Stateful widget to Parent Widget in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM