音楽プレーヤー:再生位置スライダー
CoddyのFlutterジャーニー「画面サイズに合わせたレイアウト」セクションの一部。レッスン 28/30。
プレーヤーは曲のどこまで再生されたかを示し、ドラッグして別の位置に移動できます。これはSliderで、その値は状態として保持されます。
その下には2つの時間を配置します。左側には再生済みの時間、右側には曲の長さを表示します。間にSpacerを置いたRowを使うと、両端に配置されるため、どのスマートフォンでもスライダーの両端と揃います。
const Row(
children: [
Text('0:00'),
Spacer(),
Text('4:00'),
],
)チャレンジ
簡単アーティストの下にプログレスバーを追加します。
State内にフィールドdouble _position = 0.3;を追加しますSizedBox(height: 16)の後に、_positionを表示するSliderを追加します。スライダーを動かしたときに、新しい値をsetStateに保存します- その下に
Rowを追加し、左端に1:12、右端に3:45を配置します
自分で試してみよう
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: Scaffold(
appBar: AppBar(title: const Text('Now Playing')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Stack(
children: [
Container(
width: double.infinity,
height: 300,
decoration: BoxDecoration(
color: Colors.deepPurple,
borderRadius: BorderRadius.circular(24),
),
child: const Center(
child: Icon(Icons.album, size: 120, color: Colors.white),
),
),
Positioned(
top: 12,
right: 12,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: Colors.black54,
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'HD',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
),
),
],
),
const SizedBox(height: 24),
const Text(
'Midnight Drive',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
const Text('The Night Owls', style: TextStyle(color: Colors.grey)),
],
),
),
),
);
}
}
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
画面サイズに合わせたレイアウトのすべてのレッスン
自分で練習してみよう: Flutterオンラインコンパイラ