[Flutter] BottomSheet 위젯

2024. 11. 12. 11:54·Flutter
  • 화면 아래쪽에서 올라오는 다이얼로그
  • showBottomSheet() 및 showModalBottomSheet()의 builder 속성에 지정한 위젯을 화면 아래쪽에서 올라오도록 보여줌
    • showModalBottomSheet()는 사용자가 Bottom Sheet 외부를 터치하면 Bottom Sheet가 자동으로 닫히지만, showBottomSheet()는 사용자가 Bottom Sheet 외부를 터치해도 Bottom Sheet가 닫히지 않음
  • 다이얼로그를 닫기 위해서는 닫히게 하려면, Navigator.of(context).pop()을 호출하면 됨

 

import 'package:flutter/material.dart';

void main(){
  runApp(BottomSheetApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'myApp1',
      theme: ThemeData(
        useMaterial3: true,
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue)
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text('Bottom Sheet Sample'),),
        body: MyBottomSheet(),
      ),
    );
  }
}

// 새로운 클래스
class MyBottomSheet extends StatelessWidget {
  const MyBottomSheet({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        child: const Text('show bottom sheet'),
        onPressed: () {
          // 이벤트 핸들러 처리
          //showBottomSheet(context: context, builder: builder)
          showModalBottomSheet(context: context, builder: (context) {

            return Container(
              child: Column(
                children: [
                    ListTile(
                      leading: Icon(Icons.add),
                      title: Text('Add Item'),
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                    ),
                  ListTile(
                    leading: Icon(Icons.remove),
                    title: Text('Remove Item'),
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              ),
            );
          });
        },
      ),
    );
  }
}

'Flutter' 카테고리의 다른 글

[Flutter] Dio 통신  (0) 2024.11.12
[Flutter] TabBar 위젯  (0) 2024.11.12
[Flutter] 쇼핑 카트 앱 만들어 보기  (3) 2024.11.11
[Flutter] PageView 위젯  (0) 2024.11.11
[Flutter] GridView 위젯  (1) 2024.11.11
'Flutter' 카테고리의 다른 글
  • [Flutter] Dio 통신
  • [Flutter] TabBar 위젯
  • [Flutter] 쇼핑 카트 앱 만들어 보기
  • [Flutter] PageView 위젯
미로910
미로910
개발자를 꿈꾸는 민경이의 기록 블로그
  • 미로910
    개발 note
    미로910
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 설치 메뉴얼
      • HTML
      • JAVA
        • Java 기초
        • Java 응용
        • 자료구조
        • HTTP
        • JSP 프로그래밍
      • MySQL
        • MySQL 기본
        • 1일 1쿼리
      • CSS
      • Spring boot
      • JS
        • 게시판 만들기
      • Git
      • Flutter
        • MVVM 활용
        • 심화 버전
        • 1일 1 Flutter
      • 디자인 패턴의 활용
      • error note
      • My Project
        • [졸작] LLM 기반 특허 유사도 분석 시스템
        • 도서 관리 프로그램 (final project)
        • amigo
        • 친구 매칭 프로그램(FMP)
      • Python
      • 딥러닝
      • 네트워크
      • 공부 노트
        • 연구회
        • 자료구조
      • 기타
  • 블로그 메뉴

    • 홈
    • 전체보기
    • -----------------------
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Flutter
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
미로910
[Flutter] BottomSheet 위젯
상단으로

티스토리툴바