launcher_helper 0.4.4-dev+2005
launcher_helper: ^0.4.4-dev+2005 copied to clipboard
To help create launcher, this plugin provides information of installed apps & wallpaper, and extract colors for making UI from wallpaper or icon.
example/lib/main.dart
import 'dart:io' as io;
import 'dart:typed_data';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:launcher_helper/launcher_helper.dart';
import 'package:flutter/services.dart';
import 'core/utils/permission_handling.dart';
import 'package:image_picker/image_picker.dart';
import 'commons/routes.dart';
void main() async {
runApp(Root());
}
class Root extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Requesting runtime permissions
HandlerOfPermissions().requestPerm();
return MaterialApp(
title: 'Red',
onGenerateRoute: generateRoute,
themeMode: ThemeMode.system,
theme: ThemeData(primaryColor: Colors.blue),
home: ShowCase(),
);
}
}
class ShowCase extends StatefulWidget {
@override
_ShowCaseState createState() => _ShowCaseState();
}
class _ShowCaseState extends State<ShowCase> {
// Static method to show a [SnackBar] with `message`
static ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showFailure(
String message, BuildContext context) {
print("[Failure] $message");
return Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(message),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Showcase"),
),
body: ListView(
children: <Widget>[
ListTile(
title: Text("For Wallpaper"),
onTap: () async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
// Get wallpaper as binary data
Uint8List imageData = await LauncherHelper.getWallpaper;
// Pushing show Wallpaper page
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ShowWallpaper(
imageData,
),
),
);
} on PlatformException {
String message = 'Failed to get wallpaper, check permissions';
showFailure(message, context);
}
},
),
ListTile(
title: Text("For any image"),
onTap: () async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
// Get image from image picker
io.File image =
await ImagePicker.pickImage(source: ImageSource.gallery);
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ShowImage(
// using readAsBytesSync to get Uint8List from image
image.readAsBytesSync(),
),
),
);
} catch (e) {
String message = 'Failed to get image';
showFailure(message, context);
}
},
),
ListTile(
title: Text("For Device applications"),
onTap: () {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
// Get all apps
LauncherHelper.getApplications().then(
(ApplicationCollection apps) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => AppListPage(
appList: apps,
),
),
);
},
);
} on PlatformException {
String message = 'Failed to get apps';
showFailure(message, context);
}
},
),
],
),
);
}
}
class ShowWallpaper extends StatefulWidget {
final Uint8List imageData;
const ShowWallpaper(this.imageData);
@override
_ShowWallpaperState createState() => _ShowWallpaperState();
}
class _ShowWallpaperState extends State<ShowWallpaper> {
int brightness;
double luminance, brightnessDart;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Methods for wallpaper"),
),
body: GridView.count(
crossAxisCount: 1,
mainAxisSpacing: 5,
children: <Widget>[
Card(
child: GridTile(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.memory(
widget.imageData,
),
),
footer: Padding(
padding: const EdgeInsets.all(4.0),
child: Text("Home screen wallpaper"),
),
),
),
ListTile(
title: Text(
"Tap to check wallpaper brightness (using native method)\nReturns a brightness level between 0 and 255, where 0 = totally black and 255 = totally bright."),
subtitle: Text(brightness?.toString() ?? ""),
onTap: () async {
brightness =
await LauncherHelper.calculateBrightness(widget.imageData);
print("Brightness (native): $brightness");
setState(() {
brightness = brightness;
});
},
),
],
),
);
}
}
class ShowImage extends StatefulWidget {
final Uint8List imageData;
const ShowImage(this.imageData);
@override
_ShowImageState createState() => _ShowImageState();
}
class _ShowImageState extends State<ShowImage> {
int brightness;
double luminance, brightnessDart;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Methods for any image"),
),
body: ListView(
children: <Widget>[
Card(
child: ListTile(
title: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.memory(
widget.imageData,
fit: BoxFit.fitWidth,
),
),
subtitle: Padding(
padding: const EdgeInsets.all(4.0),
child: Text("This is the image you picked"),
),
),
),
ListTile(
title: Text(
"Tap to check image brightness (using native method)\nReturns a brightness level between 0 and 255, where 0 = totally black and 255 = totally bright."),
subtitle: Text(brightness?.toString() ?? ""),
onTap: () async {
// Method to calculate brightness using native method from platform channel
brightness =
await LauncherHelper.calculateBrightness(widget.imageData);
print("Brightness (native): $brightness");
// updates brightness state
setState(() {
brightness = brightness;
});
},
),
],
),
);
}
}
class AppListPage extends StatelessWidget {
final ApplicationCollection appList;
AppListPage({this.appList});
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[400],
appBar: AppBar(
title: Text('Installed ${appList.length} Apps'),
),
body: ListView.builder(
itemCount: appList.length,
itemBuilder: (BuildContext context, int index) {
var app = appList.toList()[index];
return AppIconShape(
data: AppIconShapeData.squircle(),
child: ListTile(
onTap: () {
// LauncherHelper.launchApp(app.packageName);
return customDialogBox(app, context);
},
leading: Container(
child: app.icon,
),
title: Text(
app.label,
),
subtitle: Text(
app.packageName,
),
),
);
},
),
);
}
}
Future customDialogBox(Application app, BuildContext context) async {
// await app.update(); // No need to update
bool isEnabled, doesExist;
try {
isEnabled = await LauncherHelper.isApplicationEnabled(app.packageName);
doesExist = await LauncherHelper.doesApplicationExist(app.packageName);
} on PlatformException {
print("Platform error");
}
var palette = await LauncherHelper.generatePalette(app.icon.foreground.bytes);
Color dominantColor = palette.colors.isNotEmpty
? palette.colors.first ?? Colors.transparent
: Colors.transparent;
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: new Container(
child: new Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// dialog top
new Row(
children: <Widget>[
GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => IconShow(
icon: app.icon,
),
),
);
},
child: Container(
padding: const EdgeInsets.all(8.0),
child: app.icon,
),
),
new Container(
child: new Text(
app.label,
style: TextStyle(
color: Colors.black,
fontSize: 18.0,
),
),
),
],
),
Row(
children: <Widget>[
Container(
constraints: BoxConstraints.tight(Size(50, 50)),
padding: const EdgeInsets.all(8.0),
child: app.icon.foreground,
),
!(app.icon is AdaptableIcon)
? SizedBox()
: Container(
constraints: BoxConstraints.tight(Size(50, 50)),
padding: const EdgeInsets.all(8.0),
child: (app.icon as AdaptableIcon).background,
),
],
),
// dialog centre
new Container(
child: Text(app.packageName),
),
new Container(
child: Text("Version name: ${app.versionName}"),
),
new Container(
child: Text("Version code: ${app.versionCode}"),
),
new Container(
child: Text("Is app enabled? : $isEnabled"),
),
new Container(
child: Text("Does app exist?: $doesExist"),
),
// dialog bottom
Text("Dominant color"),
Container(
padding: EdgeInsets.all(8),
margin: EdgeInsets.all(8),
color: dominantColor,
height: 50,
width: 50,
),
RaisedButton(
onPressed: () {
LauncherHelper.launchApp(app.packageName);
},
child: Text("Launch"),
),
],
),
),
);
},
);
}
class IconShow extends StatefulWidget {
final AppIcon icon;
const IconShow({Key key, this.icon}) : super(key: key);
@override
_IconShowState createState() => _IconShowState();
}
class _IconShowState extends State<IconShow> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[400],
body: Center(
child: Container(
height: 100,
width: 100,
child: AppIconShape(
data: AppIconShapeData.square().copyWith(),
child: (widget.icon as AdaptableIcon).background,
),
),
),
);
}
}