import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import '../services/api_service.dart';
import '../style.dart';
import '../widgets/modal_progress_indicator.dart';
import 'menu.dart';
import 'package:flutter_advanced_networkimage/provider.dart';
import 'package:flutter_advanced_networkimage/transition.dart';
import 'package:flutter_advanced_networkimage/zoomable.dart';
import 'package:flutter/services.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
@override
State<StatefulWidget> createState() {
return new HomePageState();
}
}
class HomePageState extends State<HomePage> {
BuildContext _ctx;
final _scaffoldKey = new GlobalKey<ScaffoldState>();
bool _loading = false;
var apiRest = new ApiService();
Size screenSize;
var isPortrait = true;
double widthTile = 120;
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
String notificationMessage;
bool isShowNotification = false;
@override
void initState() {
super.initState();
initHomePageState();
}
static Future<dynamic> myBackgroundMessageHandler(
Map<String, dynamic> message) {
/*if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
print("myBackgroundMessageHandler $data");
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
print("myBackgroundMessageHandler $notification");
}*/
print("myBackgroundMessageHandler $message");
// Or do other work.
}
initHomePageState() async {
print("initHomePageState:");
//Firebase Message Configuração
_firebaseMessaging.autoInitEnabled().then((bool enabled) => print(enabled));
_firebaseMessaging.setAutoInitEnabled(true).then((f) {
_firebaseMessaging
.autoInitEnabled()
.then((bool enabled) => print(enabled));
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage teste: $message");
showNotification(message);
},
//onBackgroundMessage: Platform.isIOS ? myBackgroundMessageHandler : null,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
//Future.delayed(Duration(seconds: 1), () {
_firebaseMessaging.requestNotificationPermissions(IosNotificationSettings(
sound: true, badge: true, alert: true, provisional: true));
print("pedindo permisao");
//});
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("pediu permisao");
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
setState(() {
print("Push Messaging token: $token");
});
});
}
@override
Widget build(BuildContext context) {
_ctx = context;
final mediaQueryData = MediaQuery.of(context);
isPortrait = MediaQuery.of(context).orientation == Orientation.portrait;
screenSize = mediaQueryData.size;
widthTile =
isPortrait ? (screenSize.width / 2 - 20) : (screenSize.width / 3 - 20);
return Scaffold(
//appBar: AppBar(),
key: _scaffoldKey,
//backgroundColor: AppStyle.backgroundDark, //#3b4455
body: Stack(
children: _buildList(context),
),
drawer: MenuPrincipal(),
);
}
List<Widget> _buildList(BuildContext context) {
var listview = SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
//logo
Container(
width: double.infinity,
height: screenSize.height / 3.7,
child: Padding(
padding: EdgeInsets.fromLTRB(0, 25, 0, 10),
child: Image.asset('assets/logo.png'),
),
),
//menu
Container(
width: screenSize.width,
height: isPortrait ? 570 : 450,
child: Stack(
children: <Widget>[
Positioned(
left: isPortrait
? ((screenSize.width - widthTile * 2) / 2)
: ((screenSize.width - widthTile * 3) / 2),
top: 0,
child: Container(
width: isPortrait ? widthTile * 2 : widthTile * 3,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Color.fromRGBO(128, 251, 255, 0.52),
blurRadius: 10),
],
),
height:
isPortrait ? 500 : 800, //vertival=360 horizontal=240,
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(10)),
child: Container(
//padding: EdgeInsets.all(10),
color: Colors.white,
child: Wrap(
children: <Widget>[
tile(
route: "/agendas",
title: "PROGRAMAÇÃO",
iconUnicode: "\uEB0C"),
tile(
route: "/atracoes",
title: "ARTISTAS",
iconUnicode: "\uE851"),
tile(
route: "/hospedagem",
title: "ONDE FICAR",
iconUnicode: "\uE6FA"),
tile(
route: "/alimentacao",
title: "ONDE COMER",
iconUnicode: "\uEB84"),
tile(
route: "/noticia",
title: "NOTÍCIAS",
iconUnicode: "\ueafe"),
tile(
route: "/live",
title: "LIVE",
iconUnicode: "\uedd1"),
],
),
),
),
),
),
//wave
Positioned(
bottom: 0,
child: Container(
//color: Colors.red,
width: screenSize.width,
//color: Colors.amberAccent,
height: 220,
child: Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
child: Image.asset(
'assets/wave-bottom.png',
fit: BoxFit.fill,
),
),
),
),
//logo
Positioned(
left: isPortrait
? (screenSize.width - screenSize.width / 2) / 2
: (screenSize.width - screenSize.width / 3) / 2,
bottom: 30,
child: Container(
width: isPortrait
? screenSize.width / 2
: screenSize.width / 3,
child: Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
child: Image.asset(
'assets/logo-pmro-branco.png',
fit: BoxFit.fitHeight,
),
),
),
),
],
),
)
//
],
),
);
var l = new List<Widget>();
//background page
l.add(
Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/background.png'),
fit: BoxFit.cover,
),
),
),
);
l.add(listview);
//botão menu
l.add(Positioned(
left: 10,
top: 25,
child: Container(
decoration: BoxDecoration(
color: Color(0xAA26B4D6), //Colors.lightBlue[100]
shape: BoxShape.circle,
),
child: IconButton(
icon: Icon(
Icons.menu,
color: Colors.white,
),
onPressed: () => _scaffoldKey.currentState.openDrawer(),
),
)));
if (_loading) {
l.add(modalProgressIndicator());
}
if (isShowNotification) {
l.add(notificationDialog());
}
return l;
}
showNotification(Map<String, dynamic> message) {
if (message != null) {
if (message.containsKey('notification')) {
var not = message['notification'];
if (not != null) {
if (not.containsKey('body')) {
notificationMessage = not['body'];
}
}
}
}
if (notificationMessage != null) {
setState(() {
isShowNotification = true;
});
Future.delayed(Duration(seconds: 8), () {
setState(() {
isShowNotification = false;
});
});
}
}
notificationDialog() {
return Positioned(
left: 0,
right: 0,
bottom: 0,
child: Container(
color: Color.fromRGBO(20, 20, 20, 0.5),
child: Padding(
padding: EdgeInsets.fromLTRB(16, 10, 16, 10),
child: Text(
notificationMessage,
style: TextStyle(color: Colors.white),
),
)),
);
}
tile(
{Function onTap,
String title,
String route,
String iconUnicode = "\uEB0C"}) {
return InkWell(
onTap: onTap != null
? onTap
: () {
if (route != null) {
Navigator.of(context).pushNamed(route);
}
},
child: Container(
width: widthTile,
height: 120,
decoration: BoxDecoration(
border:
Border.all(color: Color.fromRGBO(15, 60, 225, 0.11), width: 0.5),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
child: Stack(
children: <Widget>[
Text(
iconUnicode,
style: TextStyle(
fontSize: 40,
fontFamily: "iconsmind",
color: Color(0xFF26B4D6),
),
)
],
),
),
Text(
title,
style: TextStyle(
fontSize: 15,
fontFamily: 'Roboto',
color: Color(0xFF26B4D6),
fontWeight: FontWeight.bold),
),
],
),
),
);
}
}
After adding the firebase message to my flutter project and implementing it as described in the pub.dev readme, this error occurs on Android and ios when I uncomment line 77 onBackgroundMessage: myBackgroundMessageHandler.
on Android the App closes unexpectedly on ios the app does not close so many more messages in the log.
Another thing is that on Android onMessage works correctly. on ios nothing happens, onMessage is not called.
logs
pubspec.yaml
yaml
home_page.dart
sample