在Flutter中如何将值传递给小部件

时间:2020-05-21 13:34:43

标签: flutter dart

我正处于Flutter的学习阶段,我需要根据点击的方式来确定单击哪个 _buildTile 来编写功能。

我的两个 _buildTile 如下

 _buildTile(
          Padding(
            padding: const EdgeInsets.all(14.0),
            child: Column
              (
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>
                [
                  Padding(padding: EdgeInsets.only(bottom: 5.0)),
                  Text('Cash Received', style: TextStyle(color: Colors.black54,
                        fontWeight: FontWeight.w700, fontSize: 18.0)),
                  Text('25,000.00', style: TextStyle(color: Colors.green,
                      fontWeight: FontWeight.w700, fontSize: 28.0)),
                ]
            ),
          ),
        ),

_buildTile(
          Padding
            (
            padding: const EdgeInsets.all(14.0),
            child: Column
              (
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>
                [
                  Padding(padding: EdgeInsets.only(bottom: 5.0)),
                  Text('Cash Spent', style: TextStyle(color: Colors.black45,
                      fontWeight: FontWeight.w700, fontSize: 18.0)),
                  Text('9,000.00', style: TextStyle(color: Colors.red,
                      fontWeight: FontWeight.w700, fontSize: 28.0)),
                ]
            ),
          ),
        ),

在下面的函数中,如何获得 _buildTile ?我单击了如何传递值,任何人都可以帮助我

Widget _buildTile(Widget child, {Function() onTap}) {
return Material(
    elevation: 14.0,
    borderRadius: BorderRadius.circular(12.0),
    shadowColor: Color(0x802196F3),
    child: InkWell
      (
      // Do onTap() if it isn't null, otherwise do print()
        onTap: onTap != null ? () => onTap() : () { print('Which _buildTile I clicked'); },
        child: child
    )
);}

1 个答案:

答案 0 :(得分:1)

为什么不将一些标识符传递给_buildTile()

 _buildTile(
          child: Padding( //This is your "Widget child Argument"
            padding: const EdgeInsets.all(14.0),
            child: Column
              (
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>
                [
                  Padding(padding: EdgeInsets.only(bottom: 5.0)),
                  Text('Cash Received', style: TextStyle(color: Colors.black54,
                        fontWeight: FontWeight.w700, fontSize: 18.0)),
                  Text('25,000.00', style: TextStyle(color: Colors.green,
                      fontWeight: FontWeight.w700, fontSize: 28.0)),
                ]
            ),
          ),
          //Under the Widget you are passing as your first argument, you pass the others
          tileNumber: 1, //You can change the identifier here for each Tile you make
        ),

这里是buildTile();

Widget _buildTile({Widget child, {Function() onTap}, int tileNumber}) {
return Material(
    elevation: 14.0,
    borderRadius: BorderRadius.circular(12.0),
    shadowColor: Color(0x802196F3),
    child: InkWell
      (
      // Do onTap() if it isn't null, otherwise do print()
        onTap: onTap != null ? () => onTap() : () { print("clicked tile #" + tileNumber) },
        child: child
    )
);}

请注意,我在_buildTile()的()中放入了额外的{},这些使您的参数成为命名参数。这意味着您可以更轻松地查看您要传递的参数。您可以看到“子项:填充”而不只是“填充”在起作用。