[Flutter] 6. Constraints

문정준's avatar
May 29, 2025
[Flutter] 6. Constraints
Contents
예제
 
💡

Widget Constraints

  1. 부모는 자식에게 제약을 전파한다 (그 전파를 외우고 있을 수 없다)
  1. 자식은 부모의 제약 안에서만 크기를 정할 수 있다.
  1. 자식이 크기를 주지 않으면, maxWidth, maxHeight에 맞춰진다.
 
  • Container
    • min : 0
    • max : double.infinity
    • 크기를 주지 않고, 자식이 있으면 (자식 크기만큼 줄어듬)
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Constraints Example')), body: Center( child: Container( color: Colors.blue, constraints: BoxConstraints( // 1. 부모는 자식에게 제약조건을 전파 minWidth: 0, minHeight: 0, maxWidth: double.infinity, maxHeight: double.infinity, ), child: Container( color: Colors.red, // 2. 자식은 부모의 제약조건 내에서만 동작 // 3. 자식은 크기가 없으면 Max 값을 따름 child: Center(child: Text('Child')), ), ), ), ), ); } }
 

예제

import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Constraints Example')), body: ListView( children: [ AspectRatio( aspectRatio: 1 / 1, child: Image.asset("test.png", fit: BoxFit.cover), ), ], ), ), ); } }
Share article

sxias