컨테이너 안에 위젯 추가하기

gov's avatar
Dec 27, 2024
컨테이너 안에 위젯 추가하기
💡
컨테이너 : 자식은 무조건 부모 크기를 따라간다 -> 해결) 스택, Align
 

1. Align

notion image
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: HomePage(), ); } } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Column( children: [ Container( width: 100, height: 100, decoration: BoxDecoration( border: Border.all(), borderRadius: BorderRadius.circular(50) ), child: Align( child: Container( width: 80, height: 80, decoration: BoxDecoration( borderRadius: BorderRadius.circular(40), color: Colors.red ), ), ), ) ], ), ); } }

2. Stack

notion image
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: HomePage(), ); } } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Column( children: [ Stack( children: [ Container( width: 100, height: 100, decoration: BoxDecoration( border: Border.all(), borderRadius: BorderRadius.circular(50) ), ), Positioned( left: 10, top: 10, child: Container( width: 80, height: 80, decoration: BoxDecoration( border: Border.all(), borderRadius: BorderRadius.circular(50) ), ), ) ], ), ], ), ); } }
 

3. 사용자 입력 창

notion image
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: HomePage(), ); } } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Column( children: [ Stack( children: [ Container( height: 200, width: 100, ), Container( width: 100, height: 100, decoration: BoxDecoration( border: Border.all(), borderRadius: BorderRadius.circular(50) ), ), Positioned( left: 10, top: 50, child: Container( width: 80, height: 80, decoration: BoxDecoration( border: Border.all(), borderRadius: BorderRadius.circular(40) ), ), ), ], ), // 원형 2개 Divider(), Stack( children: [ TextFormField( decoration: InputDecoration( hintText: "Username", contentPadding: EdgeInsets.only(top: 100, left: 20), suffixIcon: Icon(Icons.do_not_disturb_on_total_silence), focusedBorder: OutlineInputBorder(), enabledBorder: OutlineInputBorder(), ), ), Positioned( left: 15, top: 20, child: Icon(Icons.person), ), ], ) ], ), ); } }
 
Share article

goho