折り返し内の間隔
CoddyのFlutterジャーニー「画面サイズに合わせたレイアウト」セクションの一部。レッスン 9/30。
タグは収まるようになりましたが、互いにくっついています。Wrap には、子要素間の間隔を設定する項目が2つあります。
Wrap(
spacing: 8,
runSpacing: 12,
children: [...],
)spacing: は同じ行にある2つの子要素の間隔です。runSpacing: は、ある行と次の行の間隔です。Wrap の各行はrunと呼ばれ、これが名前の由来です。
Wrap が行に収まるものを決める際には間隔も考慮されるため、spacing を大きくすると、子要素が次の行に移動することがあります。
チャレンジ
簡単タグの間隔を空けましょう:
- 同じ行のタグ間は
8ピクセル - 行間は
12ピクセル
自分で試してみよう
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final tags = ['Dart', 'Flutter', 'Mobile', 'Design', 'Games', 'Web'];
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: const Text('Interests')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Wrap(
children: [
for (final tag in tags)
Container(
width: 110,
height: 40,
decoration: BoxDecoration(
color: Colors.purple.shade100,
borderRadius: BorderRadius.circular(20),
),
child: Center(child: Text(tag)),
),
],
),
),
),
);
}
}
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
画面サイズに合わせたレイアウトのすべてのレッスン
自分で練習してみよう: Flutterオンラインコンパイラ