hyzp_ybqx-Commit125:已使用flutter_background_service 0.0.1+21插件,初步实现后台提醒
This commit is contained in:
@@ -17,6 +17,8 @@ import '../../services/ServiceLocator.dart';
|
||||
import '../../services/Storage.dart';
|
||||
import 'page1_work.dart';
|
||||
// import 'package:audioplayers/audio_cache.dart';
|
||||
import 'package:flutter_background_service/flutter_background_service.dart';
|
||||
import 'package:hyzp_ybqx/pages/tabs/flutter_background_service_my.dart';
|
||||
|
||||
class Tabs extends StatefulWidget {
|
||||
Tabs({Key key, this.arguments = 0}) : super(key: key);
|
||||
@@ -65,6 +67,7 @@ class _TabsState extends State<Tabs> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
getlistItems().then((value) => try_setState());
|
||||
print('widget.arguments = ${widget.arguments}');
|
||||
//_currentIndex = 3 == widget.arguments ? 0 : widget.arguments; //解决"我的"页面根据用户所属组及时刷新问题
|
||||
@@ -85,47 +88,17 @@ class _TabsState extends State<Tabs> {
|
||||
}
|
||||
|
||||
init() async {
|
||||
// // 从磁盘读取变量
|
||||
// g_bVoiceRemind = await Storage.getBool('bVoiceRemind');
|
||||
// g_bVoiceRemind = (null == g_bVoiceRemind) ? false : g_bVoiceRemind;
|
||||
// // print('g_bVoiceRemind = $g_bVoiceRemind');
|
||||
// eventBus.fire(VoiceRemindUpdate('g_bVoiceRemind 数据已更新'));
|
||||
|
||||
// 执行语音提醒
|
||||
doRemind();
|
||||
|
||||
// doRemind();
|
||||
//设置定时提醒,循环执行定时任务,间隔 g_remindGap 秒
|
||||
setRemindTimer();
|
||||
// setRemindTimer();
|
||||
|
||||
// g_remindTimer = Timer.periodic(Duration(seconds: g_remindGap), (timer) {
|
||||
// ///定时任务
|
||||
// doRemind();
|
||||
// });
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
FlutterBackgroundService.initialize(onStart);
|
||||
}
|
||||
|
||||
// // 执行语音提醒
|
||||
// Future doRemind() async {
|
||||
// // 获取待审核黑烟车记录
|
||||
// listReviewed.clear();
|
||||
// print("listReviewed = $listReviewed");
|
||||
// await getReviewedList(); //注意:访问区县后台接口,需要统一添加区县代码参数
|
||||
// print("listReviewed = $listReviewed");
|
||||
// if (g_bVoiceRemind && listReviewed.length > 0) {
|
||||
// // if (g_bVoiceRemind) {
|
||||
// // import 'dart:io';
|
||||
// // import 'package:audioplayers/audio_cache.dart';
|
||||
// // print("播放:语音提醒.mp3");
|
||||
// AudioCache().play(File('audio/语音提醒.mp3').path); //语音提醒
|
||||
// }
|
||||
// }
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// 取消定时任务
|
||||
if (null != g_remindTimer) {
|
||||
g_remindTimer.cancel(); //取消计时器
|
||||
}
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import 'package:hyzp_ybqx/services/Storage.dart';
|
||||
|
||||
import '../../components/commonFun.dart';
|
||||
import '../../widget/JdButton.dart';
|
||||
import 'package:flutter_background_service/flutter_background_service.dart';
|
||||
import 'package:hyzp_ybqx/pages/tabs/flutter_background_service_my.dart';
|
||||
|
||||
class page7_setRemind extends StatefulWidget {
|
||||
page7_setRemind({Key key}) : super(key: key);
|
||||
@@ -19,6 +21,9 @@ class page7_setRemind extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _LoginPageState extends State<page7_setRemind> {
|
||||
bool bVoiceRemind;
|
||||
int remindGap;
|
||||
|
||||
dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
@@ -27,16 +32,7 @@ class _LoginPageState extends State<page7_setRemind> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
bVoiceRemind = g_bVoiceRemind;
|
||||
|
||||
getListView();
|
||||
|
||||
// //监听 g_bVoiceRemind 更新事件
|
||||
// eventBus.on<VoiceRemindUpdate>().listen((event) async {
|
||||
// print(event.str + ", g_bVoiceRemind = $g_bVoiceRemind");
|
||||
// setState(() {
|
||||
// getListView();
|
||||
// });
|
||||
// });
|
||||
remindGap = g_remindGap;
|
||||
}
|
||||
|
||||
////////
|
||||
@@ -106,85 +102,15 @@ class _LoginPageState extends State<page7_setRemind> {
|
||||
setState(() {
|
||||
bVoiceRemind = !bVoiceRemind;
|
||||
print("bVoiceRemind = $bVoiceRemind");
|
||||
// Storage.setBool('bVoiceRemind', bVoiceRemind);
|
||||
print("播放语音提醒");
|
||||
if (bVoiceRemind) {
|
||||
print("播放语音提醒");
|
||||
// AudioCache().play(File('audio/语音提醒.mp3').path); //语音提醒
|
||||
myPlayVoiceRemind();
|
||||
} else {
|
||||
myPlayClick();
|
||||
}
|
||||
getListView();
|
||||
});
|
||||
}
|
||||
|
||||
bool bVoiceRemind;
|
||||
|
||||
getListView() {
|
||||
_listViewUser.clear();
|
||||
_listViewUser = [
|
||||
Center(
|
||||
child: Container(
|
||||
margin: EdgeInsets.only(top: 30),
|
||||
height: ScreenUtil().setWidth(160),
|
||||
width: ScreenUtil().setWidth(160),
|
||||
//child: Image.asset('assets/images/user.png', fit: BoxFit.cover),
|
||||
child: Image.asset('assets/images/ybsthbj.png', fit: BoxFit.fitHeight),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 50),
|
||||
_getListTile('待审提醒',
|
||||
leadPath: 'assets/images/语音提醒.png',
|
||||
leadColor: _deepGreyColor,
|
||||
textColor: bVoiceRemind ? Colors.blue : null,
|
||||
onTapFun: OnTap_Remind,
|
||||
trailWidget: bVoiceRemind
|
||||
? Icon(Icons.check_box, color: Colors.blue)
|
||||
: Icon(Icons.check_box_outline_blank, color: Colors.blueAccent)),
|
||||
_getListTile('提醒间隔(秒)',
|
||||
leadPath: 'assets/images/时间间隔.png',
|
||||
leadColor: _deepBlueColor,
|
||||
trailWidget: getTextField()),
|
||||
SizedBox(height: 60),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
JdButton(
|
||||
height: 128,
|
||||
width: 282,
|
||||
//height: 126,
|
||||
text: "确认",
|
||||
color: Colors.blueAccent,
|
||||
onTop: () {
|
||||
g_bVoiceRemind = bVoiceRemind;
|
||||
print("g_bVoiceRemind = $g_bVoiceRemind");
|
||||
Storage.setBool('bVoiceRemind', g_bVoiceRemind);
|
||||
|
||||
g_remindGap = _gap;
|
||||
print("g_remindGap = $g_remindGap");
|
||||
Storage.setInt('nRemindGap', g_remindGap);
|
||||
setRemindTimer(enable: g_bVoiceRemind);
|
||||
|
||||
// AudioCache().play(File('audio/yinxiao1064.mp3').path); //音效提醒
|
||||
myPlayClacks(); //音效提醒
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
JdButton(
|
||||
height: 128,
|
||||
width: 282,
|
||||
//height: 126,
|
||||
text: "取消",
|
||||
color: Colors.blueAccent,
|
||||
onTop: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
int _gap = g_remindGap;
|
||||
|
||||
Widget getTextField() {
|
||||
return Container(
|
||||
width: ScreenUtil().setWidth(150),
|
||||
@@ -193,10 +119,10 @@ class _LoginPageState extends State<page7_setRemind> {
|
||||
focusNode: FocusNode(),
|
||||
textAlignVertical: TextAlignVertical(y: 1.0),
|
||||
controller: TextEditingController.fromValue(TextEditingValue(
|
||||
text: _gap.toString(),
|
||||
text: remindGap.toString(),
|
||||
// 保持光标在最后
|
||||
selection: TextSelection.fromPosition(
|
||||
TextPosition(affinity: TextAffinity.downstream, offset: _gap.toString().length)))),
|
||||
selection: TextSelection.fromPosition(TextPosition(
|
||||
affinity: TextAffinity.downstream, offset: remindGap.toString().length)))),
|
||||
maxLines: 1,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: InputDecoration(
|
||||
@@ -209,7 +135,7 @@ class _LoginPageState extends State<page7_setRemind> {
|
||||
enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.lightBlue)),
|
||||
),
|
||||
onChanged: (v) {
|
||||
_gap = int.parse(v);
|
||||
remindGap = int.parse(v);
|
||||
},
|
||||
),
|
||||
);
|
||||
@@ -279,7 +205,83 @@ class _LoginPageState extends State<page7_setRemind> {
|
||||
body: Container(
|
||||
padding: EdgeInsets.only(top: 30, bottom: 20, left: 20, right: 20),
|
||||
child: ListView(
|
||||
children: _listViewUser,
|
||||
children: [
|
||||
Center(
|
||||
child: Container(
|
||||
margin: EdgeInsets.only(top: 30),
|
||||
height: ScreenUtil().setWidth(160),
|
||||
width: ScreenUtil().setWidth(160),
|
||||
//child: Image.asset('assets/images/user.png', fit: BoxFit.cover),
|
||||
child: Image.asset('assets/images/ybsthbj.png', fit: BoxFit.fitHeight),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 50),
|
||||
_getListTile('待审提醒',
|
||||
leadPath: 'assets/images/语音提醒.png',
|
||||
leadColor: _deepGreyColor,
|
||||
textColor: bVoiceRemind ? Colors.blue : null,
|
||||
onTapFun: OnTap_Remind,
|
||||
trailWidget: bVoiceRemind
|
||||
? Icon(Icons.check_box, color: Colors.blue)
|
||||
: Icon(Icons.check_box_outline_blank, color: Colors.blueAccent)),
|
||||
_getListTile('提醒间隔(秒)',
|
||||
leadPath: 'assets/images/时间间隔.png',
|
||||
leadColor: _deepBlueColor,
|
||||
trailWidget: getTextField()),
|
||||
SizedBox(height: 60),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
JdButton(
|
||||
height: 128,
|
||||
width: 282,
|
||||
//height: 126,
|
||||
text: "确认",
|
||||
color: Colors.blueAccent,
|
||||
onTop: () async {
|
||||
if (g_bVoiceRemind != bVoiceRemind && g_remindGap != remindGap) {
|
||||
if (await FlutterBackgroundService().isServiceRunning()) {
|
||||
FlutterBackgroundService().sendData({"action": "stopService"});
|
||||
}
|
||||
|
||||
g_bVoiceRemind = bVoiceRemind;
|
||||
print("g_bVoiceRemind = $g_bVoiceRemind");
|
||||
await Storage.setBool('bVoiceRemind', g_bVoiceRemind);
|
||||
|
||||
g_remindGap = remindGap;
|
||||
print("g_remindGap = $g_remindGap");
|
||||
await Storage.setInt('nRemindGap', g_remindGap);
|
||||
|
||||
if (bVoiceRemind) {
|
||||
while (true) {
|
||||
var isRunning = await FlutterBackgroundService().isServiceRunning();
|
||||
if (!isRunning) {
|
||||
await FlutterBackgroundService.initialize(onStart);
|
||||
myPlayVoiceRemind();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
myPlayClick();
|
||||
}
|
||||
}
|
||||
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
JdButton(
|
||||
height: 128,
|
||||
width: 282,
|
||||
//height: 126,
|
||||
text: "取消",
|
||||
color: Colors.blueAccent,
|
||||
onTop: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user