点击文本或其他小部件时如何触发单选按钮?

时间:2019-12-27 06:28:40

标签: flutter dart

我用2个这样的小部件创建Row

import React from 'react';

const app_url = 'https://*******.com/';

import $ from "jquery";
import MobileEsp from app_url+'/js/mdetect.js'; 

//const App = ({ title }) =>

class App extends React.Component {

  componentDidMount() {
    if (!MobileEsp.DetectTierIphone() && !MobileEsp.DetectTierTablet()) {

      var LinkDownloadAppMac1 = "*****";
      var LinkDownloadAppWin1 = "******";
      switch (navigator.platform) {
        case 'MacIntel':
          $('#menu-download-link').attr('href', LinkDownloadAppMac1);
          break;
        default:
          $('#menu-download-link').attr('href', LinkDownloadAppWin1);
          break;

      }
    }
}

因此,当我点击文字时,收音机将被触发,例如在带有jquery的javascript中

Row(
   children: [
      new Radio(
        value: 0,
        groupValue: rv0,
        onChanged: _handleRadioValueChange1,
      ),

      GestureDetector(
         onTap: (){ //trigger radio button },
         child: Text('Tap Me')
      )
   ],
)

如何在飞镖扑打中做到这一点?谢谢

2 个答案:

答案 0 :(得分:0)

如果您想在单击textview时选择单选按钮值,则需要在单击textview时更改单选按钮的groupValue

示例代码

class MyApp extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MyApp> {
  int _radioValue1 = 0;

  void _handleRadioValueChange1(int value) {
    setState(() {
      _radioValue1 = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('RadioListTile Demo'),
      ),
      //hit Ctrl+space in intellij to know what are the options you can use in flutter widgets
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.all(32.0),
          child: Column(
            children: <Widget>[
              Radio(
                value: 0,
                groupValue: _radioValue1,
                onChanged: _handleRadioValueChange1,
              ),
              Radio(
                value: 1,
                groupValue: _radioValue1,
                onChanged: _handleRadioValueChange1,
              ),
              Radio(
                value: 2,
                groupValue: _radioValue1,
                onChanged: _handleRadioValueChange1,
              ),
              GestureDetector(
                onTap: () {
                  setState(() {
                    _radioValue1 = 0;
                  });
                },
                child: Text("Select First Radio Button"),
              ),
              GestureDetector(
                onTap: () {
                  setState(() {
                    _radioValue1 = 1;
                  });
                },
                child: Text("Select Second Radio Button"),
              ),
              GestureDetector(
                onTap: () {
                  setState(() {
                    _radioValue1 = 2;
                  });
                },
                child: Text("Select Third Radio Button"),
              )
            ],
          ),
        ),
      ),
    );
  }
}

答案 1 :(得分:0)

    Row(
   children: [
      new Radio(
        value: 0,
        groupValue: rv0,
        onChanged: _handleRadioValueChange1,
      ),

      GestureDetector(
         onTap: (){ 
        //trigger radio button
//you can pass any value which is the value of respective radio button
_handleRadioValueChange1(0)
 },
         child: Text('Tap Me')
      )
   ],
)

或者您可以使用RadioListTile小部件。