flutterdartflutter-buttonflutter-text-button

final VoidCallback press; (Textbutton, onPressed()) occurs error on Flutter


Hello I'm recently learning Flutter on VScode and i need to link 2 screens together via a Textbutton but it doesn't recognize itself when i put a function to move on to next screen. The button only displays properly when i put press: null in body.dart code.

press: () {} not working

this is a part of my body.dart code:

import 'package:shopping/components/default_button.dart';

  @override
  _BodyState createState() => _BodyState();
}
@override
  Widget build(BuildContext context) {
    return SafeArea(
      child: SizedBox(
        width: double.infinity,
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 3,
              child: PageView.builder(
                onPageChanged: (value) {
                  setState(() {
                    currentPage = value;
                  });
                },
                itemCount: splashData.length,
                itemBuilder: (context, index) => SplashContent(
                  image: splashData[index]["image"]!,
                  text: splashData[index]['text']!,
                ),
              ),
            ),
            Expanded(
              flex: 2,
              child: Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: getProportionateScreenWidth(20)),
                child: Column(
                  children: <Widget>[
                    const Spacer(),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: List.generate(
                        splashData.length,
                        (index) => buildDot(index: index),
                      ),
                    ),
                    const Spacer(flex: 3),
                    const DefaultButton(
                      text: "ok",
                      press: () {
                        Navigator.pushNamed(context, LoginPage.routeName); <<< press can't recognize () {}
                      },
                    ),
                    const Spacer(),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

and this is default_button.dart:

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

class DefaultButton extends StatelessWidget {
  const DefaultButton({
    Key? key,
    required this.text,
    required this.press,
  }) : super(key: key);
  final String? text;
  final VoidCallback press;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      height: getProportionateScreenHeight(56),
      child: TextButton(
        style: TextButton.styleFrom(
          foregroundColor: Colors.white,
          textStyle:
              const TextStyle(fontFamily: 'suite', fontWeight: FontWeight.w500),
          backgroundColor: const Color.fromRGBO(171, 204, 51, 1),
          shape: const StadiumBorder(),
        ),
        onPressed: press,
        child: Text(text!),
      ),
    );
  }
}

I put every packages correctly, checked the code that defined the settings for Textbutton, and changed final Function press; into > final VoidCallback press; but nothing happened.


Solution

  • just remove the const keyword

                        DefaultButton(
                          text: "ok",
                          press: () {
                            Navigator.pushNamed(context, LoginPage.routeName); <<< press can't recognize () {}
                          },
                        ),