dart 为什么当我点击Flutter应用导航栏中的设置按钮时,设置页面没有显示?

ssgvzors  于 5个月前  发布在  Flutter
关注(0)|答案(2)|浏览(75)

错误1:
常量值无效。
错误2:
const列表中的值必须是常量。
尝试从列表文字中删除关键字'const'。
以下代码中出现了以下错误(在Navigator.pushNamed...行):

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:my_app/global/common/toast.dart';

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

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("SALARY CALCULATOR"),
          centerTitle: true,
        ),
        drawer: const Drawer(
          backgroundColor: Color.fromARGB(255, 22, 116, 192),
          child: Column(
            children: [
              DrawerHeader(
                child: Icon(
                  Icons.wallet,
                  color: Colors.white,
                  size: 45,
                ),
              ),
              ListTile(
                leading: Icon(Icons.settings),
                title: Text('S E T T I N G S'),
                onTap: () {
                  Navigator.pushNamed(context, '/settingspage');
                }, 
              )
            ],
          ),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const SizedBox(height: 30),
            GestureDetector(
              onTap: () {
                FirebaseAuth.instance.signOut();
                Navigator.pushNamed(context, "/login");
                showToast(message: "Successfully signed out");
              },
              child: Container(
                height: 45,
                width: 100,
                decoration: BoxDecoration(
                  color: Colors.blue,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: const Center(
                  child: Text(
                    "Sign out",
                    style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 18,
                    ),
                  ),
                ),
              ),
            )
          ],
        ));
  }
}

字符串
在应用程序的那一刻,我设法在firebase的帮助下完成了身份验证部分,通过注册和登录,在您创建帐户后,它会将您引导到home_page.dart,其中顶部有一个AppBar,上面有应用程序的名称,我还创建了一个导航抽屉,如果您按下,一个窗口打开,我有应用程序徽标+设置页面和个人资料页面,问题是,当我点击设置它不直接我到相应的页面
a picture with a part of the code and all the files and folders in the left
and this one is with code from main.dart but I don't know what to change to get rid of that error/problem

qni6mghb

qni6mghb1#

在第20行,你创建了一个Drawer,它的常量构造函数,所以它的子元素也必须是常量。所以Column也是用它的常量构造函数构造的,现在它的子元素必须是常量。不幸的是,ListTile不能是常量,因为一个回调被传递给了它的onTap参数,而且这个回调函数不是一个常量。所以解决方案是从第20行的Drawer构造函数中删除const

drawer: Drawer(

字符串
你可以保持DrawerHeader不变,所以在第24行,把const关键字放在构造函数之前:

const DrawerHeader(

vsikbqxv

vsikbqxv2#

在你的代码中,你把Drawer设为const,它的子元素是column,它的一个子元素是listtile。listtile的属性ontap只有在它的父元素没有const的时候才有效。所以从drawer中删除const就可以解决你的问题。

相关问题