gpt4 book ai didi

flutter - 2个容器之间的文字 flutter

转载 作者:IT王子 更新时间:2023-10-29 06:53:58 24 4
gpt4 key购买 nike

我想在 flutter 中显示 2 个容器之间的文本。问题是容器会适应文本的大小。我不想要这种行为。想要这样的东西。 (我很陌生)。

我想做一个音乐播放器。文本不能拆分。

enter image description here

最佳答案

编辑:根据您的要求,您想要创建一个自定义播放器,根据歌曲的当前位置更新其颜色。

为此,您可以使用 CustomPainter 播放器创建一个 CustomPaint 小部件,该播放器会在歌曲状态发生变化时进行更新。

class MyPlayerBar extends CustomPainter {
MyPlayerBar({this.fullSongTimeInSeconds, this.currentSecond});

final int fullSongTimeInSeconds;
final int currentSecond;

@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint();
double cursor = (currentSecond * size.width) / fullSongTimeInSeconds;
Radius cornerRadius = Radius.circular(3.0);

// Already played half color (your darker orange)
paint.color = Color.fromRGBO(206, 69, 0, 1.0);

// Painting already played half
canvas.drawRRect(
RRect.fromRectAndCorners(Rect.fromLTWH(0.0, 0.0, cursor, size.height),
topLeft: cornerRadius, bottomLeft: cornerRadius),
paint);

// Yet to play half color (your lighter orange)
paint.color = Color.fromRGBO(227, 113, 18, 1.0);

// Painting the remaining space
canvas.drawRRect(
RRect.fromRectAndCorners(Rect.fromLTWH(cursor, 0.0, size.width - cursor, size.height),
bottomRight: cornerRadius, topRight: cornerRadius),
paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}

我创建了一个完整的示例来模拟一首 3 分钟的歌曲(180 秒),结果如下:

example

完整示例代码:

class MyPlayer extends StatefulWidget {
_MyPlayerState createState() => _MyPlayerState();
}

class _MyPlayerState extends State<MyPlayer> {
int _songCurrentPosition = 0;
int _fullSongInSeconds = 180; // 3 minutes song

@override
void initState() {
super.initState();
_songPlaying();
}

void _songPlaying() async {
if (_songCurrentPosition >= _fullSongInSeconds) return;
await Future.delayed(Duration(seconds: 1));
setState(() => _songCurrentPosition += 1);
_songPlaying();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My player'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: CustomPaint(
painter: MyPlayerBar(
currentSecond: _songCurrentPosition, // Your current song position in seconds
fullSongTimeInSeconds: _fullSongInSeconds,
),
child: Container(
alignment: Alignment.center,
height: 30.0,
width: double.infinity,
child: Text(
'Playing: 01 - Hey, this is my life',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
),
),
),
),
);
}
}

class MyPlayerBar extends CustomPainter {
MyPlayerBar({this.fullSongTimeInSeconds, this.currentSecond});

final int fullSongTimeInSeconds;
final int currentSecond;

@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint();
double cursor = (currentSecond * size.width) / fullSongTimeInSeconds;
Radius cornerRadius = Radius.circular(3.0);

// Already played half color (your darker orange)
paint.color = Color.fromRGBO(206, 69, 0, 1.0);

// Painting already played half
canvas.drawRRect(
RRect.fromRectAndCorners(Rect.fromLTWH(0.0, 0.0, cursor, size.height),
topLeft: cornerRadius, bottomLeft: cornerRadius),
paint);

// Yet to play half color (your lighter orange)
paint.color = Color.fromRGBO(227, 113, 18, 1.0);

// Painting the remaining space
canvas.drawRRect(
RRect.fromRectAndCorners(Rect.fromLTWH(cursor, 0.0, size.width - cursor, size.height),
bottomRight: cornerRadius, topRight: cornerRadius),
paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}

关于flutter - 2个容器之间的文字 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54561699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com