Programming/Dart+Flutter

[기말고사 대비] 2. home page

주눅 2025. 6. 11. 13:07

2. home page 

import 'package:flutter/material.dart';

// 장소 모델 클래스
class Place {
  final String id;      // 장소 고유 ID
  final String name;    // 장소명
  final String address; // 주소
  String memo;          // 사용자가 적는 메모

  Place({
    required this.id,
    required this.name,
    required this.address,
    this.memo = '',
  });
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 저장된 장소 목록, 임시 저장용
  final List<Place> _places = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('장소 메모 목록'),
      ),
      body: _places.isEmpty
          ? const Center(
        child: Text('저장된 장소가 없습니다. + 버튼을 눌러 추가하세요.'),
      )
          : ListView.builder(
        itemCount: _places.length,
        itemBuilder: (context, index) {
          final place = _places[index];
          return ListTile(
            title: Text(place.name),
            subtitle: Text(place.address),
            trailing: IconButton(
              icon: const Icon(Icons.delete, color: Colors.red),
              onPressed: () {
                // 삭제 확인 다이얼로그
                showDialog(
                  context: context,
                  builder: (context) => AlertDialog(
                    title: const Text('삭제 확인'),
                    content: Text('${place.name} 을(를) 삭제하시겠습니까?'),
                    actions: [
                      TextButton(
                        onPressed: () => Navigator.pop(context),
                        child: const Text('취소'),
                      ),
                      TextButton(
                        onPressed: () {
                          setState(() {
                            _places.removeAt(index);
                          });
                          Navigator.pop(context);
                        },
                        child: const Text('삭제'),
                      ),
                    ],
                  ),
                );
              },
            ),
            onTap: () async {
              // 상세화면으로 이동하며 수정된 데이터 받아오기
              final updatedPlace = await Navigator.pushNamed(
                context,
                '/detail',
                arguments: place,
              );

              if (updatedPlace != null && updatedPlace is Place) {
                setState(() {
                  _places[index] = updatedPlace;
                });
              }
            },
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          // 검색 화면으로 이동 후 선택한 장소 받기
          final newPlace = await Navigator.pushNamed(context, '/search');
          if (newPlace != null && newPlace is Place) {
            setState(() {
              _places.add(newPlace);
            });
          }
        },
        tooltip: '장소 추가',
        child: const Icon(Icons.add),
      ),
    );
  }
}