【问题标题】:CheckboxListTile - too many positional argumentsCheckboxListTile - 位置参数太多
【发布时间】:2021-11-25 13:50:18
【问题描述】:

我正在尝试添加一个CheckboxListTile,并从flutter dev 中找到了复选框代码,并尝试在我的小部件中实现它,如下所示。但我得到错误

错误:too many positional argumentspositional arguments must occur before named arguments

    import 'package:flutter/material.dart';

     @override
  Widget build(BuildContext context) {
     bool _checked = true;
    return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
                    
                  Container(
                    
                    width: widget.sentenceWidth,
                    child: CheckboxListTile(
                      title: Text('me, the label'),
                      controlAffinity: ListTileControlAffinity.leading,
                     value: _checked ,
                      onChanged: (bool value) setState(() {
                      _checked =  value;
                      }
                  ),),),

【问题讨论】:

    标签: flutter


    【解决方案1】:

    这是工作代码:

    您已将 bool 作为变量类型传递给 onChange。我还更改了宽度参数,因此还要检查该参数是否传递错误。

      @override
      Widget build(BuildContext context) {
        bool _checked = true;
        return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                  width: 500,
                  child: CheckboxListTile(
                    title: const Text('me, the label'),
                    controlAffinity: ListTileControlAffinity.leading,
                    value: _checked,
                    onChanged: (value) {
                      print(value);
                    },
                  ))
            ]);
      }
    

    试试这个,如果遇到任何问题,请告诉我。

    【讨论】:

    • bool _checked = true;在构建上下文下,它是不可更新的,因为它在 buildContext 中使用,需要在 buildContext 上声明
    猜你喜欢
    • 2020-08-10
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2023-01-14
    • 2019-10-22
    • 1970-01-01
    • 2021-09-21
    相关资源
    最近更新 更多