gpt4 book ai didi

flutter - Flutter中具有默认值的CustomDrawer

转载 作者:行者123 更新时间:2023-12-03 05:00:02 25 4
gpt4 key购买 nike

我对 Flutter 很陌生。我正在尝试构建一个可以根据需要导入和自定义的 CustomDrawer 类。

    import 'package:flutter/material.dart';

class CustomDrawer extends AppBar {
CustomDrawer ({ Key key, Widget leading, Widget title})
: super(key: key, leading: leading, title: title, actions: <Widget>[
new IconButton(
icon: new Icon(Icons.notifications_none),
onPressed: () => print("Hello World"),
),
]);
}

我希望前导 Widget 在未提供参数时显示默认文本“占位符”,否则前导应显示传递给 CustomDrawer 的任何内容。
有人可以帮我吗?

最佳答案

一个简单的三元运算符可以解决这个问题。

构建方法:

@override
Widget build(BuildContext context) {
return Scaffold(
/// You can try either of these.
appBar: CustomDrawer(),
appBar: CustomDrawer(leading: Icon(Icons.ac_unit),),
);
}

自定义抽屉:
class CustomDrawer extends AppBar {
CustomDrawer({
Key key,
Widget leading,
Widget title,
}) : super(
key: key,
/// Adding ternary operator.
leading: leading == null ? Placeholder() : leading,
title: title,
actions: <Widget>[
IconButton(
icon: Icon(Icons.notifications_none),
onPressed: () => print("Hello World"),
),
],
);
}

关于flutter - Flutter中具有默认值的CustomDrawer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59697074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com