使用 flutter_zoom_drawer 导航到页面时出现问题

问题描述 投票:0回答:1

我使用 pub dev 的

flutter_zoom_drawer
,一切都很好。但是当我尝试从菜单更改页面并下次尝试时,我遇到了麻烦,因为菜单打不开。

抽屉代码:

import 'package:flutter/material.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:natureleaf/pages/home_page.dart';
import 'package:natureleaf/pages/menu_page.dart';

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

  @override
  State<AnimatedDrawe> createState() => _AnimatedDraweState();
}

class _AnimatedDraweState extends State<AnimatedDrawe> {
  final ZoomDrawerController z = ZoomDrawerController();
  @override
  Widget build(BuildContext context) {
    return ZoomDrawer(
        controller: z,
        angle: -15,
        mainScreen: HomePage(controller: z),
        mainScreenTapClose: true,
        menuScreen: MenuScreen(controller: z),
        openCurve: Curves.fastOutSlowIn,
        borderRadius: 30,
        moveMenuScreen: true,
        slideWidth: MediaQuery.of(context).size.width * 0.8,
        showShadow: true,
        shadowLayer1Color: const Color.fromARGB(255, 27, 56, 34),
        shadowLayer2Color: const Color.fromARGB(255, 38, 62, 43));
  }
}

菜单页代码:

// ignore_for_file: use_build_context_synchronously

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:natureleaf/functions/toast_utils.dart';
import 'package:natureleaf/pages/home_page.dart';
import 'package:natureleaf/pages/login_page.dart';
import 'package:natureleaf/pages/products_page.dart';

class MenuScreen extends StatefulWidget {
  final ZoomDrawerController controller;
  const MenuScreen({super.key, required this.controller});

  @override
  State<MenuScreen> createState() => _MenuScreenState();
}

class _MenuScreenState extends State<MenuScreen> {
  String name = '';
  String vorname = '';
  final FirebaseAuth _auth = FirebaseAuth.instance;
  @override
  void initState() {
    name = UserData.name;
    vorname = UserData.vorname;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 39, 74, 48),
      body: ListView(
        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 40),
        children: [
          const SizedBox(height: 40),
          ClipOval(
            child: Image.asset(
              'assets/images/avatar.png',
              width: 70,
              height: 70,
              color: Colors.white30,
            ),
          ),
          const SizedBox(height: 10),
          Center(
            child: Text(
              '$name $vorname',
              textAlign: TextAlign.center,
              style: GoogleFonts.lato(color: Colors.white60, fontSize: 15),
            ),
          ),
          const SizedBox(height: 30),
          GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => HomePage(controller: widget.controller),
                ),
              );
            },
            child: ListTile(
              leading: const Icon(
                Icons.home,
                color: Colors.white,
              ),
              title: Text(
                'Strona główna',
                style: GoogleFonts.lato(color: Colors.white, fontSize: 15),
              ),
            ),
          ),
          GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => ProductsPage(
                    controller: widget.controller,
                  ),
                ),
              );
            },
            child: ListTile(
              leading: const Icon(
                Icons.inventory,
                color: Colors.white,
              ),
              title: Text(
                'Lista produktów',
                style: GoogleFonts.lato(color: Colors.white, fontSize: 15),
              ),
            ),
          ),
          ListTile(
            leading: const Icon(
              Icons.error,
              color: Colors.white,
            ),
            title: Text(
              'Zgłoś produkt',
              style: GoogleFonts.lato(color: Colors.white, fontSize: 15),
            ),
          ),
          GestureDetector(
            onTap: () {
              _signOut();
            },
            child: ListTile(
              leading: const Icon(
                Icons.logout,
                color: Colors.white,
              ),
              title: Text(
                'Wyloguj się',
                style: GoogleFonts.lato(color: Colors.white, fontSize: 15),
              ),
            ),
          )
        ],
      ),
    );
  }

  Future<void> _signOut() async {
    try {
      await _auth.signOut();
      ToastUtils.showToast(context, 'Pomyślnie wylogowano z konta.', 'Success');
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => const LoginPage()),
      );
    } catch (e) {
      ToastUtils.showToast(
          context, 'Nieznany błąd podczas wylogowywania.', 'Warn');
    }
  }
}

示例页面代码(主页)

// ignore_for_file: use_build_context_synchronously

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:natureleaf/functions/button.dart';
import 'package:natureleaf/functions/toast_utils.dart';
import 'package:natureleaf/pages/login_page.dart';
import 'package:natureleaf/pages/products_page.dart';

class HomePage extends StatefulWidget {
  final ZoomDrawerController controller;
  const HomePage({super.key, required this.controller});

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

class _HomePageState extends State<HomePage> {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: const Color.fromARGB(255, 39, 74, 48),
        body: PopScope(
          canPop: false,
          child: SafeArea(
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Align(
                      alignment: Alignment.centerLeft,
                      child: GestureDetector(
                        onTap: () {
                          widget.controller.toggle!();
                        },
                        child: const Icon(
                          Icons.menu,
                          color: Colors.white,
                          size: 30,
                        ),
                      ),
                    ),
                    const SizedBox(height: 20),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Image.asset(
                          'assets/images/avatar.png',
                          width: 70,
                          height: 70,
                          color: Colors.white30,
                        ),
                        Container(
                          height: 50,
                          width: 1,
                          color: Colors.white,
                          margin: const EdgeInsets.symmetric(horizontal: 10),
                        ),
                        SizedBox(
                          width: 150,
                          child: RichText(
                            textAlign: TextAlign.start,
                            text: TextSpan(
                              style: GoogleFonts.lato(),
                              children: <TextSpan>[
                                TextSpan(
                                  text:
                                      '${UserData.name} ${UserData.vorname}\n',
                                  style: GoogleFonts.lato(
                                      fontSize: 15,
                                      color: Colors.white.withOpacity(0.8),
                                      fontWeight: FontWeight.normal),
                                ),
                                TextSpan(
                                    text: UserData.admin == 1
                                        ? 'Zarządca\n'
                                        : 'Pracownik\n',
                                    style: GoogleFonts.lato(
                                        fontSize: 12,
                                        color: UserData.admin == 1
                                            ? Colors.red.withOpacity(0.8)
                                            : Colors.white.withOpacity(0.5),
                                        fontWeight: FontWeight.normal)),
                                TextSpan(
                                    text: UserData.shop,
                                    style: GoogleFonts.lato(
                                        fontSize: 15,
                                        color: Colors.white.withOpacity(1),
                                        fontWeight: FontWeight.normal)),
                              ],
                            ),
                          ),
                        ),
                        const SizedBox(width: 50),
                        GestureDetector(
                          onTap: () {
                            _signOut();
                          },
                          child: Column(
                            children: [
                              const Icon(
                                Icons.logout,
                                color: Colors.white54,
                                size: 30,
                              ),
                              Text(
                                'Wyloguj',
                                style: GoogleFonts.lato(color: Colors.white54),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                    const SizedBox(height: 20),
                    Divider(
                      color: Colors.white.withOpacity(0.2),
                      height: 0,
                      thickness: 1,
                    ),
                    const SizedBox(height: 20),
                    Center(
                      child: Text(
                        'Lista produktów dostępna w przypisanym sklepie oraz zgłoszenie sprzedaży go',
                        textAlign: TextAlign.center,
                        style: GoogleFonts.lato(
                          color: Colors.white.withOpacity(0.5),
                          fontSize: 15,
                        ),
                      ),
                    ),
                    const SizedBox(height: 10),
                    FancyButton(
                        icon: Icons.inventory,
                        text: 'Lista produktów',
                        onPressed: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => ProductsPage(
                                    controller: widget.controller)),
                          );
                        }),
                    const SizedBox(height: 20),
                    Center(
                      child: Text(
                        'Zgłoszenie braku danego produktu w sklepie',
                        textAlign: TextAlign.center,
                        style: GoogleFonts.lato(
                          color: Colors.white.withOpacity(0.5),
                          fontSize: 15,
                        ),
                      ),
                    ),
                    const SizedBox(height: 10),
                    FancyButton(
                        icon: Icons.error,
                        text: 'Zgłoś produkt',
                        onPressed: () {}),
                    const SizedBox(height: 20),
                    Center(
                      child: Text(
                        'Sprawdź ilość sprzedaży w danym miesiącu oraz ogólnie',
                        textAlign: TextAlign.center,
                        style: GoogleFonts.lato(
                          color: Colors.white.withOpacity(0.5),
                          fontSize: 15,
                        ),
                      ),
                    ),
                    const SizedBox(height: 10),
                    FancyButton(
                        icon: Icons.person,
                        text: 'Statystyki pracownika',
                        onPressed: () {}),
                    Visibility(
                      visible: UserData.admin == 1 ? true : false,
                      child: Column(
                        children: [
                          const SizedBox(height: 20),
                          Center(
                            child: Text(
                              'Lista pracowników oraz zarządców zatrudnionych w firmie i zarządzanie nimi',
                              textAlign: TextAlign.center,
                              style: GoogleFonts.lato(
                                color: Colors.white.withOpacity(0.5),
                                fontSize: 15,
                              ),
                            ),
                          ),
                          const SizedBox(height: 10),
                          FancyButton(
                              icon: Icons.contacts,
                              text: 'Lista pracowników',
                              onPressed: () {}),
                        ],
                      ),
                    ),
                    Visibility(
                      visible: UserData.admin == 1 ? true : false,
                      child: Column(
                        children: [
                          const SizedBox(height: 20),
                          Center(
                            child: Text(
                              'Stany magazynowe we wszystkich dostępnych sklepach',
                              textAlign: TextAlign.center,
                              style: GoogleFonts.lato(
                                color: Colors.white.withOpacity(0.5),
                                fontSize: 15,
                              ),
                            ),
                          ),
                          const SizedBox(height: 10),
                          FancyButton(
                              icon: Icons.dynamic_feed,
                              text: 'Stany magazynowe',
                              onPressed: () {}),
                        ],
                      ),
                    ),
                    Visibility(
                      visible: UserData.admin == 1 ? true : false,
                      child: Column(
                        children: [
                          const SizedBox(height: 20),
                          Center(
                            child: Text(
                              'Uzupełnij stan magazynowy danego produktu w danym sklepie',
                              textAlign: TextAlign.center,
                              style: GoogleFonts.lato(
                                color: Colors.white.withOpacity(0.5),
                                fontSize: 15,
                              ),
                            ),
                          ),
                          const SizedBox(height: 10),
                          FancyButton(
                              icon: Icons.incomplete_circle,
                              text: 'Uzupełnij produkt',
                              onPressed: () {}),
                        ],
                      ),
                    ),
                    Visibility(
                      visible: UserData.admin == 1 ? true : false,
                      child: Column(
                        children: [
                          const SizedBox(height: 20),
                          Center(
                            child: Text(
                              'Dodaj nowy produkt do sprzedaży',
                              textAlign: TextAlign.center,
                              style: GoogleFonts.lato(
                                color: Colors.white.withOpacity(0.5),
                                fontSize: 15,
                              ),
                            ),
                          ),
                          const SizedBox(height: 10),
                          FancyButton(
                              icon: Icons.add,
                              text: 'Dodaj produkt',
                              onPressed: () {}),
                        ],
                      ),
                    ),
                    Visibility(
                      visible: UserData.admin == 1 ? true : false,
                      child: Column(
                        children: [
                          const SizedBox(height: 20),
                          Center(
                            child: Text(
                              'Zdejmij produkt ze stanu w danym sklepie',
                              textAlign: TextAlign.center,
                              style: GoogleFonts.lato(
                                color: Colors.white.withOpacity(0.5),
                                fontSize: 15,
                              ),
                            ),
                          ),
                          const SizedBox(height: 10),
                          FancyButton(
                              icon: Icons.remove,
                              text: 'Usuń produkt',
                              onPressed: () {}),
                        ],
                      ),
                    ),
                    Visibility(
                      visible: UserData.admin == 1 ? true : false,
                      child: Column(
                        children: [
                          const SizedBox(height: 20),
                          Center(
                            child: Text(
                              'Dodawanie nowego konta do bazy danych aplikacji firmowej',
                              textAlign: TextAlign.center,
                              style: GoogleFonts.lato(
                                color: Colors.white.withOpacity(0.5),
                                fontSize: 15,
                              ),
                            ),
                          ),
                          const SizedBox(height: 10),
                          FancyButton(
                              icon: Icons.person,
                              text: 'Dodaj pracownika',
                              onPressed: () {}),
                        ],
                      ),
                    ),
                    const SizedBox(height: 20),
                  ],
                ),
              ),
            ),
          ),
        ));
  }

  Future<void> _signOut() async {
    try {
      await _auth.signOut();
      ToastUtils.showToast(context, 'Pomyślnie wylogowano z konta.', 'Success');
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => const LoginPage()),
      );
    } catch (e) {
      ToastUtils.showToast(
          context, 'Nieznany błąd podczas wylogowywania.', 'Warn');
    }
  }
}

那么我如何才能顺利地从菜单正确导航到另一个页面?

我尝试了不同的选项来导航到该页面,但没有成功。

flutter dart drawer
1个回答
0
投票

那么,有人可以帮助解决这个问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.