StatelessWidget 到 StatefulWidget

StatelessWidget to StatefulWidget

我正在改编来自维基百科资源管理器(开源)的 class 来浏览预先选择的页面。我正在尝试添加一个不会更新的页面计数器,因为它是一个 StatelessWidget。有人可以帮我把它变成 StatefulWidget 吗?

class NavigationControls extends StatelessWidget {
  const NavigationControls(this._webViewControllerFuture)
      : assert(_webViewControllerFuture != null);

  final Future<WebViewController> _webViewControllerFuture;

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<WebViewController>(
      future: _webViewControllerFuture,
      builder:
          (BuildContext context, AsyncSnapshot<WebViewController> snapshot) {
        final bool webViewReady =
            snapshot.connectionState == ConnectionState.done;
        final WebViewController controller = snapshot.data;
        return _buttonsPagination(webViewReady, controller, context);
      },
    );
  }

您可以通过按 StatelessWidget 上方键盘上的快捷键自动转换它,它 应该 为您提供转换为 StatefulWidget 的选项。

在 Mac 上尝试:CMD + .

在 Window 上尝试:CTRL + .

总之,给你了:

class NavigationControls extends StatefulWidget {
  const NavigationControls(this._webViewControllerFuture)
      : assert(_webViewControllerFuture != null);

  final Future<WebViewController> _webViewControllerFuture;

  @override
  _NavigationControlsState createState() => _NavigationControlsState();


class _NavigationControlsState extends State<NavigationControls> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<WebViewController>(
      future: widget._webViewControllerFuture,
      builder:
          (BuildContext context, AsyncSnapshot<WebViewController> snapshot) {
        final bool webViewReady =
            snapshot.connectionState == ConnectionState.done;
        final WebViewController controller = snapshot.data;
        return _buttonsPagination(webViewReady, controller, context);
      },
    );
  }}

您只需将光标放在 StatelessWidget 上,按 Alt + Enter 并单击转换为 StatefulWidget。所有样板代码都将自动为您创建。

耶!