Flutter参数通过

7rfyedvj  于 2023-01-06  发布在  Flutter
关注(0)|答案(2)|浏览(113)

我试图传递参数到一个按钮小部件,但我得到下面的错误消息。
下面是我的观点:

ElevatedRegisterButton(
                navigator: Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
              return const RegisterPage1();
            })))

这是我的小部件,我试图传递参数:

import 'package:flutter/material.dart';

class ElevatedRegisterButton extends StatefulWidget {
  const ElevatedRegisterButton({super.key, required this.navigator});

  final String navigator;

  @override
  State<ElevatedRegisterButton> createState() => _ElevatedRegisterButtonState();
}

class _ElevatedRegisterButtonState extends State<ElevatedRegisterButton> {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
      onPressed: () {
        widget.navigator;
      },
      child: const Text('Register'),
    );
  }
}

下面是我收到的错误消息:
参数类型“Future”不能分配给参数类型“String”。

avwztpqn

avwztpqn1#

您需要将navigator成员设置为final void Function()类型,因为ElevatedButtononPressed属性需要此类型。您还需要以不同的方式传递它,因为push是另一种类型的函数。最后,只需将onPressed设置为widget.navigator
下面是基于您的代码片段的示例代码:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) =>
      const MaterialApp(home: Scaffold(body: HomePage()));
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) => SafeArea(
        // see the difference, push is within () {}
        child: ElevatedRegisterButton(navigator: () {
          Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const RegisterPage1()));
        }),
      );
}

class ElevatedRegisterButton extends StatefulWidget {
  const ElevatedRegisterButton({super.key, required this.navigator});

  final void Function() navigator;

  @override
  State<ElevatedRegisterButton> createState() => _ElevatedRegisterButtonState();
}

class _ElevatedRegisterButtonState extends State<ElevatedRegisterButton> {
  @override
  Widget build(BuildContext context) => ElevatedButton(
        style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
        // here simply set the function
        onPressed: widget.navigator,
        child: const Text('Register'),
      );
}

// added for demo purposes
class RegisterPage1 extends StatelessWidget {
  const RegisterPage1({super.key});

  @override
  Widget build(BuildContext context) =>
      const Scaffold(body: SafeArea(child: Text('RegisterPage1')));
}
ht4b089n

ht4b089n2#

    • 问题1**

在您的代码中:

class ElevatedRegisterButton extends StatefulWidget {
  const ElevatedRegisterButton({super.key, required this.navigator});

  // this line
  final String navigator;

您正在接受String navigator;但是你传递给它的是一个Future

ElevatedRegisterButton(
                navigator: Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
              return const RegisterPage1();
            })))
    • 问题2**

如果您尝试运行代码,则会得到一个错误:

setState() or markNeedsBuild called during build

因此,要解决这些问题,请按如下所示重构代码:

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light().copyWith(),
      debugShowCheckedModeBanner: false,
      home: SafeArea(
        child: Scaffold(
            body: ElevatedRegisterButton(navigator: Navigator.of(context))),
      ),
    );
  }
}

class ElevatedRegisterButton extends StatefulWidget {
  const ElevatedRegisterButton({super.key, required this.navigator});

  final NavigatorState navigator;
  @override
  State<ElevatedRegisterButton> createState() => _ElevatedRegisterButtonState();
}

class _ElevatedRegisterButtonState extends State<ElevatedRegisterButton> {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
      onPressed: () {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          widget.navigator.push(MaterialPageRoute(
            builder: (context) => Text("h1"),
          ));
        });
      },
      child: const Text('Register'),
    );
  }
}
另请参见

setState() or markNeedsBuild called during build

相关问题