Repository: flutter
Issue: Flickering on size change while using VD platform view for SurfaceView android
Labels: platform-android,a: platform-views,has reproducible steps,P2,platform-views: vd,team-android,triaged-android,:hourglass_flowing_sand:,found in release: 3.22,found in release: 3.23

### Steps to reproduce

1. Create a flutter Project with the given code
2. run the flutter project
3. click the Increase Height Button 
4. Clicking this button will increase the height of the SizedBox that wraps the PlatformView, resulting in flickering as the size is updated in the UI.

 

### Expected results


https://github.com/flutter/flutter/assets/6736869/71be3191-e6b5-4fc4-ad72-f8c998a789a7

Here the image not making any flickering while change size

### Actual results

Here is slow motion video of current working, if size changed

https://github.com/flutter/flutter/assets/6736869/c52bbbbe-9f2c-47eb-97d1-8ad278e2fd99

Here is actual video of current working, if size changed


https://github.com/flutter/flutter/assets/6736869/c01f9616-1d5d-4a19-8cc6-606b49cb90f1





### Code sample

<details open><summary>Code sample</summary>

### **main.dart**

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  double heightValue = 300;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          children: [
            Container(
              height: heightValue,

              color: const Color(0xffff6600),
              child: Center(
                child: SizedBox(
                  height: 300,
                  child: Image.asset(
                    'assets/cat.jpg',
                    fit: BoxFit.fitWidth,
                  ),
                ),
              ),
            ),
            ElevatedButton(
              onPressed: ()  {
                setState(() {
                  heightValue += 50; // Increase height
                });
              },
              child: const Text('Increase Height'),
            ),
          ],
        ),
        appBar: AppBar(
          title: const Text('Platform Surface Video Player'),
        ),
      ),
    );
  }
}

```

**In Android Side,**

### **MainActivity.java**

```java

package com.example.vflick.flutter_video_flickering;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {


    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        flutterEngine.getPlugins().add(new SurfaceViewPlugin());

    }

}
```

### **SurfaceVIewFactory.java**

```java
package com.example.vflick.flutter_video_flickering;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.flutter.plugin.common.BinaryMessenger;

import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

public class SurfaceVIewFactory extends PlatformViewFactory {
 
    private final BinaryMessenger messenger;
    public SurfaceVIewFactory(BinaryMessenger messenger) {
        super(StandardMessageCodec.INSTANCE);
        this.messenger=messenger;    }

    @NonNull
    @Override
    public PlatformView create(@Nullable Context context, int viewId, @Nullable Object args) {
        return new AndroidSurfaceView(context,messenger,viewId);
    }
}


```

### **SurfaceViewPlugin.java**

```java
package com.example.vflick.flutter_video_flickering;

import androidx.annotation.NonNull;


import io.flutter.embedding.engine.plugins.FlutterPlugin;

public class SurfaceViewPlugin implements FlutterPlugin {
    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {

        binding.getPlatformViewRegistry().registerViewFactory("com.example.platformview_height_glitch/Surfaceview",new SurfaceVIewFactory(binding.getBinaryMessenger()));
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {


    }
}

```


### **AndroidSurfaceView.java**

```java
package com.example.vflick.flutter_video_flickering;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.example.vflick.flutter_video_flickering.R;

import java.io.IOException;
import io.flutter.Log;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.platform.PlatformView;

public class AndroidSurfaceView implements PlatformView, SurfaceHolder.Callback {
    private static final String TAG = "VideoSurfaceViewPluginTest";
    private final Context context;
    private LinearLayout rootView;
    private final MediaPlayer mediaPlayer;

    public AndroidSurfaceView(Context context, BinaryMessenger viewId, Object args) {
        this.context = context;
        mediaPlayer = new MediaPlayer();
        initializeMediaPlayer();
        initializeView();
    }

    private void initializeMediaPlayer() {
        try {
            mediaPlayer.setDataSource(context, Uri.parse("https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"));
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(MediaPlayer::start);
            mediaPlayer.setLooping(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initializeView() {
        rootView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.layout, null);
        SurfaceView surfaceView = rootView.findViewById(R.id.surfaceView);
        surfaceView.getHolder().addCallback(this);
    }

    @Nullable
    @Override
    public View getView() {
        return rootView;
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        mediaPlayer.setDisplay(holder);
        Log.d(TAG, "Surface created");
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        Log.d(TAG, "Surface changed to format: " + format + " width: " + width + " height: " + height);
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        Log.d(TAG, "Surface destroyed");
    }

    @Override
    public void dispose() {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }
}

```

</details>


### Screenshots or Video

_No response_

### Logs

_No response_

### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.22.0, on macOS 14.5 23F79 darwin-arm64, locale en-IN)
    • Flutter version 3.22.0 on channel stable at /Users/linslouis/Developer/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5dcb86f68f (8 weeks ago), 2024-05-09 07:39:20 -0500
    • Engine revision f6344b75dc
    • Dart version 3.4.0
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/linslouis/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/linslouis/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
    • 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
    • android-studio-dir = /Applications/Android Studio.app
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)

[✓] VS Code (version 1.90.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.90.0

[✓] Connected device (4 available)
    • ASUS I003DD (mobile)            • L8AIGF00128699H       • android-arm64  • Android 12 (API 31)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 126.0.6478.127

[✓] Network resources
    • All expected network resources are available.

• No issues found!

```

</details>