Menu
CoddyTech

角に配置する

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

Stackの子要素は、左上隅から配置されます。別の場所に配置するには、Positionedで囲み、端からどれだけ離すかを指定します。

Stack(
  children: [
    Container(height: 200, color: Colors.grey),
    const Positioned(
      bottom: 8,
      left: 8,
      child: Text('Bottom left'),
    ),
  ],
)

top:、bottom:、left:、right:は、Stackの各辺からのピクセル単位の距離です。それぞれの方向について、topまたはbottomのいずれか一方と、leftまたはrightのいずれか一方を指定します。

Positionedの子要素は、Stackのサイズを変えません。サイズは引き続き写真によって決まります。

challenge icon

チャレンジ

簡単

写真の右上隅にあるSALEバッジを、上端から12ピクセル、右端から12ピクセルの位置に移動します。

自分で試してみよう

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('Shop')),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            children: [
              Stack(
                children: [
                  Container(
                    height: 200,
                    color: Colors.blueGrey.shade100,
                    child: const Center(child: Icon(Icons.image, size: 64)),
                  ),
                  Container(
                    padding: const EdgeInsets.all(8),
                    color: Colors.red,
                    child: const Text('SALE', style: TextStyle(color: Colors.white)),
                  ),
                ],
              ),
              const SizedBox(height: 12),
              const Text('Running shoes', style: TextStyle(fontSize: 20)),
            ],
          ),
        ),
      ),
    );
  }
}
quiz icon腕試し

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

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

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