Flutter 小部件get\u appBar在flatter中的含义是什么

Flutter 小部件get\u appBar在flatter中的含义是什么,flutter,Flutter,我正在读这样一个颤振代码: Widget get _appBar { return Column( children: <Widget>[ Container( decoration: BoxDecoration( gradient: LinearGradient( colors: [Color(0x66000000), Colors.transparent],

我正在读这样一个颤振代码:

  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\u appBar{
返回列(
儿童:[
容器(
装饰:盒子装饰(
梯度:线性梯度(
颜色:[颜色(0x66000000),颜色.透明],
开始:Alignment.topCenter,
结束:对齐。底部中心,
),
),
子:容器(
填充:从LTRB(14,20,0,0)开始的边缘设置,
身高:86.0,
装饰:盒子装饰(
颜色:
Color.fromARGB((appBarAlpha*255).toInt(),255,255,255),
boxShadow:[
箱形阴影(
颜色:appBarAlpha==1.0
?颜色。黑色12
:颜色。透明,
偏移量:偏移量(2,3),
半径:6,
扩展半径:0.6,
),
]),
孩子:搜索栏(
searchBarType:appBarAlpha>0.2
?SearchBarType.homeLight
:SearchBarType.home,
inputBoxClick:\u jumpToSearch,
默认文本:搜索栏默认文本,
LeftButton单击:(){},
speakClick:_jumpToSpeak,
右键单击:\u jumpToUser,
),
),
),
容器(
高度:appBarAlpha>0.2?0.5:0,
装饰:盒子装饰(
boxShadow:[boxShadow(颜色:Colors.black12,模糊半径:0.5)])
],
);
}
Widget get\u appBar
的含义是什么?它有appBar控件吗?为什么不直接返回AppBar?

它是一个“getter”:

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

()

因此,
Widget get\u appBar
是一个名为
\u appBar
的属性的getter,该属性属于
Widget
类型。你可以把它变成一个函数:
Widget\u appBar()


我猜您的代码在多个位置使用此应用程序栏,因此找到了一种方法将代码放在一个位置,从所有其他位置调用它,而不是复制所有位置。

dart代码中使用
get
关键字提供对对象属性的读取访问。通常这是通过方法完成的(如您的案例)。在
Widget get\u appBar
中,我们可以看到
Widget
返回类型、
get
关键字(表示它是一个getter)和字段名/getter-
\u appBar

在本例中,字段getter
\u appBar
被定义为返回小部件的接口。getter可以将方法伪装成字段。因此,访问此字段的方式如下所示:

  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)]))
      ],
    );
  }
var someObject=someObject();
//这里我们访问字段并将其分配给不同的对象
var widget=someObject.\u appBar;
现在有一些有趣的事情发生在这里,关于getter的名字。它以下划线(
\uuu
)开头,表示它是私有成员。这可能只是为了在内部使用库/包/类,而不是在其他位置访问它


代码可能没有直接返回
AppBar
,因为这是AppBar的自定义实现。您不需要为
Scaffold
AppBar
参数提供
AppBar
小部件,只需要为参数提供
PreferredSizeWidget
。查看上面的文档。

您是否运行了完整的代码?