【问题标题】:Flutter: How to set Drawer Header width to the max?Flutter:如何将抽屉页眉宽度设置为最大?
【发布时间】:2019-09-11 11:25:54
【问题描述】:

我正在我的应用程序中添加抽屉,并关注official guideline。我想把抽屉分成三个部分:

  1. 抽屉顶部(顶部)

  2. 一些项目

  3. 设置(在底部)

如果我有ListView 作为Drawer 的直接子代,那么它可以正常工作,但我无法在底部添加 设置 部分。我找到了this,它符合我的要求,但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),)),
            ])),

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

查看下图以供参考:

【问题讨论】:

    标签: flutter navigation-drawer flutter-layout drawerlayout


    【解决方案1】:

    我做了一个这样的

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

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

    【讨论】:

    • 感谢分享,标题已用您的代码修复。我在抽屉里有大约 4 个列表项,我想要底部的第 5 个项目。我该怎么做?
    • 我很好奇这个代码Theme.of(context).platform == TargetPlatform.iOS ? new Color(0xFF0062ac) : Colors.white,这是为iOS用户显示不同的颜色还是iOS不支持标准代码Colors.white
    • 很高兴您修复了标题!代码Theme.of(context).platform == TargetPlatform.iOS ? new Color(0xFF0062ac) : Colors.white只是为了在ios系统上采用另一种颜色,但Colors.white是ios支持的。我来分析一下怎么把第五项放在最下面我传给你
    【解决方案2】:

    [

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

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    • @zayn 感谢您的回答,但这根本没有帮助。我正在尝试设置 DrawerHeader 的宽度,而您的答案没有。 :(
    【解决方案3】:

    尝试将您的 DrawerHeader 更改为下面给出的

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

    【讨论】:

    • 它不起作用,我在抽屉padding: EdgeInsets.all(0), 中添加了填充,并创建了一个具有颜色属性color: Colors.redAccent, 的容器。并将我的文本添加为​​容器的child,但输出相同。你知道吗,这是为什么呢?仅供参考,我 ColumnDrawer 的直系子代,所有内容都包含在该列中。
    • 请在您使用抽屉的地方发布您的完整课程代码,我们会检查并通知您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多