[Flutter] Dialog 숙제

멋진감자·2025년 6월 18일
0

Flutter

목록 보기
2/25
post-thumbnail

생김새

코드

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  MyApp({super.key});

  
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var name = ['닭강정', '마라샹궈', '소불고기'];

  
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (context) {
              return Dialog(
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.zero),
                child: Homework(),
              );
            },
          );
        },
      ),
      appBar: AppBar(),
      body: ListView.builder(
        itemCount: 3,
        itemBuilder: (context, i) {
          return ListTile(
            leading: Image.asset('grape.png'),
            title: Text(name[i]),
          );
        },
      ),
    );
  }
}

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

  
  Widget build(BuildContext context) {
    return SizedBox(
      width: 300,
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          spacing: 15,
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Contact',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
            TextField(),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TextButton(
                  onPressed: Navigator.of(context).pop,
                  child: Text('Cancel'),
                ),
                TextButton(
                  onPressed: Navigator.of(context).pop,
                  child: Text('OK'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

모범답안

class DialogUI extends StatelessWidget {
  DialogUI({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return Dialog(
      child: Container(
        width: 300,
        height: 300,
        child: Column(
          children: [
            TextField(),
            TextButton( child: Text('완료'), onPressed:(){} ),
            TextButton(
              child: Text('취소'),
              onPressed:(){ Navigator.pop(context); })
          ],
        ),
      ),
    );
  }
}
profile
난멋져

0개의 댓글