如何在底片抖动中设置单选按钮

时间:2020-09-29 07:30:55

标签: flutter radio-button mobile-application

我想设计一个通过单击底部的表格将显示单选按钮的设计。当我选择单选按钮时,它将打开日期选择器。下面是我单击单选按钮时未选择单选按钮的代码。我是新手,所以有人可以帮我找出代码中的错误吗?

import 'package:flutter/material.dart';

import 'BottomSheetWidget.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(title: 'Flutter Demo', home: HomeView());
  }
}

class HomeView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: MyFloatingButton(),
    );
  }
}

class MyFloatingButton extends StatefulWidget {
  @override
  _MyFloatingButtonState createState() => _MyFloatingButtonState();
}

class _MyFloatingButtonState extends State<MyFloatingButton> {
  bool _show = true;
  @override
  Widget build(BuildContext context) {
    int _radioValue = 0;
    /* var sheetController = showBottomSheet(
        context: context,
        builder: (context) => BottomSheetWidget());*/
    void _handleRadioValueChange(int value) {
      setState(() {
        _radioValue = value;
      });
      print("first"+value.toString()+"radiovalue" +_radioValue.toString());

    }
    return  Container(
      margin: EdgeInsets.all(10.0),
      child: new Wrap(
        children: <Widget>[
          Center(
              child: Container(
                  height: 3.0, width: 40.0, color: Color(0xFF32335C))),
          SizedBox(
            height: 10.0,
          ),
          new Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              new Radio(
                value: 0,
                groupValue: _radioValue,
                onChanged: (value) {setState(() {
                  _radioValue=value;
                });
                print("radiofirst"+value.toString()+"radiovalue" +_radioValue.toString());
                _handleRadioValueChange(value);
                },
              ),
              new Text(
                'Single Date',
                style: new TextStyle(fontSize: 16.0),
              ),
              new Radio(
                value: 1,
                groupValue: _radioValue,
                onChanged:  (value) {setState(() {
                  _radioValue=value;
                });
                print("radiosecond "+value.toString()+"radiovalue " +_radioValue.toString());
                _handleRadioValueChange(value);
                },
              ),
              new Text(
                'Dual Date',
                style: new TextStyle(
                  fontSize: 16.0,
                ),
              ),
            ],
          ),


        ],
      ),
    );
  }

}
[![enter image description here][1]][1]  ```

 


  [1]: https://i.stack.imgur.com/4yoFg.png

1 个答案:

答案 0 :(得分:1)

您可以在下面复制粘贴运行完整代码
您可以从int _radioValue = 0;中移出_handleRadioValueChangebuild

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(title: 'Flutter Demo', home: HomeView());
  }
}

class HomeView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: MyFloatingButton(),
    );
  }
}

class MyFloatingButton extends StatefulWidget {
  @override
  _MyFloatingButtonState createState() => _MyFloatingButtonState();
}

class _MyFloatingButtonState extends State<MyFloatingButton> {
  bool _show = true;
  int _radioValue = 0;
  /* var sheetController = showBottomSheet(
        context: context,
        builder: (context) => BottomSheetWidget());*/
  void _handleRadioValueChange(int value) {
    setState(() {
      _radioValue = value;
    });
    print("first" + value.toString() + "radiovalue" + _radioValue.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(10.0),
      child: Wrap(
        children: <Widget>[
          Center(
              child: Container(
                  height: 3.0, width: 40.0, color: Color(0xFF32335C))),
          SizedBox(
            height: 10.0,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Radio(
                value: 0,
                groupValue: _radioValue,
                onChanged: (value) {
                  setState(() {
                    _radioValue = value;
                  });
                  print("radiofirst" +
                      value.toString() +
                      "radiovalue" +
                      _radioValue.toString());
                  _handleRadioValueChange(value);
                },
              ),
              Text(
                'Single Date',
                style: TextStyle(fontSize: 16.0),
              ),
              Radio(
                value: 1,
                groupValue: _radioValue,
                onChanged: (value) {
                  setState(() {
                    _radioValue = value;
                  });
                  print("radiosecond " +
                      value.toString() +
                      "radiovalue " +
                      _radioValue.toString());
                  _handleRadioValueChange(value);
                },
              ),
              Text(
                'Dual Date',
                style: TextStyle(
                  fontSize: 16.0,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}