如何在颤动中更改抽屉图标? [英] How can I change Drawer icon in flutter?

查看:20
本文介绍了如何在颤动中更改抽屉图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抽屉有这个默认的三个水平条作为默认图标,但我想把它改成别的东西.
我已经检查了 Drawer() 下的可能选项,但似乎没有附加任何属性.

解决方案

这应该可行.

小部件构建(BuildContext 上下文){返回脚手架(应用栏:应用栏(标题:文本('嗨'),领先:图标按钮(图标:图标(Icons.accessible),onPressed: () =>Scaffold.of(context).openDrawer(),),),);

来自文档 ->

<块引用>

{Widget 前导} 类型:Widget
在 [title] 之前显示的小部件.如果这是 null 并且 [automaticallyImplyLeading] 设置为 true,则 [AppBar] 将暗示一个适当的小部件.例如,如果 [AppBar] 位于还具有 [Drawer] 的 [Scaffold] 中,则 [Scaffold] 将使用打开抽屉的 [IconButton] 填充此小部件(使用 [Icons.menu]).如果没有 [Drawer] 并且父 [Navigator] 可以返回,则 [AppBar] 将使用调用 [Navigator.maybePop] 的 [BackButton].以下代码展示了如何手动指定抽屉按钮而不是依赖 [automaticallyImplyLeading]:

import 'package:flutter/material.dart';小部件构建(上下文){返回应用栏(领先:建设者(构建器:(BuildContext 上下文){返回图标按钮(图标:常量图标(图标.menu),按下:(){Scaffold.of(context).openDrawer();},工具提示:MaterialLocalizations.of(context).openAppDrawerTooltip,);},),);}

<块引用>

此示例中使用 [Builder] 以确保上下文引用子树的该部分.这样,即使在创建 [Scaffold] 的代码中也可以使用此代码片段(在这种情况下,如果没有 [Builder],上下文将无法看到 [Scaffold],因为它会引用该小部件的祖先).

The drawer has this default three horizontal bars as default icon but I want to change it to something else.
I have checked the possible options under the Drawer(), but no property seems to be attached to that.

解决方案

This should work.

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:Text('hi'),
        leading: IconButton(
          icon: Icon(Icons.accessible),
          onPressed: () => Scaffold.of(context).openDrawer(),
        ),
      ),
);

From the docs ->

{Widget leading} Type: Widget
A widget to display before the [title]. If this is null and [automaticallyImplyLeading] is set to true, the [AppBar] will imply an appropriate widget. For example, if the [AppBar] is in a [Scaffold] that also has a [Drawer], the [Scaffold] will fill this widget with an [IconButton] that opens the drawer (using [Icons.menu]). If there's no [Drawer] and the parent [Navigator] can go back, the [AppBar] will use a [BackButton] that calls [Navigator.maybePop]. The following code shows how the drawer button could be manually specified instead of relying on [automaticallyImplyLeading]:

import 'package:flutter/material.dart';
Widget build(context) {
  return AppBar(
    leading: Builder(
      builder: (BuildContext context) {
        return IconButton(
          icon: const Icon(Icons.menu),
          onPressed: () {
            Scaffold.of(context).openDrawer();
          },
          tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
        );
      },
    ),
  );
}

The [Builder] is used in this example to ensure that the context refers to that part of the subtree. That way this code snippet can be used even inside the very code that is creating the [Scaffold] (in which case, without the [Builder], the context wouldn't be able to see the [Scaffold], since it would refer to an ancestor of that widget).

这篇关于如何在颤动中更改抽屉图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆