Change Flutter Drawer Background Color
Asked Answered
D

12

54

How can I change the background color of a flutter nav drawer? There doesn't seem to be a color or background-color property.

Distraught answered 23/12, 2017 at 10:58 Comment(1)
Drawer documentation lists BackgroundColor property, but it doesn't seem to exist. Strange.Revelry
R
70

When you build your ListView in the child property of your Drawer, you can wrap your different sections of the Drawer inside a Container and use the color property of the Container.

enter image description here

drawer: new Drawer(
        child: new ListView(
          children: <Widget>[
            new Container(child: new DrawerHeader(child: new CircleAvatar()),color: Colors.tealAccent,),
            new Container (
              color: Colors.blueAccent,
              child: new Column(
                children: new List.generate(4, (int index){
                  return new ListTile(
                    leading: new Icon(Icons.info),
                  );
                }),
              ),
            )
          ],
        ),
      ),

A better alternative if you already have a consistent coloring design in your mind, is to define your ThemeData under the theme property of the root of your app, the DrawerHeader and the body will follow your canvasColor, so you need to override the value of one of them to change the color:

enter image description here

return new MaterialApp(
....
theme: new ThemeData(
       canvasColor: Colors.redAccent,

       ....),
)
Rosendorosene answered 23/12, 2017 at 11:36 Comment(1)
Thanks for the advice about ThemeData. IMHO, using consistent color scheme is a way to go here.Concepcion
O
51

Best way to wrap Drawer with Theme,

For example:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //other scaffold items
        drawer: Theme(
           data: Theme.of(context).copyWith(
                 canvasColor: Colors.blue, //This will change the drawer background to blue.
                 //other styles
              ),
              child: Drawer(
                 child: Column(
                    children: <Widget>[
                       //drawer stuffs
                    ],
                 ),
             ),
        );
  }
Orbit answered 30/3, 2019 at 8:55 Comment(2)
Only this works for transparent backgroundCasaubon
this doesn’t work to me, i dont now why, maybe because i have the drawer in a componentUnderhanded
L
18

The easiest way would probably be to just wrap the ListView inside a Container and specify its color like following:

drawer: Drawer(
  child: Container(color: Colors.red,
    child: new ListView(
      ...
    )
  )
)
Luminal answered 6/7, 2018 at 5:2 Comment(0)
B
9

For changing Drawer header color, you can use below code :


UserAccountsDrawerHeader(
  accountName: Text("Ashish Rawat"),
  accountEmail: Text("[email protected]"),
  decoration: BoxDecoration(
    color: const Color(0xFF00897b),
  ),
  currentAccountPicture: CircleAvatar(
    backgroundColor: Theme.of(ctxt).platform == TargetPlatform.iOS
        ? const Color(0xFF00897b)
        : Colors.white,
    child: Text(
      "A",
      style: TextStyle(fontSize: 40.0),
    ),
  ),
),
Burkett answered 4/4, 2019 at 10:42 Comment(0)
L
6

You can just use this code;

drawer: Drawer(
        child: Container(
          //child: Your widget,
          color: Colors.red,
          width: double.infinity,
          height: double.infinity,
        ),
      )
Latium answered 13/3, 2021 at 20:40 Comment(0)
N
3

PLAIN BACKGROUND

Just set your desired theme color using primarySwatch: Colors.brown property in ThemeData

class MyApp extends StatelessWidget {
  final appTitle = 'Drawer Demo';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: appTitle,
      theme: new ThemeData(
        primarySwatch: Colors.brown, // Your app THEME-COLOR
      ),
      home: MyHomePage(title: appTitle),
    );
  }
}

GRADIENT BACKGROUND Add the gradient property to AppBar.

@override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("profyl.org",
              style: TextStyle(color: Colors.white),
              textDirection: TextDirection.ltr),
          flexibleSpace: Container(
            decoration: new BoxDecoration(
              gradient: new LinearGradient(
                  colors: [
                    const Color(0xFF3366FF),
                    const Color(0xFF00CCFF),
                  ],
                  begin: const FractionalOffset(0.0, 0.0),
                  end: const FractionalOffset(1.0, 0.0),
                  stops: [0.0, 1.0],
                  tileMode: TileMode.clamp),
            ),
          ),
        ),
        body: HomeListPage(),
        drawer: DrawerPage());
  }

enter image description here enter image description here

Neoclassic answered 13/3, 2019 at 6:55 Comment(0)
D
3

Try This.

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Container(
        color: Colors.black,
        child: ListView(
          padding: const EdgeInsets.all(0),
          children: [

          ],
        ),
      ),
    );
  }
}
Dagenham answered 30/10, 2020 at 18:28 Comment(0)
F
2

This will help

 drawer: Drawer(
    child: Container(
      color: Colors.blueAccent,
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          UserAccountsDrawerHeader(
            decoration: BoxDecoration(
              color: Color(0xFF56ccf2),
            ),
            accountName: Text("User Name Goes"),
            accountEmail: Text("[email protected]"),
            currentAccountPicture: CircleAvatar(
              backgroundColor:
              Theme.of(context).platform == TargetPlatform.iOS
                  ? Color(0xFF56ccf2)
                  : Colors.white,
              child: Text("TK",
                style: TextStyle(fontSize: 50,
                  color: Colors.lightGreenAccent,),),
            ),
          ),
          ListTile(
            title: Text('Home',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                )),
            contentPadding: EdgeInsets.fromLTRB(20, 5, 0, 5),
            trailing: Icon(Icons.arrow_right,
              color: Colors.white,),
            onTap: () {
              Navigator.of(context).pop();
              Navigator.of(context).push(MaterialPageRoute(
                  builder: (BuildContext context) => HomeScreen()));
            },
          ),
        ],
      ),
    ),
  ),
Flavour answered 22/7, 2020 at 7:22 Comment(2)
As this is some pretty extensive code, you should probably add a little bit of explanation or related documents such as documentation.Kendalkendall
@FabianBettag Okay. I will do so next time I make an attempt to answer a question. ThanksFlavour
F
1

The simplest way:

Drawer(
  child: ListView(
    padding: EdgeInsets.zero,
    children: <Widget>[
      DrawerHeader(
         decoration: BoxDecoration(color:Theme.of(context).bottomAppBarColor),
    )],
  ),
)
Forewing answered 5/4, 2020 at 14:57 Comment(0)
F
1

You can wrap whatever you have in your drawer with a container wrapped with expanded widget. Thus you can change the color of the container there. Something like this will work.

Drawer(
    child: Expanded(
      child: Container(
       color: Colors.red,
       child: Text('Tabs'),
      ),
    ),
  )
Frimaire answered 10/8, 2020 at 12:40 Comment(0)
E
1

You can use DrawerThemeData:

return MaterialApp(
  theme: ThemeData(
   drawerTheme: const DrawerThemeData(
     backgroundColor: Color(0xff24191b),
   ),
  ),
 );
Ewald answered 20/11, 2023 at 21:59 Comment(0)
B
0

In drawer wrap Container on ListView or ListTile Data

Container(
  color: Colors.indigo,
  child: ListView.builder(....))

enter image description here

Biddable answered 29/2 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.