簡體   English   中英

Flutter:如何將抽屜頁眉寬度設置為最大?

[英]Flutter: How to set Drawer Header width to the max?

我在我的應用程序中添加了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),)),
            ])),

默認情況下,抽屜標題寬度不是最大。 它會根據內容大小進行調整,如果我增加字體大小或在左側和右側添加填充,則會占用更多寬度。

查看下面的圖片以供參考:

在此處輸入圖片說明

我做了一個這樣的人

在此處輸入圖片說明

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,
              ),
            ),
          ],
        ),
      )

我對代碼進行了更新,抽屜如下所示:

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,
                    ),
                  ),
                ],
              ),
            )

在此處輸入圖片說明

您必須在Flex內的ListView和ListView下面放置一個ListTile

[ 在此處輸入圖片說明]

 InkWell(
              onTap: () => debugPrint("home Page"),
              child: ListTile(
                title: Text("Home Page"),
                leading: Icon(Icons.home,color: Colors.red,),
              ),
            ),
            InkWell(
              onTap: () => debugPrint("My Account"),
              child: ListTile(
                title: Text("My Account"),
                leading: Icon(Icons.person,color: Colors.red,),
              ),
            ),
            InkWell(
              onTap: () => debugPrint("My Orders"),
              child: ListTile(
                title: Text("My Orders"),
                leading: Icon(Icons.shopping_basket,color: Colors.red,),
              ),
            ),
            InkWell(
              onTap: () {
//                Navigator.push(context, MaterialPageRoute(builder: (context)=> new ))
              },
              child: ListTile(
                title: Text("Shopping cart"),
                leading: Icon(Icons.shopping_cart,color: Colors.red,),
              ),
            ),
            InkWell(
              onTap: () => debugPrint("Favorite"),
              child: ListTile(
                title: Text("Favorite"),
                leading: Icon(Icons.favorite,color: Colors.red,),
              ),
            ),
            Divider(color: Colors.black,indent: 10.0,endIndent: 58.0,),
            InkWell(
              onTap: () => debugPrint("Setting"),
              child: ListTile(
                title: Text("Favorite"),
                leading: Icon(Icons.settings,color: Colors.green,),
              ),
            ),
            InkWell(
              onTap: () => debugPrint("Help"),
              child: ListTile(
                title: Text("Help"),
                leading: Icon(
                  Icons.help,
                  color: Colors.blue,
                ),
              ),
            ),
          ],
        ),
      ),

嘗試將您的DrawerHeader更改為以下給定的值

 DrawerHeader(
          padding: EdgeInsets.all(0),
          child: Container(
            color: Colors.redAccent,
          ),
        ),

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM