0

我知道框架应该是这样工作的,但我正在寻找一种解决方法。

class Toy extends StatelessWidget {
  final WidgetBuilder childBuilder;
  final String variation;
  final bool disableAppBar;
  final bool resizable;

  const Toy(
      {@required this.childBuilder,
      @required this.variation,
      this.disableAppBar = false,
      this.resizable = false});

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(variation),
      onTap: () => Navigator.of(context).push(MaterialPageRoute(
          builder: (context) => Scaffold(
                appBar: disableAppBar
                    ? null
                    : AppBar(
                        title: Text(variation),
                      ),
                body: resizable
                    ? ResizableToy(
                        builder: childBuilder,
                      )
                    : childBuilder(context),
              ))),
    );
  }
}

在这段代码中,我在 childBuilder 内容中得到了热重载。但如果我改变disableAppBar,这不会得到重建。

我试图提取整个小部件,但无济于事

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(variation),
      onTap: () => Navigator.of(context).push(MaterialPageRoute(
          builder: (context) =>
              ToyBuilder(disableAppBar, variation, resizable, childBuilder))),
    );
  }
}

class ToyBuilder extends StatelessWidget {
  final disableAppBar;
  final variation;
  final resizable;
  final childBuilder;

  ToyBuilder(
      this.disableAppBar, this.variation, this.resizable, this.childBuilder);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: disableAppBar
          ? null
          : AppBar(
              title: Text(variation),
            ),
      body: resizable
          ? ResizableToy(
              builder: childBuilder,
            )
          : childBuilder(context),
    );
  }
}

关于如何完成这项工作的任何提示?

4

0 回答 0