lor*_*dex 23 navigation uinavigationbar drawer dart flutter
抽屉有这个默认的三个水平条作为默认图标,但我想把它改成别的东西。我已经检查了 Drawer() 下的可能选项,但似乎没有附加任何属性。PS:我是 Flutter 的初学者。
cmd*_*ter 57
这应该有效。
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text('hi'),
leading: IconButton(
icon: Icon(Icons.accessible),
onPressed: () => Scaffold.of(context).openDrawer(),
),
),
);
Run Code Online (Sandbox Code Playgroud)
从文档->
{小部件前导} 类型:小
部件 要显示在 [title] 之前的小部件。如果这是 null 并且 [automaticallyImplyLeading] 设置为 true,则 [AppBar] 将暗示适当的小部件。例如,如果 [AppBar] 位于也有 [Drawer] 的 [Scaffold] 中,则 [Scaffold] 将使用打开抽屉的 [IconButton] 填充此小部件(使用 [Icons.menu])。如果没有 [Drawer] 并且父级 [Navigator] 可以返回,则 [AppBar] 将使用调用 [Navigator.maybePop] 的 [BackButton]。以下代码显示了如何手动指定抽屉按钮而不是依赖 [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,
);
},
),
);
}
Run Code Online (Sandbox Code Playgroud)
本示例中使用 [Builder] 来确保上下文引用子树的那部分。这样,即使在创建 [Scaffold] 的代码中也可以使用此代码片段(在这种情况下,如果没有 [Builder],上下文将无法看到 [Scaffold],因为它会引用该小部件的祖先)。
小智 19
appBar: AppBar(
leading: Builder(
builder: (context) => IconButton(
icon: Icon(Icons.menu_rounded),
onPressed: () => Scaffold.of(context).openDrawer(),
),
),
title: Text(
"Track your Shipment",
),
),
Run Code Online (Sandbox Code Playgroud)
小智 14
您也可以使用这样的自定义按钮打开抽屉。创建这个脚手架密钥。
var scaffoldKey = GlobalKey<ScaffoldState>();
Run Code Online (Sandbox Code Playgroud)
现在在你的状态类中添加了一个脚手架,如下所示
@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(),
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30676 次 |
| 最近记录: |