简体   繁体   English

如何在颤动中更改抽屉图标?

[英]How can I change Drawer icon in 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.我已经检查了Drawer()下的可能选项,但似乎没有附加任何属性。

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 {Widget 领先} 类型:Widget
A widget to display before the [title].在 [title] 之前显示的小部件。 If this is null and [automaticallyImplyLeading] is set to true, the [AppBar] will imply an appropriate widget.如果这是 null 并且 [automaticallyImplyLeading] 设置为 true,则 [AppBar] 将暗示适当的小部件。 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]).例如,如果 [AppBar] 位于也有 [Drawer] 的 [Scaffold] 中,则 [Scaffold] 将使用打开抽屉的 [IconButton] 填充此小部件(使用 [Icons.menu])。 If there's no [Drawer] and the parent [Navigator] can go back, the [AppBar] will use a [BackButton] that calls [Navigator.maybePop].如果没有 [Drawer] 并且父级 [Navigator] 可以返回,则 [AppBar] 将使用调用 [Navigator.maybePop] 的 [BackButton]。 The following code shows how the drawer button could be manually specified instead of relying on [automaticallyImplyLeading]:以下代码显示了如何手动指定抽屉按钮而不是依赖 [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.本示例中使用 [Builder] 来确保上下文引用子树的那部分。 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).这样,即使在创建 [Scaffold] 的代码中也可以使用此代码片段(在这种情况下,如果没有 [Builder],上下文将无法看到 [Scaffold],因为它会引用该小部件的祖先)。

appBar: AppBar(
        leading: Builder(
          builder: (context) => IconButton(
            icon: Icon(Icons.menu_rounded),
            onPressed: () => Scaffold.of(context).openDrawer(),
          ),
        ),
        title: Text(
          "Track your Shipment",
        ),
      ),

You can open a drawer with a custom button like this too.您也可以使用这样的自定义按钮打开抽屉。 create this scaffold key.创建此脚手架键。

var scaffoldKey = GlobalKey<ScaffoldState>();

now added a scaffolled in your state class like this现在在你的状态类中添加了一个像这样的脚手架

      @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      drawer: Drawer(
        child: Text('create drawer widget tree here'),
      ),
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          'appName',
          style: Theme.of(context).textTheme.headline2,
        ),
        leading: IconButton(
          onPressed: () {
            scaffoldKey.currentState?.openDrawer();
          },
          icon: Image.asset(
            'assets/images/menu.png',
            fit: BoxFit.cover,
          ),
        ),
      ),
      body: Container(),
    );
  }

Happy coding Guys :)快乐编码伙计们:) 在此处输入图片说明

 appBar: AppBar(

    leading: Icon(Icons.favorite),

    title: Text("Drawer"),),

Lets say you have: index.dart (where you want to use the appbar), drawer.dart (your drawer or navigation menu) and appbar.dart (your appbar)假设您有:index.dart(您要使用应用栏的位置)、drawer.dart(您的抽屉或导航菜单)和 appbar.dart(您的应用栏)

you can do this in drawer:你可以在抽屉里做这个:

Widget drawer(BuildContext context) {
    return Drawer(
child: ListView(
  padding: EdgeInsets.zero,
  children: <Widget>[
    Container(
        ...
    )
);

then your appbar.dart:然后你的 appbar.dart:

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: Colors.white,
      leading: InkWell(
        onTap: () => Scaffold.of(context).openDrawer(),
        child: Image.asset("assets/images/imgAppBar.png"),
      ),
     title: Container(...

then your index.dart:那么你的 index.dart:

@override
Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      drawer: drawer(context),
      appBar: CustomAppBar(),
    ...

this is just a simple one.这只是一个简单的。 You can use IconButton in case you want to use an Icon etc.如果您想使用图标等,您可以使用 IconButton。

Actually, i tried the answer by cmd_prompter and it didn't work for me.实际上,我尝试了 cmd_prompter 的答案,但它对我不起作用。

The better approach is describedhere 这里描述更好的方法

My working code is here:我的工作代码在这里:

return DefaultTabController(
      key: Key("homePage"),
      length: 2,
      child: Scaffold(
          endDrawer: Drawer(

        ),
          appBar: AppBar(
            leading: BackButton(
                onPressed: () {
                  },
              ),
            title: Text(profile.selectedCity!),
            actions: [
              Padding(
                padding: EdgeInsets.symmetric(horizontal: baseUnit(3)),
                child: Builder(
                  builder: (context) => IconButton(
                      icon: Icon(Icons.account_circle),
                      onPressed: () => Scaffold.of(context).openEndDrawer(),
                    )
                )
              )

It worked fine for me - especially this part regarding using Builder.它对我来说很好 - 尤其是关于使用 Builder 的这部分。 This is important - otherwise it was not working for me.这很重要 - 否则它对我不起作用。

In case where someone needs to change the icon color only:如果有人只需要更改图标颜色:

It's easier to do by adding an iconTheme to the AppBar通过向 AppBar 添加 iconTheme更容易做到

you need to create the Global key of type ScaffoldKey the use that to open the drawer and change the icon too您需要创建 ScaffoldKey 类型的全局键,用于打开抽屉并更改图标

    Widget build(BuildContext context) {
    var scafoldKey = GlobalKey<ScaffoldState>();
    return Scaffold(
      appBar: AppBar(
        title:Text('hi'),
        leading: IconButton(
          icon: Icon(Icons.accessible),
          onPressed: () => scafoldKey.currentState.openDrawer(),
        ),
      ),
    );
class HomeOne extends StatefulWidget { const HomeOne({Key? key}) : super(key: key);

@override State createState() =>HomeOneState(); }

var scaffoldKey = GlobalKey();

class HomeOneState extends State { @override Widget build(BuildContext context) { var theme = Theme.of(context); return Directionality( textDirection: TextDirection.rtl, child: Scaffold( key: scaffoldKey, drawerEnableOpenDragGesture: true, // drawerScrimColor: Colors.red, appBar: AppBar( leading: IconButton( onPressed: () => scaffoldKey.currentState?.openDrawer(), icon: const Icon( Icons.add, color: Colors.red, )), ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过主题更改 Flutter 中抽屉的标准汉堡菜单图标的大小 - How can I change the Size of the standard hamburger menu icon of a drawer in Flutter via theme Flutter 更改 Scaffold 抽屉图标 - Flutter change Scaffold drawer icon 我如何更改用户 state flutter 的抽屉项目 - how can i change drawer items on user state flutter 打开/关闭抽屉布局时如何将导航抽屉汉堡菜单图标更改为箭头图标 - Flutter? - How to change navigation drawer hamburger menu icon to arrow icon when open/close drawer layout - Flutter? 将抽屉项目图标更改为右侧 flutter - change drawer item icon to right flutter Flutter 导航抽屉汉堡包图标变色 - Flutter navigation drawer hamburger icon color change 我怎么能在 flutter 上做这样的抽屉 - How i can do a drawer like this on flutter 如何在不修改 Flutter 中默认提供的行为的情况下更改 appbar 的抽屉/后退按钮图标的大小? - How can I change the size of drawer/back button icons of appbar without modifying the behavior provided by default in Flutter? Flutter:如何从抽屉中更改所选菜单项的颜色 - Flutter:How can i change the color of selected menu item from drawer 如何删除 appBar 中的抽屉图标和颤动中的搜索图标之间的填充 - how you can remove the padding between the icon for the drawer in the appBar and the Search Icon in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM