Menu
CoddyTech

ボックス内で位置を調整する

CoddyのFlutterジャーニー「画面サイズに合わせたレイアウト」セクションの一部。レッスン 12/30。

Centerは、その子要素を自分が持つ空間の中央に配置します。Alignは、好きな位置に配置できます。

Container(
  height: 200,
  color: Colors.teal,
  child: const Align(
    alignment: Alignment.topRight,
    child: Text('Top right'),
  ),
)

位置の名前は、角や辺にちなんで付けられています。Alignment.topLeft、topCenter、topRight、centerLeft、center、centerRight、bottomLeft、bottomCenter、bottomRightです。Centerは、Alignment.centerを指定したAlignです。

Stackにもalignment:を指定できます。これにより、Positionedではないすべての子要素が配置されます。Stack(alignment: Alignment.bottomCenter, children: [...])。

challenge icon

チャレンジ

簡単

表紙の中央にあるMountainsのキャプションを、左下隅に移動します。パディングによって、両端から16ピクセルの間隔が保たれます。

自分で試してみよう

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: const Text('Travel')),
        body: Column(
          children: [
            Container(
              height: 240,
              color: Colors.indigo,
              child: const Center(
                child: Padding(
                  padding: EdgeInsets.all(16),
                  child: Text(
                    'Mountains',
                    style: TextStyle(fontSize: 28, color: Colors.white),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

画面サイズに合わせたレイアウトのすべてのレッスン

自分で練習してみよう: Flutterオンラインコンパイラ