【问题标题】:The argument type 'Function' can't be assigned to the parameter type 'void Function(bool?)?'参数类型“Function”不能分配给参数类型“void Function(bool?)?”
【发布时间】:2021-08-11 11:55:22
【问题描述】:

我正在尝试使用构造函数参数传递一个函数,但它显示了标题中提到的错误。

import 'package:flutter/material.dart';

class TaskTile extends StatefulWidget {
  @override
  _TaskTileState createState() => _TaskTileState();
}

class _TaskTileState extends State<TaskTile> {
  bool isChecked = false;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(
        'This is a task',
        style: TextStyle(
          decoration: isChecked ? TextDecoration.lineThrough : null,
        ),
      ),
      trailing: TaskCheckbox(
        checkboxState: isChecked,
        toggleCheckboxState: (bool checkboxState) {
          setState(() {
            isChecked = checkboxState;
          });
        },
      ),
    );
  }
}

class TaskCheckbox extends StatelessWidget {
  final bool checkboxState;
  final Function toggleCheckboxState;

  TaskCheckbox(
      {required this.checkboxState, required this.toggleCheckboxState});

  @override
  Widget build(BuildContext context) {
    return Checkbox(
      activeColor: Colors.lightBlueAccent,
      value: checkboxState,
      onChanged: toggleCheckboxState,
    );
  }
}

我在网上搜索过这个但没有运气。

所以如果有人可以帮助我,我会非常感激。

【问题讨论】:

    标签: flutter flutter-state statelesswidget


    【解决方案1】:

    面临同样的问题,下面是我喜欢使用的代码示例。 有关更多信息,请查看文档:Checkbox class

    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      static const String _title = 'Flutter Code Sample';
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: _title,
          home: Scaffold(
            appBar: AppBar(title: const Text(_title)),
            body: const Center(
              child: MyStatefulWidget(),
            ),
          ),
        );
      }
    }
    
    class MyStatefulWidget extends StatefulWidget {
      const MyStatefulWidget({Key? key}) : super(key: key);
    
      @override
      State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
    }
    
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      bool isChecked = false;
    
      @override
      Widget build(BuildContext context) {
        Color getColor(Set<MaterialState> states) {
          const Set<MaterialState> interactiveStates = <MaterialState>{
            MaterialState.pressed,
            MaterialState.hovered,
            MaterialState.focused,
          };
          if (states.any(interactiveStates.contains)) {
            return Colors.blue;
          }
          return Colors.red;
        }
    
        return Checkbox(
          checkColor: Colors.white,
          fillColor: MaterialStateProperty.resolveWith(getColor),
          value: isChecked,
          onChanged: (bool? value) {
            setState(() {
              isChecked = value!;
            });
          },
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      '我相信你和我也在看 Anjela Yu 的课程我找到了解决方案'@该关键字对 null 安全性感到失望

      import 'package:flutter/material.dart';
      
      class TaskTile extends StatefulWidget {
        @override
        _TaskTileState createState() => _TaskTileState();
      }
      
      class _TaskTileState extends State<TaskTile> {
        bool isChecked = false;
      
        @override
        Widget build(BuildContext context) {
          return ListTile(
            title: Text(
              'This is a task',
              style: TextStyle(
                decoration: isChecked ? TextDecoration.lineThrough : null,
              ),
            ),
            trailing: TaskCheckbox(
              checkboxState: isChecked,
              toggleCheckboxState: (bool? checkboxState) {
                setState(() {
                  isChecked = checkboxState!;
                });
              },
            ),
          );
        }
      }
      
      class TaskCheckbox extends StatelessWidget {
        final bool checkboxState;
        final Function? toggleCheckboxState;
      
        TaskCheckbox(
            {required this.checkboxState, required this.toggleCheckboxState});
      
        @override
        Widget build(BuildContext context) {
          return Checkbox(
            activeColor: Colors.lightBlueAccent,
            value: checkboxState,
            onChanged: toggleCheckboxState as void Function(bool?)?
          );
        }
      }
      

      【讨论】:

        【解决方案3】:
        `import 'dart:ui';
         import 'package:flutter/material.dart';
        
         class TaskTile extends StatefulWidget {
          @override
           _TaskTileState createState() => _TaskTileState();
             } 
        
        class _TaskTileState extends State<TaskTile> {
        bool isChecked = false;
        checkBoxCallback(bool checkBoxState) {
            setState(() {
              isChecked = checkBoxState;
              });
         }
        
         @override
         Widget build(BuildContext context) {
            return ListTile(
             title: Text(
              "Complete Assignments",
               style: TextStyle(
                decoration: isChecked ? TextDecoration.lineThrough : null,
               fontSize: 20,
             ),
           ),
           trailing: TaskCheckbox(
               checkBoxState: isChecked, toggleCheckBoxState: checkBoxCallback), );
            }
          }
        
          class TaskCheckbox extends StatelessWidget {
          final bool checkBoxState;
          final Function toggleCheckBoxState;
        
          const TaskCheckbox(
           {Key? key,
          required this.checkBoxState,
          required this.toggleCheckBoxState})
          : super(key: key);
        
        @override
        Widget build(BuildContext context) {
          return Checkbox(
            activeColor: Colors.blue,
            value: checkBoxState,
            onChanged: (newValue) {
              toggleCheckBoxState(newValue);
            });
          }
         }
        

        【讨论】:

          【解决方案4】:

          您的final Function toggleCheckboxState; 需要有一个bool? 参数。\

          试试这样:

          final Function(bool?) toggleCheckboxState;

          像这样分配toggleCheckboxState时还需要改代码,

          toggleCheckboxState: (bool? checkboxState) {
            setState(() {
              isChecked = checkboxState!;
            });
          },
          

          这里需要bool? checkboxState,因为您的类型是 Function(bool?)

          【讨论】:

          • 这个测试过吗?
          • @NisanthReddy 哦,我差点忘了那里的?。感谢您的编辑。
          • 这是一个救生员。它对我有用。谢谢你,达尔山。
          猜你喜欢
          • 2022-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-01
          • 2023-04-05
          • 1970-01-01
          • 2021-09-01
          • 1970-01-01
          相关资源
          最近更新 更多