lpush 0.0.1 lpush: ^0.0.1 copied to clipboard
A new flutter plugin project.
import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:lpush/lpush.dart';
import 'package:lpush/lpush_notify_message.dart';
import 'package:lpush/lpush_local_notification.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: MainApp(),
);
}
}
class MainApp extends StatefulWidget {
@override
_MainAppState createState() {
return _MainAppState();
}
}
class _MainAppState extends State<MainApp> {
@override
void initState() {
super.initState();
Lpush.setCustomNotification();
Lpush.addPushReceiver(_onEvent, _onError);
}
void _onEvent(Object event){
print('>>>>>>>>>>>>>onEvent:' + event.toString());
setState(() {
Map<String, dynamic> eventMap =json.decode(event);
Map<String, dynamic> result = eventMap['result'];
int action = eventMap['action'];
switch (action){
case 0:
LPushNotifyMessage message = new LPushNotifyMessage.fromJson(result);
showDialog(
context: context,
builder: (context){
return AlertDialog(
content: Text(message.content),
actions: <Widget>[
new FlatButton(
child: new Text("收到回调"),
onPressed: (){
Navigator.pop(context);
},
),
],
);
});
break;
case 1:
LPushNotifyMessage message = new LPushNotifyMessage.fromJson(result);
showDialog(
context: context,
builder: (context){
return AlertDialog(
content: Text(message.content),
actions: <Widget>[
new FlatButton(
child: new Text("被点击"),
onPressed: (){
Navigator.pop(context);
},
),
],
);
});
break;
}
});
}
void _onError(Object event){
setState(() {
print('>>>>>>>>>>onError:' + event.toString());
});
}
void localNotification(BuildContext context){
LPushLocalNotification localNotification = new LPushLocalNotification(
title: "正标题",//本地通知标题
content: "通知内容", //本地通知内容
timestamp: 0, //本地通知时间戳, 0立刻通知。其余为时间间隔
badge: 1, // ios角标
sound: "default", // ios声音
subTitle: "副标题",
);// ios副标题
Lpush.addLocalNotification(localNotification);
}
void setBadge(BuildContext context){
Lpush.setBadge(8);
}
void clearBadge(BuildContext context){
Lpush.clearBadge();
}
Widget _creatRow(String methodName, String methodDes, Function method,
BuildContext context) {
return new GestureDetector(
onTap: () {
method(context);
},
child: new Container(
padding: const EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
width: MediaQuery.of(context).size.width,
color: Colors.grey.withAlpha(10),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
// padding: const EdgeInsets.only(bottom: 2.0),
children: [
new Container(
padding: const EdgeInsets.only(bottom: 2.0),
child: new Text(
methodName,
style: new TextStyle(
fontWeight: FontWeight.bold,
),
),
),
new Text(
methodDes,
style: new TextStyle(
color: Colors.grey[500],
),
),
new Container(
padding: const EdgeInsets.only(top: 5.0),
child: new Container(
padding: const EdgeInsets.only(top: 0.33),
color: Colors.grey,
),
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new ListView(
padding: EdgeInsets.fromLTRB(0.0, 30.0, 0.0, 0.0),
children: <Widget>[
_creatRow("添加本地推送", "测试本地推送", localNotification, context),
_creatRow("设备角标", "添加角标", setBadge, context),
_creatRow("清空角标", "清空角标", clearBadge, context),
],
),
);
}
}