[flutter] Swipeable Widget을 사용하여 슬라이드 메뉴 토글 기능 구현하기

이번 포스트에서는 Flutter 앱에서 Swipeable Widget을 사용하여 슬라이드 메뉴의 토글 기능을 구현하는 방법에 대해 알아보겠습니다.

Swipeable Widget이란?

Swipeable Widget은 사용자가 터치하여 스와이프할 수 있는 인터랙티브한 위젯입니다. 사용자가 해당 위젯을 스와이프하면 특정 액션을 수행할 수 있도록 도와줍니다.

필요한 패키지 가져오기

먼저, flutter_swipe_action_cell 패키지를 pubspec.yaml 파일에 추가합니다.

dependencies:
  flutter_swipe_action_cell: ^1.1.1

그런 다음 pub get 명령을 실행하여 패키지를 가져옵니다.

Swipeable 메뉴 토글 기능 구현하기

다음으로, flutter_swipe_action_cell 패키지의 기능을 사용하여 슬라이드 메뉴의 토글 기능을 구현해보겠습니다.

import 'package:flutter/material.dart';
import 'package:flutter_swipe_action_cell/flutter_swipe_action_cell.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Swipeable Menu Example'),
        ),
        body: ListView.builder(
          itemCount: 10,
          itemBuilder: (context, index) {
            return SwipeActionCell(
              key: ObjectKey(index),
              trailingActions: [
                SwipeAction(
                  onTap: (CompletionType type) {
                    // 트리거되었을 때 실행할 액션 정의
                  },
                  color: Colors.green,
                  icon: Icons.check,
                ),
                SwipeAction(
                  onTap: (CompletionType type) {
                    // 트리거되었을 때 실행할 액션 정의
                  },
                  color: Colors.red,
                  icon: Icons.delete,
                ),
              ],
              child: ListTile(
                title: Text('아이템 $index'),
              ),
            );
          },
        ),
      ),
    );
  }
}

위 예제에서는 flutter_swipe_action_cell 패키지를 사용하여 Swipeable Widget을 구현하였습니다.

결론

이제 해당 Flutter 앱을 실행하면 리스트의 각 항목을 스와이프하여 특정 액션을 수행할 수 있는 Swipeable Widget이 동작합니다. Swipeable Widget을 통해 사용자 친화적이고 멋진 인터랙션을 구현할 수 있습니다.

Swipeable Widget을 사용하여 슬라이드 메뉴 토글 기능을 구현하는 방법을 알아보았습니다. 아마도 여러분은 이러한 기능을 사용하여 앱의 사용성을 향상시킬 수 있을 것입니다.

참고 자료