Skip to content

migrate to flutter 3.22.2 / update packages #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void main() {
});

group('binary uploads', () {
final url = baseUrl.replace(path: baseUrl.path + 'Binary');
final url = baseUrl.replace(path: '${baseUrl.path}Binary');

testWidgets('single file', (WidgetTester tester) async {
final taskId = await uploader.enqueue(
Expand Down
127 changes: 53 additions & 74 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,31 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
// ignore: depend_on_referenced_packages
import 'package:flutter_uploader/flutter_uploader.dart';
import 'package:flutter_uploader_example/responses_screen.dart';
import 'package:flutter_uploader_example/upload_screen.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

import 'responses_screen.dart';
import 'upload_screen.dart';

const String title = 'FileUpload Sample app';
final Uri uploadURL = Uri.parse(
'https://us-central1-flutteruploadertest.cloudfunctions.net/upload',
);
final Uri uploadURL = Uri.parse('https://us-central1-flutteruploadertest.cloudfunctions.net/upload');

FlutterUploader _uploader = FlutterUploader();

void backgroundHandler() {
WidgetsFlutterBinding.ensureInitialized();

// Notice these instances belong to a forked isolate.
var uploader = FlutterUploader();

var notifications = FlutterLocalNotificationsPlugin();

// Only show notifications for unprocessed uploads.
SharedPreferences.getInstance().then((preferences) {
var processed = preferences.getStringList('processed') ?? <String>[];

if (Platform.isAndroid) {
uploader.progress.listen((progress) {
if (processed.contains(progress.taskId)) {
return;
}
if (processed.contains(progress.taskId)) return;

notifications.show(
progress.taskId.hashCode,
Expand All @@ -43,8 +38,7 @@ void backgroundHandler() {
android: AndroidNotificationDetails(
'FlutterUploader.Example',
'FlutterUploader',
channelDescription:
'Installed when you activate the Flutter Uploader Example',
channelDescription: 'Installed when you activate the Flutter Uploader Example',
progress: progress.progress ?? 0,
icon: 'ic_upload',
enableVibration: false,
Expand All @@ -54,24 +48,20 @@ void backgroundHandler() {
maxProgress: 100,
channelShowBadge: false,
),
iOS: const IOSNotificationDetails(),
iOS: const DarwinNotificationDetails(),
),
);
});
}

uploader.result.listen((result) {
if (processed.contains(result.taskId)) {
return;
}
if (processed.contains(result.taskId)) return;

processed.add(result.taskId);
preferences.setStringList('processed', processed);

notifications.cancel(result.taskId.hashCode);

final successful = result.status == UploadTaskStatus.complete;

var title = 'Upload Complete';
if (result.status == UploadTaskStatus.failed) {
title = 'Upload Failed';
Expand All @@ -88,21 +78,18 @@ void backgroundHandler() {
android: AndroidNotificationDetails(
'FlutterUploader.Example',
'FlutterUploader',
channelDescription:
'Installed when you activate the Flutter Uploader Example',
channelDescription: 'Installed when you activate the Flutter Uploader Example',
icon: 'ic_upload',
enableVibration: !successful,
importance: result.status == UploadTaskStatus.failed
? Importance.high
: Importance.min,
enableVibration: result.status == UploadTaskStatus.failed,
importance: result.status == UploadTaskStatus.failed ? Importance.high : Importance.min,
),
iOS: const IOSNotificationDetails(
iOS: const DarwinNotificationDetails(
presentAlert: true,
),
),
)
.catchError((e, stack) {
print('error while showing notification: $e, $stack');
print('Error while showing notification: $e, $stack');
});
});
});
Expand All @@ -111,15 +98,15 @@ void backgroundHandler() {
void main() => runApp(const App());

class App extends StatefulWidget {
const App({Key? key}) : super(key: key);
const App({super.key});

@override
// ignore: library_private_types_in_public_api
_AppState createState() => _AppState();
}

class _AppState extends State<App> {
int _currentIndex = 0;

bool allowCellular = true;

@override
Expand All @@ -128,57 +115,57 @@ class _AppState extends State<App> {

_uploader.setBackgroundHandler(backgroundHandler);

_initializeNotifications();
_loadAllowCellularPreference();
}

Future<void> _initializeNotifications() async {
var flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
const AndroidInitializationSettings('ic_upload');
var initializationSettingsIOS = IOSInitializationSettings(
var initializationSettingsAndroid = const AndroidInitializationSettings('ic_upload');
var initializationSettingsIOS = DarwinInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: true,
onDidReceiveLocalNotification:
(int id, String? title, String? body, String? payload) async {},
onDidReceiveLocalNotification: (int id, String? title, String? body, String? payload) async {},
);
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: (payload) async {},
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
}

SharedPreferences.getInstance()
.then((sp) => sp.getBool('allowCellular') ?? true)
.then((result) {
if (mounted) {
setState(() {
allowCellular = result;
});
}
});
Future<void> _loadAllowCellularPreference() async {
final sp = await SharedPreferences.getInstance();
final result = sp.getBool('allowCellular') ?? true;
if (mounted) {
setState(() {
allowCellular = result;
});
}
}

Future<void> _toggleAllowCellular() async {
final sp = await SharedPreferences.getInstance();
await sp.setBool('allowCellular', !allowCellular);
if (mounted) {
setState(() {
allowCellular = !allowCellular;
});
}
}

@override
Widget build(BuildContext context) {
return MaterialApp(
title: title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
theme: ThemeData(primarySwatch: Colors.blue),
home: Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(allowCellular
? Icons.signal_cellular_connected_no_internet_4_bar
: Icons.wifi_outlined),
onPressed: () async {
final sp = await SharedPreferences.getInstance();
await sp.setBool('allowCellular', !allowCellular);
if (mounted) {
setState(() {
allowCellular = !allowCellular;
});
}
},
icon: Icon(allowCellular ? Icons.signal_cellular_connected_no_internet_4_bar : Icons.wifi_outlined),
onPressed: _toggleAllowCellular,
),
],
),
Expand All @@ -190,19 +177,11 @@ class _AppState extends State<App> {
setState(() => _currentIndex = 1);
},
)
: ResponsesScreen(
uploader: _uploader,
),
: ResponsesScreen(uploader: _uploader),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.cloud_upload),
label: 'Upload',
),
BottomNavigationBarItem(
icon: Icon(Icons.receipt),
label: 'Responses',
),
BottomNavigationBarItem(icon: Icon(Icons.cloud_upload), label: 'Upload'),
BottomNavigationBarItem(icon: Icon(Icons.receipt), label: 'Responses'),
],
onTap: (newIndex) {
setState(() => _currentIndex = newIndex);
Expand Down
20 changes: 7 additions & 13 deletions example/lib/responses_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
import 'dart:async';

import 'package:flutter/material.dart';
// ignore: depend_on_referenced_packages
import 'package:flutter_uploader/flutter_uploader.dart';
import 'package:flutter_uploader_example/upload_item.dart';
import 'package:flutter_uploader_example/upload_item_view.dart';

/// Shows the statusresponses for previous uploads.
///
class ResponsesScreen extends StatefulWidget {
const ResponsesScreen({
Key? key,
required this.uploader,
}) : super(key: key);

const ResponsesScreen({super.key, required this.uploader});
final FlutterUploader uploader;

@override
_ResponsesScreenState createState() => _ResponsesScreenState();
State<ResponsesScreen> createState() => _ResponsesScreenState();
}

class _ResponsesScreenState extends State<ResponsesScreen> {
Expand All @@ -33,15 +30,13 @@ class _ResponsesScreenState extends State<ResponsesScreen> {

_progressSubscription = widget.uploader.progress.listen((progress) {
final task = _tasks[progress.taskId];
print(
'In MAIN APP: ID: ${progress.taskId}, progress: ${progress.progress}');
print('In MAIN APP: ID: ${progress.taskId}, progress: ${progress.progress}');
if (task == null) return;
if (task.isCompleted()) return;

var tmp = <String, UploadItem>{}..addAll(_tasks);
tmp.putIfAbsent(progress.taskId, () => UploadItem(progress.taskId));
tmp[progress.taskId] =
task.copyWith(progress: progress.progress, status: progress.status);
tmp[progress.taskId] = task.copyWith(progress: progress.progress, status: progress.status);
setState(() => _tasks = tmp);
}, onError: (ex, stacktrace) {
print('exception: $ex');
Expand All @@ -54,8 +49,7 @@ class _ResponsesScreenState extends State<ResponsesScreen> {

var tmp = <String, UploadItem>{}..addAll(_tasks);
tmp.putIfAbsent(result.taskId, () => UploadItem(result.taskId));
tmp[result.taskId] =
tmp[result.taskId]!.copyWith(status: result.status, response: result);
tmp[result.taskId] = tmp[result.taskId]!.copyWith(status: result.status, response: result);

setState(() => _tasks = tmp);
}, onError: (ex, stacktrace) {
Expand Down
18 changes: 5 additions & 13 deletions example/lib/upload_item_view.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// ignore_for_file: public_member_api_docs

import 'package:flutter/material.dart';

import 'package:flutter_uploader/flutter_uploader.dart';
import 'package:flutter_uploader_example/upload_item.dart';

Expand All @@ -12,10 +11,10 @@ class UploadItemView extends StatelessWidget {
final CancelUploadCallback onCancel;

const UploadItemView({
Key? key,
super.key,
required this.item,
required this.onCancel,
}) : super(key: key);
});

@override
Widget build(BuildContext context) {
Expand All @@ -27,10 +26,7 @@ class UploadItemView extends StatelessWidget {
children: <Widget>[
Text(
item.id,
style: Theme.of(context)
.textTheme
.caption!
.copyWith(fontFamily: 'monospace'),
style: Theme.of(context).textTheme.bodySmall!.copyWith(fontFamily: 'monospace'),
),
Container(
height: 5.0,
Expand All @@ -51,16 +47,12 @@ class UploadItemView extends StatelessWidget {
Container(height: 5.0),
if (item.status == UploadTaskStatus.running)
LinearProgressIndicator(value: item.progress!.toDouble() / 100),
if (item.status == UploadTaskStatus.complete ||
item.status == UploadTaskStatus.failed) ...[
if (item.status == UploadTaskStatus.complete || item.status == UploadTaskStatus.failed) ...[
Text('HTTP status code: ${item.response!.statusCode}'),
if (item.response!.response != null)
Text(
item.response!.response!,
style: Theme.of(context)
.textTheme
.caption!
.copyWith(fontFamily: 'monospace'),
style: Theme.of(context).textTheme.bodySmall!.copyWith(fontFamily: 'monospace'),
),
]
],
Expand Down
Loading