Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

Flutter/Dart - How can I pass radio button value to a different class in another file?

I need to pass the radio button value stored in variable radioValue from homepage.dart to class DialogFactory in dialogfactory.dart, where I will use it. The current radioValue value should be passed when I press a button which calls function _openDialog(), which is meant to open the alert dialog with the selected style.

======================================

homepage.dart


[...]

class _MyHomePageState extends State<MyHomePage> {
  int radioValue = 1;

  void _handleRadioValueChange(int value) {
    setState(() {
      radioValue = value;
    });
  }

  void _openDialog() {

    DialogFactory.showAlertDialog(
      context,
      title: Text('Alert Dialog!!!'),
      content: Text(
          'THIS IS AN ALERT DIALOG! IT MEANS YOU SHOULD BE IN ALERT STATE, RIGHT?'),
      actions: [
        DialogAction(
          child: Text('YES'),
          onPressed: () => print('YES'),
        ),
        DialogAction(
          child: Text('NO'),
          onPressed: () => print('NO'),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Radio(
              value: 0,
              groupValue: radioValue,
              onChanged: _handleRadioValueChange,
            ),
            new Text('IOS'),
            Radio(
              value: 1,
              groupValue: radioValue,
              onChanged: _handleRadioValueChange,
            ),
            new Text('Android'),
            RaisedButton(
              child: Text('Show Alert'),
              color: Colors.purple,
              textColor: Colors.white,
              onPressed: _openDialog,
            ),
          ],
        ),
      ),
    );
  }
}

dialogfactory.dart:


class DialogFactory {
  static Future<T> showAlertDialog<T>(BuildContext context,
      {Widget title, Widget content, List<DialogAction> actions}) {
    IDialog dialogData;
    int radioValue = -1;

    // HOW TO GET RADIO VALUE?

    if (radioValue == 0) {
      // ios
      dialogData = IosDialog();
    } else if (radioValue == 1) {
      //android
      dialogData = AndroidDialog();
    } else {
      dialogData = AndroidDialog();
    }

    return showDialog(
      context: context,
      builder: (context) => dialogData.create(
        context,
        title ?? Text('N?o informado'),
        content ?? Text('N?o informado'),
        actions ?? [],
      ),
    );
  }
}

link to the repository with the code: https://github.com/gicraveiro/FactoryMethod


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I passed the radioValue from the homepage.dart into the showAlertDialog.

enter image description here enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...