抖动复选框在StatelessWidget中不起作用

时间:2019-12-21 17:08:04

标签: android flutter dart flutter-layout hybrid-mobile-app

这是我的课程

class Home extends StatelessWidget {

,然后复选框在此处。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(20.0),
              child: Column(
                children: <Widget>[
                  TextField(
                      controller: ctrlMotherName,
                      decoration: InputDecoration(
                          labelText: "Name of Mother",
                          border: OutlineInputBorder()
                      )
                  ),
                  SizedBox(height: 10,),
                  Checkbox(
                    value: false,
                    onChanged: (bool val){

                    },
                  ),

我无法选中该复选框。当我也使用单选按钮时,也会发现相同的问题。

2 个答案:

答案 0 :(得分:1)

由于要处理更改的值,因此需要使用StatefulWidget。我提供了一个示例:

class MyAppOne extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyAppOne> {
  bool _myBoolean = false;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Checkbox(
        value: _myBoolean,
        onChanged: (value) {
          setState(() {
            _myBoolean = value; // rebuilds with new value
          });
        },
      ),
    );
  }
}

答案 1 :(得分:0)

把 flutter 想象成 javascript 并将位置作为参数传递给列表构建器中的 medCheckedChanged 函数。当 dart 解析器对表达式或 lambda 函数求值时,它将使用位置参数作为值调用该方法。

class testWidget2 extends StatefulWidget {
    testWidget2({Key key}) : super(key: key);
    int numberLines = 50;
    List<bool> checkBoxValues = [];

    @override
   _testWidget2State createState() => _testWidget2State();
 }

 class _testWidget2State extends State<testWidget2> {
   _medCheckedChanged(bool value, int position) {
      setState(() => widget.checkBoxValues[position] = value);
  }

  @override
  void initState() {
  // TODO: implement initState
   super.initState();
  int i = 0;
   setState(() {
    while (i < widget.numberLines) {
    widget.checkBoxValues.add(false);
    i++;
   }
   });
  }

 @override
 Widget build(BuildContext context) {
  return Scaffold(
    body: Container(
        child: ListView.builder(
            itemCount: widget.checkBoxValues.length,
            itemBuilder: (context, position) {
              return Container(
                height: MediaQuery.of(context).size.width * .06,
                width: MediaQuery.of(context).size.height * .14,
                alignment: Alignment(0, 0),
                child: Checkbox(
                  activeColor: Color(0xff06bbfb),
                  value: widget.checkBoxValues[position],
                  onChanged: (newValue) {
                    _medCheckedChanged(newValue, position);
                  }, //pass to medCheckedChanged() the position
                ),
              );
            })));
   }
 }