Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widget will not dispose after hot restart. #100

Open
ganluo960214 opened this issue Mar 4, 2024 · 1 comment
Open

widget will not dispose after hot restart. #100

ganluo960214 opened this issue Mar 4, 2024 · 1 comment

Comments

@ganluo960214
Copy link

Code sample
import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WrapWidget(
        videoUris: [
          IAC()
        ],
      ),
    );
  }
}

class WrapWidget extends StatefulWidget {
  const WrapWidget({
    super.key,
    required this.videoUris,
  });

  final List<IAC> videoUris;

  @override
  State<WrapWidget> createState() => _WrapWidgetState();
}

class _WrapWidgetState extends State<WrapWidget> {
  @override
  Widget build(BuildContext context) {
    return Swiper(
      itemCount: 2,
      itemBuilder: (BuildContext context, int index){
        if(index==0){
          return Placeholder();
        }else{
          return widget.videoUris[0].a();
        }
      },
        // child: widget.videoUris[0].a()
    );
  }
}

abstract class AC{
  Widget a();
}
class IAC implements AC{
  @override
  Widget a(){
    return const Demo();
  }
}

class Demo extends StatefulWidget {
  const Demo({super.key});

  @override
  State<Demo> createState() => _DemoState();
}

class _DemoState extends State<Demo> {

  @override
  void initState() {
    print("_VideoWidgetNeedDisposeState-initState");
    const uri = "https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4";
    controller = VideoPlayerController.networkUrl(Uri.parse(uri));
    initializeVideoPlayerFuture = controller.initialize();
    controller.setLooping(true);

    super.initState();
  }

  late final VideoPlayerController controller;
  late final Future<void> initializeVideoPlayerFuture;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: initializeVideoPlayerFuture,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {

          if (snapshot.connectionState == ConnectionState.done) {
            controller.play();
            return Stack(
              children: [
                Center(
                  child: AspectRatio(
                    aspectRatio: controller.value.aspectRatio,
                    child: VideoPlayer(controller),
                  ),
                ),
              ],
            );
          }else{
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.play();
        },
        child: const Icon(Icons.play_circle),
      ),
    );
  }

  @override
  void dispose() {
    print("_VideoWidgetNeedDisposeState-dispose");
    super.dispose();
    controller.dispose();
  }

}
@ganluo960214
Copy link
Author

first swipe to right, then hot restart. you still can hearing video's audio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant