【问题标题】:what is the Widget get _appBar meaning in flutterFlutter中的Widget get _appBar是什么意思
【发布时间】:2020-11-02 16:21:41
【问题描述】:

我正在阅读这样的颤振代码:

  Widget get _appBar {
    return Column(
      children: <Widget>[
        Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [Color(0x66000000), Colors.transparent],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
          ),
          child: Container(
            padding: EdgeInsets.fromLTRB(14, 20, 0, 0),
            height: 86.0,
            decoration: BoxDecoration(
                color:
                    Color.fromARGB((appBarAlpha * 255).toInt(), 255, 255, 255),
                boxShadow: [
                  BoxShadow(
                    color: appBarAlpha == 1.0
                        ? Colors.black12
                        : Colors.transparent,
                    offset: Offset(2, 3),
                    blurRadius: 6,
                    spreadRadius: 0.6,
                  ),
                ]),
            child: SearchBar(
              searchBarType: appBarAlpha > 0.2
                  ? SearchBarType.homeLight
                  : SearchBarType.home,
              inputBoxClick: _jumpToSearch,
              defaultText: SEARCH_BAR_DEFAULT_TEXT,
              leftButtonClick: () {},
              speakClick: _jumpToSpeak,
              rightButtonClick: _jumpToUser,
            ),
          ),
        ),
        Container(
            height: appBarAlpha > 0.2 ? 0.5 : 0,
            decoration: BoxDecoration(
                boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 0.5)]))
      ],
    );
  }

Widget get _appBar是什么意思?它得到一个appBar控件吗?为什么不直接返回一个 AppBar?

【问题讨论】:

  • 你运行完整代码了吗?

标签: flutter


【解决方案1】:

它是一个“吸气剂”:

Getter 和 setter 是提供对对象属性的读写访问的特殊方法。回想一下,每个实例变量都有一个隐式的 getter,如果合适的话,还有一个 setter。您可以通过使用 get 和 set 关键字实现 getter 和 setter 来创建其他属性

(Dart Documentation)

所以Widget get _appBar_appBar 类型为Widget 的属性的getter。你可以把它变成一个函数:Widget _appBar()

我猜你的代码在多个地方使用了这个应用栏,因此找到了一种方法将代码放在一个地方,以便从所有其他地方调用它,而不是复制所有代码。

【讨论】:

    【解决方案2】:

    get 关键字在 dart 代码中用于提供对对象属性的读取访问。通常这是通过方法完成的(例如您的情况)。 Check this out for more on getters and setters。在Widget get _appBar 中,我们看到Widget 返回类型、get 关键字(表示它是一个getter)和字段/getter 的名称-_appBar

    在此示例中,字段 getter _appBar 被定义为用于返回小部件的接口。 Getter 可以在某种程度上将方法伪装成字段。所以您访问此字段的方式如下所示:

    var someObject = SomeObject();
    
    // here we're accessing the field and assigning it to a different object
    var widget = someObject._appBar;
    

    现在这里有一些有趣的事情发生在 getter 的名字上。它以下划线 (_) 开头,表示它是私有成员。这可能是故意为库/包/类在内部使用而设计的,而不是在其他位置访问它。

    代码未直接返回AppBar 可能是因为这是应用栏的自定义实现。您无需为ScaffoldappBar 参数提供AppBar 小部件,您只需为参数提供PreferredSizeWidget。查看上面的文档 here (Scaffold)here (appBar property)here(PreferredSizeWidget)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-26
      • 2015-11-03
      • 2013-03-07
      • 1970-01-01
      • 2021-04-24
      • 2021-03-28
      • 2020-02-28
      • 1970-01-01
      相关资源
      最近更新 更多