Menu
CoddyTech

모서리에 배치하기

Coddy Flutter 여정의 화면 크기에 맞는 레이아웃 섹션에 포함된 레슨. 30개 중 11번째.

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 컴파일러