flutter_foreground_task 1.0.1 copy "flutter_foreground_task: ^1.0.1" to clipboard
flutter_foreground_task: ^1.0.1 copied to clipboard

outdated

This plugin is used to implement a foreground service on the Android platform.

example/lib/main.dart

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

void main() => runApp(ExampleApp());

class ExampleApp extends StatefulWidget {
  @override
  _ExampleAppState createState() => _ExampleAppState();
}

class _ExampleAppState extends State<ExampleApp> {
  final flutterForegroundTask = FlutterForegroundTask.instance.init(
    notificationOptions: NotificationOptions(
      channelId: 'notification_channel_id',
      channelName: 'Foreground Notification',
      channelDescription: 'This notification appears when the foreground task is running.'
    ),
    foregroundTaskOptions: ForegroundTaskOptions(
      interval: 5000
    )
  );

  void startForegroundTask() {
    flutterForegroundTask.start(
      notificationTitle: 'Foreground task is running',
      notificationText: 'Tap to return to the app',
      taskCallback: (DateTime timestamp) {
        print('timestamp: $timestamp');
      }
    );
  }
  
  void stopForegroundTask() {
    flutterForegroundTask.stop();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // A widget that prevents the app from closing when the foreground task is running.
      // Declare between the [MaterialApp] and [Scaffold] widgets.
      home: WithForegroundTask(
        foregroundTask: flutterForegroundTask,
        child: Scaffold(
          appBar: AppBar(
            title: const Text('Flutter Foreground Task'),
            centerTitle: true
          ),
          body: buildContentView()
        ),
      ),
    );
  }

  Widget buildContentView() {
    final buttonBuilder = (String text, {VoidCallback onPressed}) {
      return ElevatedButton(
        child: Text(text),
        onPressed: onPressed
      );
    };

    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          buttonBuilder('start', onPressed: startForegroundTask),
          buttonBuilder('stop', onPressed: stopForegroundTask)
        ],
      ),
    );
  }
}
346
likes
0
pub points
97%
popularity

Publisher

unverified uploader

This plugin is used to implement a foreground service on the Android platform.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_foreground_task