【问题标题】:StatelessWidget to StatefulWidgetStatelessWidget 到 StatefulWidget
【发布时间】:2020-05-12 18:20:32
【问题描述】:

我正在改编一个来自 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);
      },
    );
  }

【问题讨论】:

    标签: flutter statefulwidget statelesswidget


    【解决方案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);
          },
        );
      }}
    

    【讨论】:

      【解决方案2】:

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

      耶!

      【讨论】:

        猜你喜欢
        • 2021-09-19
        • 1970-01-01
        • 2020-04-06
        • 2020-10-30
        • 1970-01-01
        • 2019-08-25
        • 2021-06-24
        • 2021-12-14
        • 2018-12-30
        相关资源
        最近更新 更多