当来自 initialRoute 的页面时触发来自 MaterialButton 的 onPressed

onPressed from MaterialButton were trigged when the page from initialRoute

我的 WelcomeScreen 有两个按钮,加载此页面时,将自动按下两个按钮。 外部小部件“RoundButton”使用了两个按钮。 为什么我知道那些按钮被按下了,因为我正在使用打印功能,我看到第一个按钮和第二个按钮被自动按顺序按下。

  1. Navigator.pushNamed(上下文,LoginScreen2.id);
  2. Navigator.pushNamed(上下文,RegistrationScreen.id);
  3. Navigator.pushNamed(上下文,LoginScreen2.id);
  4. Navigator.pushNamed(上下文,RegistrationScreen.id);

有什么设置可以避免这个问题吗?谢谢。

环境: SDK:“>=2.16.0 <3.0.0” 在 Chroma

上构建此程序

welcome_screen.dart

import 'package:trashcollectionday/Screens/login_screen2.dart';
import 'package:trashcollectionday/screens/registration_screen.dart';
import 'package:flutter/material.dart';
import 'package:trashcollectionday/screens/login_screen.dart';
import 'package:animated_text_kit/animated_text_kit.dart';
import 'package:trashcollectionday/components/roundedButton.dart';

class WelcomeScreen extends StatefulWidget {

  static const String id = 'welcome_screen';
  // will be a class, you don't have to to WelcomeScreen().index

  @override
  _WelcomeScreenState createState() => _WelcomeScreenState();
}

class _WelcomeScreenState extends State<WelcomeScreen> with SingleTickerProviderStateMixin {
  late AnimationController controller;
  late Animation animation;
  late Animation animation1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 24.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Row(
              children: <Widget>[
                Hero(
                  tag: 'logo',
                  child: Container(
                    child: Image.asset('images/logo.png'),
                    height: 60,
                  ),
                ),
                DefaultTextStyle(
                  style: const TextStyle(
                    fontSize: 40.0,
                    fontFamily: 'Horizon',
                  ),
                  child: AnimatedTextKit(
                    animatedTexts: [TypewriterAnimatedText('Application')],
                  ),
                ),
              ],
            ),
            const SizedBox(
              height: 48.0,
            ),
            RoundButton(
              title: 'Log In',
              colour: Colors.lightBlue,
              onPressed: () {
                print('Log In');
                //Go to login screen.
                Navigator.pushNamed(context, LoginScreen2.id);
              },
            ),
            RoundButton(
              title: 'Register',
              colour: Colors.blueAccent,
              onPressed: () {
                print('Reg');
                    //Go to login screen.
                Navigator.pushNamed(context, RegistrationScreen.id);
              },
            ),
          ],
        ),
      ),
    );
  }
}


roundedButton.dart

import 'package:flutter/material.dart';

class RoundButton extends StatelessWidget {
  const RoundButton({required this.title, required this.colour, required this.onPressed});
  final Color colour;
  final String title;
  final Function onPressed;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        elevation: 5.0,
        color: colour,
        borderRadius: BorderRadius.circular(30.0),
        child: MaterialButton(
          onPressed: onPressed(),
          minWidth: 200.0,
          height: 42.0,
          child: Text(
            title,
          ),
        ),
      ),
    );
  }
}

主要。飞镖

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: Colors.teal,
        textTheme: const TextTheme(bodyText2: TextStyle(color: Colors.yellow)),
        primaryColor: Colors.orange,
      ),
      initialRoute: WelcomeScreen.id,
      // home: ItemDetailsScrrent(),
      routes: {
        WelcomeScreen.id : (context) => WelcomeScreen(),
        LoginScreen2.id: (context) => LoginScreen2(),
        RegistrationScreen.id: (context) => RegistrationScreen(),
        RecordScreen.id: (context) => RecordScreen(),
        LoginScreen.id: (context) => LoginScreen(),
        ItemDetailsScrrent.id: (context) => ItemDetailsScrrent(),
      },
      // home: const LoginScreen(),
    );
  }
}

我不确定,但可能是因为您在 RoundButton 中使用 onPressed: onPressed() 导致出现问题。

你可以像这样不用括号使用这个函数; onPressed:onPressed,

您应该从

中的 RoundButton class 中删除括号
onPressed: onPressed() 

   onPressed:onPressed

并在这行代码中添加括号

final Function onPressed;

如这里

 final Function() onPressed;