0

所以我试图做一个功能,当我们点击一​​个图标按钮时,它会改变视频路径。它将使用字符串更改视频路径。我正在使用 print("$videoname") 来确保字符串发生变化,并且确实如此。但它仍然不起作用,使用 videoplayercontroller.assets(): 在此处输入图像描述 这是我试图制作的代码

    String videoname="Video/Intro.mp4";
              Container(
              child:Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                ChewieListItem(
    
                  videoPlayerController: VideoPlayerController.asset(videoname),
                  looping: false,
    
                       ),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 20.0),
                  height: 100.0,
                  child: ListView(
                    scrollDirection: Axis.horizontal,
                    children: [
                      IconButton(
    
                        icon:Icon(Icons.people),
                      onPressed: (){
                              videoname="Video/Intro.mp4";
                              print("$videoname");
                      },
    
    
                      ),
    
                      IconButton(
    
                        icon:Icon(Icons.personal_video),
                        onPressed: (){
                          videoname="Video/Intro1.mp4";
                          print("$videoname");
                        },
    
    
                      ),
    
    
                    ],
                  ),
                ),
    )
4

2 回答 2

1

您可以在下面复制粘贴运行完整代码
第 1 步:key: UniqueKey()ChewieListItem
第 2 步中使用:setStateonPressed
代码片段中使用

ChewieListItem(
          key: UniqueKey(),
          videoPlayerController: VideoPlayerController.asset(videoname),
          looping: false,
        ),
        
 IconButton(
    icon: Icon(Icons.people),
    onPressed: () {
      setState(() {
        videoname = "Video/Intro.mp4";
        print("$videoname");
      });
    },
  ),
 IconButton(
    icon: Icon(Icons.personal_video),
    onPressed: () {
      setState(() {
        videoname = "Video/Intro1.mp4";
        print("$videoname");
      });
    },
  ),        

工作演示

在此处输入图像描述

完整代码

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';

class ChewieListItem extends StatefulWidget {
  // This will contain the URL/asset path which we want to play
  final VideoPlayerController videoPlayerController;
  final bool looping;

  ChewieListItem({
    @required this.videoPlayerController,
    this.looping,
    Key key,
  }) : super(key: key);

  @override
  _ChewieListItemState createState() => _ChewieListItemState();
}

class _ChewieListItemState extends State<ChewieListItem> {
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    print("ChewieListItem initState");
    // Wrapper on top of the videoPlayerController
    _chewieController = ChewieController(
      videoPlayerController: widget.videoPlayerController,
      aspectRatio: 16 / 9,
      // Prepare the video to be played and display the first frame
      autoInitialize: true,
      looping: widget.looping,
      // Errors can occur for example when trying to play a video
      // from a non-existent URL
      errorBuilder: (context, errorMessage) {
        return Center(
          child: Text(
            errorMessage,
            style: TextStyle(color: Colors.white),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Chewie(
        controller: _chewieController,
      ),
    );
  }

  @override
  void dispose() {
    print("ChewieListItem dispose");
    super.dispose();
    // IMPORTANT to dispose of all the used resources
    widget.videoPlayerController.dispose();
    _chewieController.dispose();
  }
}

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String videoname = "Video/Intro.mp4";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
          child:
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        ChewieListItem(
          key: UniqueKey(),
          videoPlayerController: VideoPlayerController.asset(videoname),
          looping: false,
        ),
        Container(
          margin: EdgeInsets.symmetric(vertical: 20.0),
          height: 100.0,
          child: ListView(
            scrollDirection: Axis.horizontal,
            children: [
              IconButton(
                icon: Icon(Icons.people),
                onPressed: () {
                  setState(() {
                    videoname = "Video/Intro.mp4";
                    print("$videoname");
                  });
                },
              ),
              IconButton(
                icon: Icon(Icons.personal_video),
                onPressed: () {
                  setState(() {
                    videoname = "Video/Intro1.mp4";
                    print("$videoname");
                  });
                },
              ),
            ],
          ),
        ),
      ])),
    );
  }
}
于 2020-08-31T06:31:36.363 回答
0

setState应该为你工作

onPressed: () {
              setState(() {
                videoname="Video/Intro1.mp4";
                print("$videoname");
              });
            },
于 2020-08-31T04:21:23.420 回答