在点击按钮之前执行的颤动代码

问题描述 投票:2回答:1

我有一个ListTile(在抽屉里),点击它后,进入一个新的屏幕,在那个屏幕上,用户可以从一些(使用for循环)IconButtons(在ListView中)中选择,但是这个功能是当用户被发送到选择屏幕时(当他/她点击ListTile时)实际执行按钮时执行将被执行,因此该功能(顺便打开另一个屏幕)被执行相同的量在我的ListView中和单击它们之前有IconButtons的时间。

有关更多详细信息,请参阅源代码:

单击ListTile时:

              setState(() {
                goDownloads(files);
              });

打开新屏幕的功能:

goDownloads(List<File> files) async {
  for (int n = 0; n < files.length; n++) {
    String url = files[n].path;
    Widget container = new Container(
        child: new Row(
            children: <Widget>[
              new IconButton(
                onPressed: openBook(url),//This is the code that gets executed before it's supposed to
                icon: new Icon(Icons.open_in_browser),
            ),
...
          ]
      )
  );
  dlbooks.add(container);
}
setState(() {
  Navigator.of(context).push(new MaterialPageRoute<Null>(
    builder: (BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(title: new Text('My Page')),
        body: new Center(
          child: new ListView(
              children: dlbooks
          ),
        ),
      );
    },
  ));
});
}

我也有一个main.dart文件中的完整代码,虽然我不推荐它,因为它写得不好,我还是一个菜鸟。 https://pastebin.com/vZapvCKx先谢谢!

android button onclick dart flutter
1个回答
2
投票

此代码将函数传递给按下按钮时执行的onPressed

onPressed: () => openBook(url)

你的代码

onPressed: openBook(url)

执行openBook(url)并将结果传递给onPressed,以便在按下按钮时执行,这似乎不是你想要的。

© www.soinside.com 2019 - 2024. All rights reserved.