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

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

问题描述

抽屉具有默认的三个水平条作为默认图标,但我想将其更改为其他内容.我已经检查了Drawer()下的可能选项,但是似乎没有属性附加到该选项.PS:我是Flutter的初学者.

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. PS: I am a beginner in Flutter.

推荐答案

这应该有效.

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

从文档->

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

{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,
        );
      },
    ),
  );
}

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

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).

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

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