Drawer all white, nothing shows up

Asked

Viewed 132 times

1

I’m having trouble creating the SiderBar. The Drawer nothing appears. Everything is blank.

My man who calls this file is:

import 'package:Cardapio/consts/consts_app.dart';
import 'package:flutter/material.dart';

class AppBarHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    return Container(
      child: Scaffold(
        floatingActionButton: Padding(
          padding: EdgeInsets.only(right: 10, top: 60),
          child: FloatingActionButton(
            child: Icon(Icons.menu),
            backgroundColor: Colors.red,
            onPressed: () {
              Scaffold.of(context).openDrawer();
            },
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(150),
          child: Stack(
            children: <Widget>[
              Positioned(
                top: -(240 / 3.5),
                left: screenWidth - (240 / 1.57),
                child: Opacity(
                  child:
                      Image.asset(ConstsApp.darkFire, height: 200, width: 200),
                  opacity: 0.2,
                ),
              ),
            ],
          ),
        ),
        drawer: Drawer(
          child: ListView(
            children: <Widget>[
              DrawerHeader(
                child: Text('Drawer Header'),
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
              ),
              ListTile(
                title: Text('Item 1'),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
              ListTile(
                title: Text('Item 2'),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Barra toda branca.

1 answer

1


The problem is the mode that is calling Drawer. To work add a key in his Scaffold and then call it by clicking the button.

Example

import 'package:flutter/material.dart';

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: AppBarHome(),
        ),
      ),
    );
  }
}

class AppBarHome extends StatelessWidget {
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    return Container(
      child: Scaffold(
        key: _scaffoldKey,
        floatingActionButton: Padding(
          padding: EdgeInsets.only(right: 10, top: 60),
          child: FloatingActionButton(
            child: Icon(Icons.menu),
            backgroundColor: Colors.red,
            onPressed: () {
              _scaffoldKey.currentState.openDrawer();              
            },
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
        appBar: AppBar(title: Text("Titulo")),
        drawer: Drawer(
          child: ListView(
            children: <Widget>[
              DrawerHeader(
                child: Text('Drawer Header'),
                decoration: BoxDecoration(
                  color: Colors.blue,
                ),
              ),
              ListTile(
                title: Text('Item 1'),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
              ListTile(
                title: Text('Item 2'),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

So it works normally yours Drawer.

I noticed that in the image posted in the question there are particularities that were not shown in your code, such as the non-existence of AppBar. I added it to the screen just so the code could be working on Dartpad.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.