Skip to content

"Convert to StatefulWidget" moves members related to implemented types #57087

@DanTup

Description

@DanTup

Raised at Dart-Code/Dart-Code#5337 by @alestiago.

When using "Convert to Stateful Widget", fields and methods are moved even if they need to remain on the widget (for example because of implements PreferredSizeWidget.

Presumably, members should not be moved if they are part of a base type/interface?


Describe the bug

Using the "Convert to StatefulWidget" action in a widget that implements PreferredSizeWidget results in erroneous code.

To Reproduce

  1. Create a StatelessWidget that implements PreferredSizeWidget:
import 'package:flutter/material.dart';

class MyWidgets extends StatelessWidget implements PreferredSizeWidget {
  const MyWidgets({super.key});

  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }

  @override
  Size get preferredSize => const Size.square(100);
}
  1. Use the "Convert to StatefulWidget" action, the result is:
class MyWidgets extends StatefulWidget implements PreferredSizeWidget {
  const MyWidgets({super.key});

  @override
  State<MyWidgets> createState() => _MyWidgetsState();
}

class _MyWidgetsState extends State<MyWidgets> {
  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }

  @override
  Size get preferredSize => const Size.square(100);
}

The above is invalid code and will have a compile time error:

Missing concrete implementation of 'getter PreferredSizeWidget.preferredSize'.
Try implementing the missing method, or make the class abstract.dartnon_abstract_class_inherits_abstract_member

Expected behavior

The "Convert to StatefulWidget" action should resolve this case as:

import 'package:flutter/material.dart';

class MyWidgets extends StatefulWidget implements PreferredSizeWidget {
  const MyWidgets({super.key});

  @override
  State<MyWidgets> createState() => _MyWidgetsState();

  @override
  Size get preferredSize => const Size.square(100);
}

class _MyWidgetsState extends State<MyWidgets> {
  @override
  Widget build(BuildContext context) {
    return const Placeholder();
  }
}

Please complete the following information:

Workspace Environment
Dart Code extension: 3.100.0
Flutter extension: 3.100.0 (activated)

App: Visual Studio Code
App Host: desktop
Version: mac 1.94.2

Workspace type: Flutter (LSP)
Workspace name: my_app

Dart (3.5.3): /Users/alestiago/Developer/flutter/bin/cache/dart-sdk
Flutter (3.24.3): /Users/alestiago/Developer/flutter (macOS (darwin/macos))

Output from 'flutter doctor'

/Users/alestiago/Developer/flutter/bin/flutter doctor -v

[✓] Flutter (Channel stable, 3.24.3, on macOS 14.7 23H124 darwin-arm64, locale en-US)
    • Flutter version 3.24.3 on channel stable at /Users/alestiago/Developer/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (9 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/alestiago/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)

[✓] VS Code (version 1.94.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.100.0

[✓] Connected device (3 available)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.7 23H124 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.7 23H124 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 130.0.6723.117

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-assistIssues with analysis server assistsdevexp-serverIssues related to some aspect of the analysis servertype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions