Add device preview
This commit is contained in:
+3
-43
@@ -1,47 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:universal_ble/universal_ble.dart';
|
||||
import 'package:universal_ble_example/data/storage_service.dart';
|
||||
import 'package:universal_ble_example/home/permission_screen.dart';
|
||||
import 'package:universal_ble_example/home/scanner_screen.dart';
|
||||
import 'package:universal_ble_example/universal_ble_app.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await StorageService.instance.init();
|
||||
// await UniversalBle.setLogLevel(BleLogLevel.verbose);
|
||||
bool hasPermission = await UniversalBle.hasPermissions(
|
||||
withAndroidFineLocation: false,
|
||||
);
|
||||
|
||||
runApp(MyApp(
|
||||
hasPermission: hasPermission,
|
||||
));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
final bool hasPermission;
|
||||
const MyApp({super.key, required this.hasPermission});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Universal BLE',
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: Colors.blue,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
),
|
||||
darkTheme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: Colors.blue,
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
),
|
||||
themeMode: ThemeMode.system,
|
||||
home: hasPermission ? ScannerScreen() : const PermissionScreen(),
|
||||
);
|
||||
}
|
||||
bool hasPermission = await initializeApp();
|
||||
runApp(UniversalBleApp(hasPermission: hasPermission));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:device_preview_screenshot/device_preview_screenshot.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:universal_ble_example/universal_ble_app.dart';
|
||||
|
||||
void main() async {
|
||||
bool hasPermission = await initializeApp();
|
||||
|
||||
// On macOS: ~/Library/Containers/com.navideck.universalble/Data/Documents/screenshots
|
||||
final screenshotsDir = await getScreenshotsDirectory();
|
||||
debugPrint('Screenshots will be saved to: ${screenshotsDir.path}');
|
||||
|
||||
runApp(
|
||||
DevicePreview(
|
||||
enabled: true,
|
||||
builder: (context) => UniversalBleApp(
|
||||
hasPermission: hasPermission,
|
||||
locale: DevicePreview.locale(context),
|
||||
builder: DevicePreview.appBuilder,
|
||||
),
|
||||
devices: [
|
||||
createCustomDevice(
|
||||
name: 'App Store 1242x2688',
|
||||
physicalWidth: 1242,
|
||||
physicalHeight: 2688,
|
||||
),
|
||||
],
|
||||
tools: [
|
||||
...DevicePreview.defaultTools,
|
||||
DevicePreviewScreenshot(
|
||||
onScreenshot: screenshotAsFiles(screenshotsDir),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Gets or creates the screenshots directory in the app's documents directory.
|
||||
Future<Directory> getScreenshotsDirectory() async {
|
||||
final appDocumentsDir = await getApplicationDocumentsDirectory();
|
||||
final screenshotsDir = Directory('${appDocumentsDir.path}/screenshots');
|
||||
|
||||
if (!await screenshotsDir.exists()) {
|
||||
await screenshotsDir.create(recursive: true);
|
||||
}
|
||||
|
||||
return screenshotsDir;
|
||||
}
|
||||
|
||||
/// Creates a custom device with the specified physical pixel dimensions.
|
||||
DeviceInfo createCustomDevice({
|
||||
required String name,
|
||||
required double physicalWidth,
|
||||
required double physicalHeight,
|
||||
double pixelRatio = 3.0,
|
||||
}) {
|
||||
final logicalWidth = physicalWidth / pixelRatio;
|
||||
final logicalHeight = physicalHeight / pixelRatio;
|
||||
|
||||
return DeviceInfo(
|
||||
identifier: DeviceIdentifier(
|
||||
TargetPlatform.iOS,
|
||||
DeviceType.phone,
|
||||
name,
|
||||
),
|
||||
name: name,
|
||||
pixelRatio: pixelRatio,
|
||||
safeAreas: EdgeInsets.zero,
|
||||
rotatedSafeAreas: EdgeInsets.zero,
|
||||
screenPath: Path()
|
||||
..addRect(Rect.fromLTWH(0.0, 0.0, logicalWidth, logicalHeight)),
|
||||
frameSize: Size(logicalWidth, logicalHeight),
|
||||
screenSize: Size(logicalWidth, logicalHeight),
|
||||
framePainter: const _EmptyFramePainter(),
|
||||
);
|
||||
}
|
||||
|
||||
class _EmptyFramePainter extends CustomPainter {
|
||||
const _EmptyFramePainter();
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:universal_ble/universal_ble.dart';
|
||||
import 'package:universal_ble_example/data/storage_service.dart';
|
||||
import 'package:universal_ble_example/home/permission_screen.dart';
|
||||
import 'package:universal_ble_example/home/scanner_screen.dart';
|
||||
|
||||
/// Initializes the app services and checks permissions.
|
||||
/// Returns whether the app has the required permissions.
|
||||
Future<bool> initializeApp() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await StorageService.instance.init();
|
||||
// await UniversalBle.setLogLevel(BleLogLevel.verbose);
|
||||
return await UniversalBle.hasPermissions(
|
||||
withAndroidFineLocation: false,
|
||||
);
|
||||
}
|
||||
|
||||
class UniversalBleApp extends StatelessWidget {
|
||||
final bool hasPermission;
|
||||
final Locale? locale;
|
||||
final Widget Function(BuildContext, Widget?)? builder;
|
||||
|
||||
const UniversalBleApp({
|
||||
super.key,
|
||||
required this.hasPermission,
|
||||
this.locale,
|
||||
this.builder,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Universal BLE',
|
||||
debugShowCheckedModeBanner: false,
|
||||
locale: locale,
|
||||
builder: builder,
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: Colors.blue,
|
||||
brightness: Brightness.light,
|
||||
),
|
||||
),
|
||||
darkTheme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: ColorScheme.fromSeed(
|
||||
seedColor: Colors.blue,
|
||||
brightness: Brightness.dark,
|
||||
),
|
||||
),
|
||||
themeMode: ThemeMode.system,
|
||||
home: hasPermission ? ScannerScreen() : const PermissionScreen(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user