Flutter:如何将 Drawer Header 宽度设置为最大值?

Ale*_*ena 0 navigation-drawer drawerlayout flutter flutter-layout

我正在我的应用程序中添加抽屉,并遵循官方指南。我想将 Drawer 分为三个部分:

  1. 抽屉头(顶部)

  2. 部分项目

  3. 设置(底部)

如果我ListView是其直接子代,Drawer则它可以正常工作,但我无法在底部添加“设置”部分。我发现了这个,它符合我的要求,但DrawerHeader不占用最大宽度。

这是我的代码

      Drawer(
            elevation: 1.5,
            child: Column(children: <Widget>[
              DrawerHeader(
                  decoration: BoxDecoration(
                color: Colors.redAccent,
              )),
              Expanded(
                  child: ListView(
                padding: EdgeInsets.zero,
                children: <Widget>[
                  //list tiles
                ],
              )),
              Container(
                color: Colors.black,
                width: double.infinity,
                height: 0.1,
              ),
              Container(
                  padding: EdgeInsets.all(10),
                  height: 100,
                  child: Text("V1.0.0",style: TextStyle(fontWeight: FontWeight.bold),)),
            ])),
Run Code Online (Sandbox Code Playgroud)

默认情况下,抽屉标题宽度不是最大的。它根据内容大小进行调整,如果我增加字体大小或在左侧和右侧添加填充,则需要更多的宽度。

检查下图以供参考:

在此处输入图片说明

Luc*_*tos 5

我做了一个这样的

在此处输入图片说明

Drawer(
        child: ListView(
          children: <Widget>[
            UserAccountsDrawerHeader(
              decoration: BoxDecoration(
                color: new Color(0xFF0062ac),
              ),
              accountName: Text("Marcelo Augusto Elias"),
              accountEmail: Text("Matrícula: 5046850"),
              currentAccountPicture: CircleAvatar(
                backgroundColor:
                    Theme.of(context).platform == TargetPlatform.iOS
                        ? new Color(0xFF0062ac)
                        : Colors.white,
                child: Icon(
                  Icons.person,
                  size: 50,
                ),
              ),
            ),
            ListTile(
              dense: true,
              title: Text("Alternar Matrícula"),
              leading: new Image.asset(
                "assets/images/icon_alterna_matricula.png",
                width: 20.0,
              ),
            ),
            ListTile(onTap: (){
              Navigator.pop(context);
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => Contracheques()),
              );
            },
              dense: true,
              title: Text("Contracheque"),
              leading: new Image.asset(
                "assets/images/icon_contracheque.png",
                width: 20.0,
              ),
            ),
            ListTile(onTap: (){
              Navigator.pop(context);
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => AutorizacaoEmprestimos()),
              );
            },
              dense: true,
              title: Text("Autorização"),
              leading: new Image.asset(
                "assets/images/icon_autorizacao.png",
                width: 20.0,
              ),
            ),
            ListTile(onTap: (){

            },
              dense: true,
              title: Text("Gráficos/Relatórios"),
              leading: new Image.asset(
                "assets/images/icon_grafico.png",
                width: 20.0,
              ),
            ),
            ListTile(onTap: (){
              Navigator.pop(context);
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => HistoricoConsignacoes()),
              );
            },
              dense: true,
              title: Text("Consignações"),
              leading: new Image.asset(
                "assets/images/icon_consignacao.png",
                width: 20.0,
              ),
            ),
            ListTile(onTap: (){
              Navigator.pop(context);
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => ConsultaMargem()),
              );
            },
              dense: true,
              title: Text("Consulta de Margem"),
              leading: new Image.asset(
                "assets/images/icon_consulta_margem.png",
                width: 20.0,
              ),
            ),
            /* ListTile(
              dense: true,
              title: Text("Informe de Rendimentos"),
              leading: new Image.asset(
                "assets/images/icon_rendimento.png",
                width: 20.0,
              ),
            ), */
            ListTile(onTap: (){
              Navigator.pop(context);
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => SimularEmprestimos()),
              );
            },
              dense: true,
              title: Text("Simular Empréstimo"),
              leading: new Image.asset(
                "assets/images/Icon_cal.png",
                width: 20.0,
              ),
            ),
            Divider(),
            ListTile(
              dense: true,
              title: Text("Compartilhar"),
              leading: new Image.asset(
                "assets/images/icon_compartilhar.png",
                width: 20.0,
              ),
            ),
            ListTile(
              dense: true,
              title: Text("Avaliar"),
              leading: new Image.asset(
                "assets/images/icon_estrela.png",
                width: 20.0,
              ),
            ),
            Divider(),
            ListTile(onTap: (){
              Navigator.pop(context);

            },
              dense: true,
              title: Text("Sair"),
              trailing: Text(
                "Versão 2.0",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 12),
              ),
              leading: new Image.asset(
                "assets/images/icon_porta_sair.png",
                width: 20.0,
              ),
            ),
          ],
        ),
      )
Run Code Online (Sandbox Code Playgroud)

我对代码进行了更新,抽屉是这样的:

Drawer(
              child: Column(
                mainAxisAlignment: MainAxisAlignment
                    .spaceBetween, // place the logout at the end of the drawer
                children: <Widget>[
                  Flexible(
                    child: ListView(
                      shrinkWrap: true,
                      children: <Widget>[
                        UserAccountsDrawerHeader(
                          decoration: BoxDecoration(
                            color: myColour,
                          ),
                          accountName: Padding(
                            child: Row(
                              children: <Widget>[
                                Text("Marcelo Augusto Elias"),
                                IconButton(
                                  icon: Icon(
                                    Icons.edit,
                                    color: whiteColour,
                                  ),
                                  onPressed: () {},
                                ),
                              ],
                            ),
                            padding: EdgeInsets.only(top: 10),
                          ),
                          accountEmail: Text("N° Cartão: XXXX XXXX XXXX 5154"),
                          currentAccountPicture: CircleAvatar(
                            backgroundColor:
                                Theme.of(context).platform == TargetPlatform.iOS
                                    ? myColour
                                    : Colors.white,
                            child: Icon(
                              Icons.person,
                              size: 50,
                            ),
                          ),
                        ),
                        ListTile(
                          dense: true,
                          title: Text("Fatura"),
                          leading: new Image.asset(
                            "assets/images/icon_fatura_barra_menu.png",
                            width: 20.0,
                          ),
                          onTap: () {
                            Navigator.pop(context);
                            Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => Fatura()),
                            );
                          },
                        ),
                        ListTile(
                          onTap: () {
                            Navigator.pop(context);
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => ConsultaMargem()),
                            );
                          },
                          dense: true,
                          title: Text("Extrato"),
                          leading: new Image.asset(
                            "assets/images/icon_barra_menu_extrato.png",
                            width: 20.0,
                          ),
                        ),
                        ListTile(
                          onTap: () {
                            Navigator.pop(context);
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      DesbloquearPrimeiraVia()),
                            );
                          },
                          dense: true,
                          title: Text("Desbloquear Cartão"),
                          leading: new Image.asset(
                            "assets/images/icon_barra_menu_desbloquearcartao.png",
                            width: 24.0,
                          ),
                        ),
                        ListTile(
                          dense: true,
                          title: Text("Meu Cartão"),
                          leading: new Image.asset(
                            "assets/images/icon_barra_menu_meucartao.png",
                            width: 20.0,
                          ),
                          onTap: () {
                            Navigator.pop(context);
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => Contracheques()),
                            );
                          },
                        ),
                        ListTile(
                          onTap: () {
                            Navigator.pop(context);
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      HistoricoConsignacoes()),
                            );
                          },
                          dense: true,
                          title: Text("Adiantamento Salarial"),
                          leading: new Image.asset(
                            "assets/images/icon_barra_menu_adiantamentosalarial.png",
                            width: 20.0,
                          ),
                        ),
                        /* ListTile(
                    onTap: () {
                      Navigator.pop(context);
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => ConsultaMargem()),
                      );
                    },
                    dense: true,
                    title: Text("Consulta de Margem"),
                    leading: new Image.asset(
                      "assets/images/icon_consulta_margem.png",
                      width: 20.0,
                    ),
                  ), */
                        /* ListTile(
              dense: true,
              title: Text("Informe de Rendimentos"),
              leading: new Image.asset(
                "assets/images/icon_rendimento.png",
                width: 20.0,
              ),
            ), */
                        /* ListTile(
                    onTap: () {
                      Navigator.pop(context);
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => SimularEmprestimos()),
                      );
                    },
                    dense: true,
                    title: Text("Simular Empréstimo"),
                    leading: new Image.asset(
                      "assets/images/Icon_cal.png",
                      width: 20.0,
                    ),
                  ), */
                        Divider(),
                        ListTile(
                          dense: true,
                          title: Text("Compartilhar"),
                          leading: new Image.asset(
                            "assets/images/icon_compartilhar.png",
                            width: 20.0,
                          ),
                        ),
                        ListTile(
                          dense: true,
                          title: Text("Avaliar"),
                          leading: new Image.asset(
                            "assets/images/icon_estrela.png",
                            width: 20.0,
                          ),
                        ),
                        Divider(),
                      ],
                    ),
                  ),
                  ListTile(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    dense: true,
                    title: Text("Sair"),
                    trailing: Text(
                      "Versão 2.0",
                      style:
                          TextStyle(fontWeight: FontWeight.bold, fontSize: 12),
                    ),
                    leading: new Image.asset(
                      "assets/images/icon_porta_sair.png",
                      width: 20.0,
                    ),
                  ),
                ],
              ),
            )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

你必须在 ListView 和 ListView 下面放一个 ListTile 里面的 Flexible