From cbdb86c2c0688ef0b396b5ee4c79e60452035b69 Mon Sep 17 00:00:00 2001 From: Midori Date: Sat, 29 Jul 2023 22:23:50 +0900 Subject: [PATCH] Refactor --- .../integration_test/integration_test.dart | 1 - example/integration_test/test_app.dart | 8 +- example/lib/main.dart | 98 ++++++++----------- 3 files changed, 44 insertions(+), 63 deletions(-) diff --git a/example/integration_test/integration_test.dart b/example/integration_test/integration_test.dart index ec36e3c2..43a2bd2a 100644 --- a/example/integration_test/integration_test.dart +++ b/example/integration_test/integration_test.dart @@ -6,7 +6,6 @@ import 'package:flutter/material.dart' show Key; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; -// ignore: avoid_relative_lib_imports import 'test_app.dart' as app; /// All tests are done in integration tests, diff --git a/example/integration_test/test_app.dart b/example/integration_test/test_app.dart index 893adbea..88b774ed 100644 --- a/example/integration_test/test_app.dart +++ b/example/integration_test/test_app.dart @@ -16,13 +16,13 @@ class App extends StatefulWidget { } class _AppState extends State { - bool isLoading = false; + bool isTesting = false; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( - child: isLoading + child: isTesting ? const CircularProgressIndicator() : SingleChildScrollView( child: Column( @@ -80,14 +80,14 @@ class _AppState extends State { key: Key(label), onPressed: () async { logger = Logger(); - setState(() => isLoading = true); + setState(() => isTesting = true); try { await onPressed(); } catch (e, st) { logger.error = e; logger.stackTrace = st; } finally { - setState(() => isLoading = false); + setState(() => isTesting = false); } }, child: Text(label), diff --git a/example/lib/main.dart b/example/lib/main.dart index 54aff907..ff62f104 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -14,14 +14,6 @@ class App extends StatelessWidget { @override Widget build(BuildContext context) { - final snackBar = SnackBar( - content: const Text('Saved! ✅'), - action: SnackBarAction( - label: 'Gallery ->', - onPressed: () async => Gal.open(), - ), - ); - return MaterialApp( theme: ThemeData( useMaterial3: true, @@ -33,13 +25,16 @@ class App extends StatelessWidget { child: SingleChildScrollView( child: Column( children: [ - _Button( + FilledButton( onPressed: () async { final requestGranted = await Gal.requestAccess(); if (requestGranted) { final path = await getFilePath('assets/done.jpg'); try { await Gal.putImage(path); + + if (!context.mounted) return; + ScaffoldMessenger.of(context).showSnackBar(snackBar); } on GalException catch (e) { log(e.toString()); } @@ -61,44 +56,31 @@ class App extends StatelessWidget { ), ); }, - label: 'Best Practice', + child: const Text('Best Practice'), ), - _Button( + FilledButton( onPressed: () async => Gal.open(), - label: 'Open Gallery', + child: const Text('Open Gallery'), ), - _Button( + FilledButton( onPressed: () async { final path = await getFilePath('assets/done.mp4'); await Gal.putVideo(path); if (!context.mounted) return; ScaffoldMessenger.of(context).showSnackBar(snackBar); }, - label: 'Save Video from local', - ), - _Button( - onPressed: () async { - final path = '${Directory.systemTemp.path}/done.mp4'; - await Dio().download( - 'https://github.com/natsuk4ze/gal/raw/main/example/assets/done.mp4', - path, - ); - await Gal.putVideo(path); - if (!context.mounted) return; - ScaffoldMessenger.of(context).showSnackBar(snackBar); - }, - label: 'Download Video', + child: const Text('Save Video from file path'), ), - _Button( + FilledButton( onPressed: () async { final path = await getFilePath('assets/done.jpg'); await Gal.putImage(path); if (!context.mounted) return; ScaffoldMessenger.of(context).showSnackBar(snackBar); }, - label: 'Save Image from local', + child: const Text('Save Image from file path'), ), - _Button( + FilledButton( onPressed: () async { final byteData = await rootBundle.load('assets/done.jpg'); final uint8List = byteData.buffer.asUint8List( @@ -107,9 +89,9 @@ class App extends StatelessWidget { if (!context.mounted) return; ScaffoldMessenger.of(context).showSnackBar(snackBar); }, - label: 'Save Image from bytes', + child: const Text('Save Image from bytes'), ), - _Button( + FilledButton( onPressed: () async { final path = '${Directory.systemTemp.path}/done.jpg'; await Dio().download( @@ -120,21 +102,34 @@ class App extends StatelessWidget { if (!context.mounted) return; ScaffoldMessenger.of(context).showSnackBar(snackBar); }, - label: 'Download Image', + child: const Text('Download Image'), + ), + FilledButton( + onPressed: () async { + final path = '${Directory.systemTemp.path}/done.mp4'; + await Dio().download( + 'https://github.com/natsuk4ze/gal/raw/main/example/assets/done.mp4', + path, + ); + await Gal.putVideo(path); + if (!context.mounted) return; + ScaffoldMessenger.of(context).showSnackBar(snackBar); + }, + child: const Text('Download Video'), ), - _Button( + FilledButton( onPressed: () async { final hasAccess = await Gal.hasAccess(); log('Has Access:${hasAccess.toString()}'); }, - label: 'Has Access', + child: const Text('Has Access'), ), - _Button( + FilledButton( onPressed: () async { final requestGranted = await Gal.requestAccess(); log('Request Granted:${requestGranted.toString()}'); }, - label: 'Request Access', + child: const Text('Request Access'), ), ], ), @@ -145,6 +140,14 @@ class App extends StatelessWidget { ); } + SnackBar get snackBar => SnackBar( + content: const Text('Saved! ✅'), + action: SnackBarAction( + label: 'Gallery ->', + onPressed: () async => Gal.open(), + ), + ); + Future getFilePath(String path) async { final byteData = await rootBundle.load(path); final file = await File( @@ -155,24 +158,3 @@ class App extends StatelessWidget { return file.path; } } - -class _Button extends StatelessWidget { - const _Button({ - required this.label, - required this.onPressed, - }); - - final String label; - final void Function() onPressed; - - @override - Widget build(BuildContext context) { - return Padding( - padding: const EdgeInsets.all(12.0), - child: FloatingActionButton.extended( - onPressed: onPressed, - label: Text(label), - ), - ); - } -}