简体   繁体   English

将字符串值从一个有状态小部件更改为另一个有状态小部件

[英]Change String value from one stateful widget to another stateful widget

I have a Stateful widget:我有一个有状态的小部件:

class PractiseInterview extends StatefulWidget {
  @override
  _PractiseInterviewState createState() => _PractiseInterviewState();
}

class _PractiseInterviewState extends State<PractiseInterview> {
  String outputText = '0';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Practise'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Text(
              outputText,         //**outputText is here**  
            ),
            Expanded(
              child: Divider(),
            ),
            Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    ChnageText(text: 'changed',),
                  ],
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

NOTICE that my outputText = '0' here.注意我的outputText = '0'在这里。

Then I have a stateless widget for my OutlineButton in stateful widget:然后我在有状态小部件中为我的 OutlineButton 提供了一个无状态小部件:

class ChnageText extends StatelessWidget {
  const ChnageText({ this.text});
  final String text;

  buttonPressed(String text) {
    if (text == 'changed') {
      //TODO: change outputText value to 'changed' at the click of this button
    }
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: OutlineButton(
        onPressed: () {
          buttonPressed(text);
        },
        child: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Text(
            text,
            style: TextStyle(
              fontSize: 15.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }
}

I have mentioned TODO in my stateless widget, there at the click of the button I want to change outputText = '0' to outputText = 'changed' .我在我的无状态小部件中提到了 TODO,单击按钮我想将outputText = '0'更改为outputText = 'changed' I don't know how to do this我不知道该怎么做

I am new to flutter and I am not able to understand this.我是 flutter 的新手,我无法理解这一点。 Basically I am making a calculator where when a button is clicked, then the value of the button should be displayed.基本上我正在制作一个计算器,当点击一个按钮时,应该显示按钮的值。

Pass a callback to ChnageText from the parent that changes outputText and calls setState .将更改outputText并调用setState的父级回调传递给ChnageText

class ChnageText extends StatelessWidget {
  const ChnageText({this.text, this.callback});
  final String text;
  final VoidCallback callback;

  buttonPressed(String text) {
    if (text == 'changed') {
      callback();//Call callback here
    }
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: OutlineButton(
        onPressed: () {
          buttonPressed(text);
        },
        child: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Text(
            text,
            style: TextStyle(
              fontSize: 15.0,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }
}

class _PractiseInterviewState extends State<PractiseInterview> {
  String outputText = '0';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Practise'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Text(
              outputText,         //**outputText is here**  
            ),
            Expanded(
              child: Divider(),
            ),
            Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    ChnageText(text: 'changed', callback: () {setState((){outputText = 'changed';});}),// Pass callback here
                  ],
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

Ideally, you wouldn't have to do this at all.理想情况下,您根本不必这样做。 What's in ChnageText doesn't need to be in its own StatelessWidget . ChnageText中的内容不需要在它自己的StatelessWidget中。 Putting all of that directly in the parent removes this problem.将所有这些直接放在父级中可以消除这个问题。

class _PractiseInterviewState extends State<PractiseInterview> {
  String outputText = '0';

  buttonPressed(String text) {
    if (text == 'changed') {
      setState(() {
        outputText = 'changed';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Practise'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            Text(
              outputText, //**outputText is here**
            ),
            Expanded(
              child: Divider(),
            ),
            Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    //Put ChnageText directly into the build of the parent
                    Expanded(
                      child: OutlineButton(
                        onPressed: () {
                          buttonPressed('changed');
                        },
                        child: Padding(
                          padding: const EdgeInsets.all(24.0),
                          child: Text(
                            'changed',
                            style: TextStyle(
                              fontSize: 15.0,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

暂无
暂无

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

相关问题 如何从另一个有状态小部件调用一个有状态小部件中的方法 - how to Call method in one stateful widget from another stateful widget 从另一个有状态小部件调用一个有状态小部件中的方法 - Flutter - call method in one stateful widget from another stateful widget - Flutter 如何在 Flutter 中从另一个有状态小部件更改一个有状态小部件的状态? - How to change state of one Stateful Widget from another Stateful Widget in Flutter? Flutter - 从另一个有状态小部件设置一个有状态小部件的变量值 - Flutter - Setting value of a variable of one stateful widget from another stateful widget 如何从另一个小部件更改有状态小部件的变量? - How to change variable of a stateful widget from another widget? 将选定的 DropDown 值从有状态小部件返回到 MainPage 有状态小部件 - return selected DropDown value from stateful widget to MainPage stateful widget 如何从无状态小部件变为有状态小部件? - How to change from Stateless Widget to Stateful Widget? 如何将基于键的对象从一个有状态小部件传递到另一个有状态小部件 - How to pass objects based on key from one stateful widget to another stateful widget 从flutter中的另一个类的有状态小部件中获取整数的值 - Get the value of an integer from a stateful widget from another class in flutter 如何在 Flutter 中将事件从一个有状态小部件广播到另一个小部件 - How To Broadcast An Event From One Stateful Widget To Another In Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM