1

我正在改编 Wikipedia Explorer(开源)中的一个类来浏览预先选择的页面。我正在尝试添加一个不会更新的页面计数器,因为它是 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);
      },
    );
  }
4

2 回答 2

1

您可以通过按上面键盘上的快捷键来自动转换它,StatelessWidget应该为您提供转换为StatefulWidget.

在 Mac 上尝试:CMD+.

在窗口尝试: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);
      },
    );
  }}
于 2020-01-27T11:23:49.557 回答
1

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

耶!

在此处输入图像描述

于 2020-01-27T12:39:34.867 回答