首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用类型函数并将其传递给ElevatedButton onPressed,Flutter

使用类型函数并将其传递给ElevatedButton onPressed,Flutter
EN

Stack Overflow用户
提问于 2021-05-23 12:29:42
回答 3查看 589关注 0票数 0

我是第一次使用Flutter + Dart。我基本上有以下几个类。首先,我有一个名为BottomForm类,其中有一个构建函数,当我在onPressed中调用函数类型变量时,该函数返回空问题。我有一个问题:参数类型‘ElevatedButton’不能赋值给参数类型'void function ()?‘。.dartargument_ type _not_assignable

代码语言:javascript
复制
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();
}

我在我们的Button的主类中有一个问题。当我传递函数functionApply时;

代码语言:javascript
复制
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ButtonForm extends StatelessWidget {
  final Function functionApply;

  final String textButton;

  ButtonForm(this.functionApply, this.textButton);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: ElevatedButton(
        child: Text(this.textButton),
        onPressed: this.functionApply, // I have a problem here!! 
      ),
    );
  }
}
EN

回答 3

Stack Overflow用户

发布于 2021-05-23 13:37:01

onPressed是VoidCallback的一种

代码语言:javascript
复制
typedef VoidCallback = void Function()

所以不是使用

代码语言:javascript
复制
final Function functionApply;

使用

代码语言:javascript
复制
final VoidCallback functionApply;

所以你的ButtonForm将会是

代码语言:javascript
复制
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!! 
      ),
    );
  }
}
票数 3
EN

Stack Overflow用户

发布于 2021-05-23 12:36:15

试试这个:

代码语言:javascript
复制
ElevatedButton(
  child: Text(this.textButton),
  onPressed: () {
    functionApply();
  },
)
票数 2
EN

Stack Overflow用户

发布于 2021-05-23 12:45:23

给出函数的返回类型。如果你没有提供任何返回类型,那么默认情况下返回类型为dynamic。但是onPressed函数的返回类型是空的,所以只要改变函数的减速就可以了。

代码语言:javascript
复制
final void Function() functionApply;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67656140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档