Flutter 使用类型功能,并将其传递到按下的提升按钮,颤振

Flutter 使用类型功能,并将其传递到按下的提升按钮,颤振,flutter,flutter-layout,Flutter,Flutter Layout,我是个新来的飞镖手。我基本上有以下几节课。 首先,我有一个名为BottomForm的clas,其中有一个生成函数,当我在onPressed中调用函数类型变量时,该函数返回ElevatedButton问题,我有一个问题说: 无法将参数类型“Function”分配给参数类型“void Function()?”。参数类型不可分配 import 'formbutton.dart'; // Define a corresponding State class. // This class holds t

我是个新来的飞镖手。我基本上有以下几节课。 首先,我有一个名为BottomForm的clas,其中有一个生成函数,当我在onPressed中调用函数类型变量时,该函数返回ElevatedButton问题,我有一个问题说: 无法将参数类型“Function”分配给参数类型“void Function()?”。参数类型不可分配

import 'formbutton.dart';

// Define a corresponding State class.
// This class holds the data related to the Form.
class _MyCustomFormState extends State<MyCustomForm> {
  // Create a text controller and use it to retrieve the current value
  // of the TextField.
  final email = TextEditingController();
  final password = TextEditingController();

  void _logIn() {
    
    print("Logged In.");
  }

  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    email.dispose();
    password.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: TextFormField(
              autocorrect: true,
              controller: email,
            ),
          ),
          ButtonForm(_logIn, "Hello"),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
              context: context,
              builder: (context) {
                return AlertDialog(
                  content: Text(email.text),
                );
              });
        },
        tooltip: "Show me the value",
        child: Icon(Icons.text_fields),
      ),
    );
  }
}

//Define a Custom Widget
class MyCustomForm extends StatefulWidget {
  @override
  _MyCustomFormState createState() => _MyCustomFormState();
}
试试这个:

ElevatedButton(
  child: Text(this.textButton),
  onPressed: () {
    functionApply();
  },
)

给出函数的返回类型。如果您没有给出任何返回类型,那么默认情况下,返回类型将是
动态
。但是
onPressed
函数的返回类型为void。所以只要改变减速功能,它就会很好地工作

final void Function() functionApply;

onPressed是一种类型的VoidCallback

typedef VoidCallback = void Function()
因此,与其使用

final Function functionApply;
使用

所以你的纽扣形状是

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ButtonForm extends StatelessWidget {
  final VoidCallback functionApply;

  final String textButton;

  ButtonForm(this.functionApply, this.textButton);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        child: Text(textButton),
        onPressed: functionApply, // Problem Solved!! 
      ),
    );
  }
}

不客气。
final VoidCallback functionApply;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ButtonForm extends StatelessWidget {
  final VoidCallback functionApply;

  final String textButton;

  ButtonForm(this.functionApply, this.textButton);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        child: Text(textButton),
        onPressed: functionApply, // Problem Solved!! 
      ),
    );
  }
}