0

AppBar颤动采取List<Widget>行动。出于某种原因,我找不到基于StreamBuilder.

我有一个 BehaviorSubject(可以更改为其他类型的流),我要在其中添加小部件,并且我想在 AppBar 中显示这些小部件(作为操作按钮)

// ....

var appBar = new AppBar(
        title: "appTitle",
        actions: _getActionWidgets()   // <= Can be fed by SteamBuilder?
      )
/// ...


// ...
// BehaviorSubject<Widget> actionWidgets;
// ...

List<Widget> _getActionWidgets(){
  return StreamBuilder(
    ... // what can be done here so that this method would return List<Widget>
  );

}

那里的大多数示例都是针对“ ListView”使用的ListView.builder(...),这不适用于我的情况。

最终目标是提供使用 StreamBuilder 的actions属性AppBar并努力寻找方法。感谢您提供任何帮助或指示,并感谢您花时间阅读我的问题。

4

2 回答 2

1

只需填充actions单个元素List,其中包含StreamBuilder

actions: [
    StreamBuilder(
        builder: (context, snapshot) {
            return Row(children: [
                // Your widgets here
            ]);
        }
    ),
]
于 2020-06-29T14:01:40.703 回答
0

不,这不是个案。在您的情况下,操作通常是按钮(FlatButton、IconButton 等)。您的案例可能如下所示:

class Bloc {
  final _subject = BehaviourSubject<List<Widget>>();
  ...  
}
void initState() {
  super.initState();
  // Prepare actions depending on condition
  final actions = [
    FlatButton(child: Text('Action 1'), onPressed: () {}),
    FlatButton(child: Text('Action 2'), onPressed: () {}),
  ];
  bloc.setActions(actions); // indeed send actions to sink
}

Widget build(BuildContext context) {
  return StreamBuilder<List<Widget>>(
    stream: bloc.actionStream, // Function of `bloc` which returns Stream<List<Widget>> (indeed IconButton, FlatButton)
    builder: (context,snapshot) {
      return Scaffold(
        appBar: AppBar(
          actions: snapshot.hasData? snapshot.data : <Widget>[],
        ),
      );
    }
  );
}
于 2020-06-29T14:14:04.690 回答