简体   繁体   English

如何在 MyApp 的构建方法中调用无状态小部件,其 state 由其有状态的父级管理

[英]How to call a Stateless widget in MyApp's build method whose state is managed by its Stateful Parent

I was following flutter tutorials for managing state of a widget from its parent on this link [https://flutter.dev/docs/development/ui/interactive#parent-managed][1] and i cant figure out how would call the widget in this case我正在关注 flutter 教程,用于管理此链接上来自其父级的小部件的 state [https://flutter.dev/docs/development/ui/interactive#parent-managed][1],我不知道如何调用在这种情况下小部件

it is very simple once you get the logic.一旦你掌握了逻辑,这非常简单。

In practice, the parent (the "true" widget that you call), ie在实践中,父级(您调用的“真实”小部件),即

class ParentWidget extends StatefulWidget {
  @override
  _ParentWidgetState createState() => _ParentWidgetState();
}

is the one that you call wherever and whenever you want in the rest of the code.是您在代码的 rest 中随时随地调用的那个。

Since this is a Stateful widget, it means that it has stated (to keep it simple, it will manage any changes on the UI).由于这是一个有状态的小部件,这意味着它已声明(为简单起见,它将管理 UI 上的任何更改)。 Any change will occur, It will be changing its state and so, this code:任何更改都会发生,它将更改其 state 等,此代码:

class _ParentWidgetState extends State<ParentWidget> {
  bool _active = false;

  void _handleTapboxChanged(bool newValue) {
    setState(() {
      _active = newValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: TapboxB(
        active: _active,
        onChanged: _handleTapboxChanged,
      ),
    );
  }
}

Anyhow, once you use a Stateful widget, you change its state whenever you want to call the function无论如何,一旦你使用了一个有状态的小部件,你只要你想调用 function 就改变它的 state

setState(() {
      oldValue= newValue;
    });

It will rebuild the entire widget changing the stuff you want (such as texts, images, widgets, and so on).它将重建整个小部件以更改您想要的内容(例如文本、图像、小部件等)。 In a non-proper way , consider it as a particular widget that can change its UI during the time.以一种不恰当的方式,将其视为可以在此期间更改其 UI 的特定小部件。

  • if you want to call it in MyApp's build method you will have to make MyApp a stateful widget so that it can manage the state of the said widget如果您想在 MyApp 的构建方法中调用它,您必须使 MyApp 成为有状态的小部件,以便它可以管理所述小部件的 state
void main() => runApp(MyApp());

//we make MyApp to be a stateful widget
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  //we define the state which will be used in the widget here
  var myState = "something";
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: Container(
            //the data used by MyWidget is managed by MyApp which is a statefull widget.
            child: MyWidget(state: myState),
          ),
        ),
      ),
    );
  }
}

Or rather wrap your widget with another stateful widget which you will use in MyApp's build method或者更确切地说,用另一个有状态的小部件包装您的小部件,您将在 MyApp 的构建方法中使用它

//we create a widget which will manage the state of its children class MyStateManagingWidget extends StatefulWidget {   @override  
_MyStateManagingWidgetState createState() => _MyStateManagingWidgetState(); }

class _MyStateManagingWidgetState extends State<MyStateManagingWidget> {   var myState = "some state";   @override   Widget build(BuildContext context) {
    //we put our widget who's state is to be managed here
    return MyWidget();   } }

class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Material App Bar'),
        ),
        body: Center(
          child: Container(
              //we now use  the state managing widget here
              child: MyStateManagingWidget()),
        ),
      ),
    );   } }

暂无
暂无

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

相关问题 如何从无状态父小部件调用有状态子小部件的状态函数? - How to call state functions of a stateful child widget from a stateless parent widget? Flutter 最佳实践 - 如何从其 state 调用有状态小部件的动态方法? - Flutter best practices - How to call a dynamic method of a stateful widget from its state? 如何从子无状态小部件设置有状态小部件的状态 - How to set the state of a stateful widget from a child stateless widget Flutter:如何将有状态小部件转换为无状态小部件并保持 state 更改 - Flutter: How to convert a stateful widget to a stateless widget and keep state changes 如何与父无状态 Widget 的子有状态 Widget 交互? - How to interact with a child Stateful Widget from a parent Stateless Widget? 无状态小部件到有状态小部件(使用创建状态) - Stateless widget to stateful widget ( using Create state) 我应该如何实现init方法? 在有状态或无状态小部件中? - How should I implement the init method? In a stateful or stateless widget? 如何从无状态小部件变为有状态小部件? - How to change from Stateless Widget to Stateful Widget? 如何在 flutter 中将有状态小部件转换为无状态小部件? - How to convert a Stateful widget into a Stateless widget in flutter? 如何将对象从有状态小部件传递到其状态? (不在小部件中) - How to pass an object from Stateful Widget to its State? (not in Widget)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM