dart setState()不会更改Flutter中容器的颜色

uinbv5nw  于 2023-01-15  发布在  Flutter
关注(0)|答案(1)|浏览(156)

我在flutter屏幕中有一个问题调色板,用户点击视频列表,该视频将播放并更改容器的颜色和文本。当我点击列表时,视频正在播放,但无法显示正在播放的视频和容器的颜色。

class _VideoDetailState extends State<VideoDetail> {
  late YoutubePlayerController _controller;
  late int selectedIndex = 0;
  var links = <String>["Dx2HBxOXccs", "1AM5Fgb-qjA"];

  void playVideo(int index) {
    setState(() {
      selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Stack(
        children: [
          YoutubePlayerScaffold(
            controller: _controller,
            builder: (context, player) {
              return Scaffold(
                body: Column(
                  children: [
                    player,
                    Expanded(
                      child: ListView.builder(
                        itemCount: 2,
                        itemBuilder: (context, index) {
                          return Column(
                            children: [
                              const SizedBox(
                                height: 5.0,
                              ),
                              GestureDetector(
                                onTap: () {
                                  playVideo(index);
                                  _controller.loadVideoById(
                                    videoId: links[index],
                                  );
                                },
                                child: Container(
                                  decoration: BoxDecoration(
                                    color: index == selectedIndex
                                        ? ColorConstants.drawerbgColor
                                        : Colors.white,
                                  ),
                                  child: Padding(
                                    padding: const EdgeInsets.all(12.0),
                                    child: Row(
                                      children: [
                                        Center(
                                          child: Stack(
                                            children: [
                                              Container(
                                                alignment: Alignment.center,
                                                height: 50,
                                                child: Image.asset(
                                                  "images/video.png",
                                                  height: 50,
                                                ),
                                              ),
                                              Positioned(
                                                bottom: 15.0,
                                                left: 20.0,
                                                child: Align(
                                                  alignment:
                                                      Alignment.bottomRight,
                                                  child: SvgPicture.asset(
                                                      "images/icons/play.svg",
                                                      height: 22.0),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        const SizedBox(
                                          width: 10,
                                        ),
                                        Column(
                                          mainAxisAlignment:
                                              MainAxisAlignment.start,
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            const CustomText(
                                              text:
                                                  "Basic Introduction of shapes",
                                              fontSize: 14.0,
                                            ),
                                            const SizedBox(
                                              height: 5,
                                            ),
                                            IntrinsicHeight(
                                              child: Row(
                                                children: [
                                                  const CustomText(
                                                    text: "03:40 min",
                                                    level: "thin",
                                                    fontSize: 13.0,
                                                    color: ColorConstants
                                                        .textColor1,
                                                  ),
                                                  const SizedBox(
                                                    width: 1.0,
                                                  ),
                                                  const SizedBox(
                                                    height: 12.0,
                                                    child: VerticalDivider(
                                                      thickness: 1.0,
                                                      color: ColorConstants
                                                          .textColor1,
                                                    ),
                                                  ),
                                                  CustomText(
                                                    text: index == selectedIndex
                                                        ? "Now Playing..."
                                                        : "Viewed",
                                                    level: "thin",
                                                    fontSize: 13.0,
                                                    color:
                                                        index == selectedIndex
                                                            ? ColorConstants
                                                                .redColor
                                                            : ColorConstants
                                                                .greenColor,
                                                  )
                                                ],
                                              ),
                                            ),
                                          ],
                                        )
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    )
                  ],
                ),
              );
            },
          ),
        ],
      ),
    );
  }
  
}

有人能帮我一下吗?

wixjitnu

wixjitnu1#

使用此代码转换十六进制颜色代码:

import 'package:flutter/material.dart';

class HexColor extends Color {
  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));

  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF$hexColor";
    }
    return int.parse(hexColor, radix: 16);
  }
}

你怎么能用它?

HexColor("Dx2HBxOXccs")

相关问题