简体   繁体   English

如何从同一屏幕上的另一个有状态小部件接收数据?

[英]How to receive data from another statefull widget on the same screen?

So i need to receive data from my RadioDialog stf widget to BoxScreen widget.所以我需要从我的RadioDialog stf 小部件接收数据到BoxScreen小部件。
I want to show different text depends of random value from boxItems, what i do on this line _randomBoxItem == boxOptionValue? Text('text1'): Text('text2'),我想显示不同的文本取决于来自 boxItems 的随机值,我在这条线上做什么_randomBoxItem == boxOptionValue? Text('text1'): Text('text2'), _randomBoxItem == boxOptionValue? Text('text1'): Text('text2'),
And i dont know how to get value from _boxOption to boxOptionValue like exmple.而且我不知道如何从 _boxOption 到 boxOptionValue 的值,例如 exmple。
Here is my code:这是我的代码:

class BoxScreen extends StatefulWidget {
  const BoxScreen({Key? key}) : super(key: key);
  static const routeName = '/box';

  @override
  State<BoxScreen> createState() => _BoxScreenState();
}

class _BoxScreenState extends State<BoxScreen> {
  String? boxOptionValue;
  final List<String> boxItems = [
    'Banana',
    'Apple',
    'Orange',
  ];

  Future<void> whatIsInTheBox(BuildContext context) async {
    return await showDialog(
      context: context,
      builder: (BuildContext context) => const RadioDialog(),
    );
  }

  @override
  Widget build(BuildContext context) {
    final String _randomBoxItem = (boxItems..shuffle()).first;
    return Scaffold(
      appBar: AppBar( title: const Text('BOX Game'),),
      body: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Text(_randomBoxItem),
                _randomBoxItem == boxOptionValue ? Text('text1') : Text('text2'),
                ElevatedButton.icon(
                  icon: const Icon(Icons.play_arrow_rounded),
                  onPressed: () {
                    whatIsInTheBox(context);
                  },
                  label: const Text('What is in BOX?'),
                ),
              ],
            ),
    );
  }
}


class RadioDialog extends StatefulWidget {
  const RadioDialog({Key? key}) : super(key: key);

  @override
  State createState() => RadioDialogState();
}

class RadioDialogState extends State<RadioDialog> {
  // SingingCharacter? _character = SingingCharacter.banana;
  String? _boxOption;

  void _handleBoxItemChange(String? value) {
    setState(() {
      _boxOption = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      contentPadding: const EdgeInsets.symmetric(horizontal: 0, vertical: 10),
      title: const Text('Select option'),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          RadioListTile<String>(
            title: const Text('Banana'),
            value: 'Banana',
            groupValue: _boxOption,
            onChanged: _handleBoxItemChange,
          ),
          RadioListTile<String>(
            title: const Text('Apple'),
            value: 'Apple',
            groupValue: _boxOption,
            onChanged: _handleBoxItemChange,
          ),
          RadioListTile<String>(
            title: const Text('Orange'),
            value: 'Orange',
            groupValue: _boxOption,
            onChanged: _handleBoxItemChange,
          ),
        ],
      ),
      actions: [
        ElevatedButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          child: const Text('I choose this!'),
        ),
      ],
    );
  }
}

I am with Flutter 2 month.我在 Flutter 2 个月。 Thanks谢谢

In the widget calling the dialog:在调用对话框的小部件中:

someVariable =  await showDialog(...);

In the dialog widget:在对话框小部件中:

Navigator.of(context).pop(returnedValue);

Are you able to declare a string outside of your widgets?你能在你的小部件之外声明一个字符串吗? Then you should be able to use it like so..然后你应该可以像这样使用它..



String newValue = ''; //**define a string outside the widgets**

class BoxScreen extends StatefulWidget {
  const BoxScreen({Key? key}) : super(key: key);
  static const routeName = '/box';

  @override
  State<BoxScreen> createState() => _BoxScreenState();
}

class _BoxScreenState extends State<BoxScreen> {
  String? boxOptionValue;
  final List<String> boxItems = [
    'Banana',
    'Apple',
    'Orange',
  ];

  Future<void> whatIsInTheBox(BuildContext context) async {
    return await showDialog(
      context: context,
      builder: (BuildContext context) => const RadioDialog(),
    );
  }

  @override
  Widget build(BuildContext context) {
    final String _randomBoxItem = (boxItems..shuffle()).first;
    return Scaffold(
      appBar: AppBar( title: const Text('BOX Game'),),
      body: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                Text(_randomBoxItem),
                _randomBoxItem == boxOptionValue ? Text('text1') : Text('text2'),
                ElevatedButton.icon(
                  icon: const Icon(Icons.play_arrow_rounded),
                  onPressed: () {
                    whatIsInTheBox(context);
                  },
                  label: const Text('What is in BOX?'),
                ),
              ],
            ),
    );
  }
}


class RadioDialog extends StatefulWidget {
  const RadioDialog({Key? key}) : super(key: key);

  @override
  State createState() => RadioDialogState();
}

class RadioDialogState extends State<RadioDialog> {
  // SingingCharacter? _character = SingingCharacter.banana;
  String? _boxOption;

  void _handleBoxItemChange(String? value) {
    setState(() {
      newValue = value; //**Make it equal the new value**
    });
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      contentPadding: const EdgeInsets.symmetric(horizontal: 0, vertical: 10),
      title: Text(newValue), //**use it in any widget like this**
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          RadioListTile<String>(
            title: const Text('Banana'),
            value: 'Banana',
            groupValue: _boxOption,
            onChanged: _handleBoxItemChange,
          ),
          RadioListTile<String>(
            title: const Text('Apple'),
            value: 'Apple',
            groupValue: _boxOption,
            onChanged: _handleBoxItemChange,
          ),
          RadioListTile<String>(
            title: const Text('Orange'),
            value: 'Orange',
            groupValue: _boxOption,
            onChanged: _handleBoxItemChange,
          ),
        ],
      ),
      actions: [
        ElevatedButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          child: const Text('I choose this!'),
        ),
      ],
    );
  }
}

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

相关问题 如何在底部的另一个屏幕中添加有状态的小部件或查看 - How to add a statefull widget or view in another screen at the bottom 如何从另一个小部件更改有状态小部件中变量的值 - How to change value of variable in statefull widget from another widget Flutter 将数据从一个 class 传递到另一个(有状态小部件) - Flutter pass data from one class to another(statefull widget) 如何从小部件调用用户定义的方法并将其转发到另一个有状态页面 - How can I call a user-defined method from a widget and forward it to another statefull page 如何在 flutter 中将 id 从有状态小部件获取到无状态小部件? - how to get the id from statefull widget to stateless widget in flutter? 如何将一个小部件的值发送到同一屏幕上的另一个小部件? (扑) - how to send a value from a widget to another on the same screen ? (flutter) 如何在颤动中将图库图像传递到另一个 Statefull 屏幕 - How to pass the gallery image to another Statefull Screen in flutter 从无状态小部件调用有状态小部件的 setState - Call a setState of a statefull widget from the stateless widget 从有状态的父级修改有状态的子小部件中的值 - Modify value in statefull child widget from its statefull parent 将变量从有状态小部件传递到 state - pass a variable from a statefull widget to state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM