[flutter] 플러터 Stepper를 사용하여 사용자가 선택한 시간 단위로 알림을 설정하는 알림 및 리마인더 애플리케이션을 만들어보자.

알림 및 리마인더 애플리케이션을 만들기 위해 Flutter의 “Stepper” 위젯을 사용할 것입니다. 이 앱은 사용자가 선택한 시간 단위로 알림을 설정할 수 있는 기능을 제공합니다.

단계 1: 프로젝트 설정 및 필요한 패키지 임포트

먼저 Flutter 프로젝트를 설정하고 필요한 패키지를 임포트해야 합니다. 이 애플리케이션은 flutter_local_notifications 패키지를 사용하여 알림을 발송할 것입니다. 따라서 pubspec.yaml 파일에 해당 패키지를 추가해줍니다.

dependencies:
  flutter_local_notifications: ^5.2.0

그런 다음, 프로젝트를 업데이트하기 위해 터미널에서 다음 명령어를 실행합니다.

flutter pub get

단계 2: UI 및 알림 설정

이제 애플리케이션의 UI 및 알림 설정을 구현해보겠습니다. main.dart 파일을 열고 다음 코드로 교체합니다.

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


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

class MyApp extends StatelessWidget {
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('알림 및 리마인더'),
        ),
        body: Center(
          child: Stepper(
            steps: <Step>[
              Step(
                title: Text('시간 설정'),
                isActive: true,
                content: Text('시간을 선택하세요.'),
              ),
              Step(
                title: Text('알림 타입 설정'),
                isActive: false,
                content: Text('알림 타입을 선택하세요.'),
              ),
              Step(
                title: Text('알림 내용 입력'),
                isActive: false,
                content: Text('알림 내용을 입력하세요.'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

이 코드는 기본적인 알림 및 리마인더 애플리케이션의 UI를 구현합니다. Stepper 위젯을 사용하여 세 가지 단계로 구성되어 있습니다.

단계 3: 알림 핸들링

다음으로, 알림을 발송하는 핸들링 기능을 추가해보겠습니다. main.dart 파일의 MyApp 클래스에 다음 코드를 추가합니다.

Future<void> _scheduleNotification() async {
  var scheduledNotificationDateTime = DateTime.now().add(Duration(seconds: 5));
  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'channel_id',
    'channel_name',
    'channel_description',
    importance: Importance.max,
    priority: Priority.high,
  );
  var iOSPlatformChannelSpecifics = IOSNotificationDetails();
  var platformChannelSpecifics = NotificationDetails(
    android: androidPlatformChannelSpecifics,
    iOS: iOSPlatformChannelSpecifics,
  );
  await flutterLocalNotificationsPlugin.schedule(
    0,
    '알림',
    '시간이 되었습니다!',
    scheduledNotificationDateTime,
    platformChannelSpecifics,
  );
}

Stepper(
  steps: <Step>[
    Step(
      title: Text('시간 설정'),
      isActive: true,
      content: Text('시간을 선택하세요.'),
    ),
    Step(
      title: Text('알림 타입 설정'),
      isActive: false,
      content: Text('알림 타입을 선택하세요.'),
    ),
    Step(
      title: Text('알림 내용 입력'),
      isActive: false,
      content: Text('알림 내용을 입력하세요.'),
    ),
  ],
  onStepContinue: () async {
    if (currentStep == 0) {
      currentStep++;
    } else if (currentStep == 1) {
      currentStep++;
    } else {
      // Save reminder and schedule notification
      await _scheduleNotification();
    }
    setState(() {});
  },

위 코드는 _scheduleNotification 메서드를 추가하여 알림을 예약하고, onStepContinue 콜백에서 단계를 진행하거나 알림을 예약합니다.

단계 4: 앱 실행

이제 애플리케이션을 실행해보세요. 앱 화면에는 세 개의 단계가 표시됩니다. 각 단계에는 제목과 내용이 있으며, 첫 번째 단계는 현재 활성화됩니다. 마지막 단계에서 ‘다음’ 버튼을 클릭하면 알림이 예약됩니다.

flutter run

알림 및 리마인더 앱을 만들기 위해 Flutter의 Stepper 위젯을 사용하는 방법에 대해 알아보았습니다. 이제 사용자가 선택한 시간 단위로 알림을 설정할 수 있는 애플리케이션을 만들 수 있습니다!

참고 자료