import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'dart:convert'; //import 'messages_data.dart'; import '../../../components/doJSON.dart'; import '../../../components/hyxx_data_handle.dart'; class listMessagesData { List listMessages = []; int listIndex = 0; } class MessagesView extends StatefulWidget { //MessagesView({Key key, this.title, this.mapData}) : super(key: key); MessagesView({ @required this.title, this.listIndex, Key key, }) : super(key: key); String title; int listIndex = 0; List listMessages; Map mapListMessagesData; _LoginPageState createState() => _LoginPageState(); } class _LoginPageState extends State { @override void initState() { // TODO: implement initState super.initState(); setInit(); } String strContent = ''; setInit() async { widget.mapListMessagesData = Map(); widget.mapListMessagesData['收到的消息'] = listMessagesData(); widget.mapListMessagesData['发送的消息'] = listMessagesData(); widget.mapListMessagesData['收到的消息'].listMessages = listMessagesInbox2; widget.mapListMessagesData['发送的消息'].listMessages = listMessagesOutbox2; selectValue = widget.title; switch (selectValue) { case '收到的消息': //第一次调用时widget.listIndex是由外面传入的,不能覆盖widget.listIndex, // 所以单独抽离出一个不会覆盖widget.listIndex的函数 onRadioBtnInboxSet(); break; case '发送的消息': onRadioBtnOutboxSet(); break; } widget.listMessages = widget.mapListMessagesData[selectValue].listMessages; getContent(); } getPreBtn_NextBtn() { preBtn = getBtnSizeX( text: "上一条", onPressedFun: null, ); nextBtn = getBtnSizeX( text: "下一条", onPressedFun: null, ); if (widget.listIndex > 0 && widget.listMessages.length > 0) { preBtn = getBtnSizeX( text: "上一条", onPressedFun: () async { if (widget.listIndex > 0) { widget.listIndex--; getContent(); } }, ); } if (widget.listIndex < (widget.listMessages.length - 1) && widget.listMessages.length > 0) { nextBtn = getBtnSizeX( text: "下一条", onPressedFun: () async { if (widget.listIndex < widget.listMessages.length - 1) { widget.listIndex++; getContent(); } }, ); } } getContent() async { if (widget.listMessages.isEmpty) { strContent = ''; widget.mapListMessagesData[selectValue].listIndex = widget.listIndex = 0; } else { widget.mapListMessagesData[selectValue].listIndex = widget.listIndex; strContent = "第 ${widget.listIndex + 1} 条(共 ${widget.listMessages.length} 条)" + "\n" + "时间:" + widget.listMessages[widget.listIndex]['date'] + ", " + widget.listMessages[widget.listIndex]['time'] + "\n\n" + "内容:" + widget.listMessages[widget.listIndex]['content']; } getPreBtn_NextBtn(); setState(() {}); } var selectValue; //解决第一次进入报错问题。因为getPreBtn_NextBtn()还未执行,preBtn和nextBtn为空 Widget preBtn = Container( color: Colors.white12, //onPressedFun为null时无效 width: 70.0, height: 35.0, child: RaisedButton( padding: EdgeInsets.all(0), textColor: Colors.black, child: Text('上一条'), onPressed: null, ), ); Widget nextBtn = Container( color: Colors.white12, //onPressedFun为null时无效 width: 70.0, height: 35.0, child: RaisedButton( padding: EdgeInsets.all(0), textColor: Colors.black, child: Text('下一条'), onPressed: null, ), ); Widget getBtnSizeColor( {@required title, width = 70.0, height = 35.0, //colorBK = Colors.white12, txtColor = Colors.black, fontSize = 16.0, bottomBorder = false, onPressedFun}) { return Container( //Failed assertion: line 285 pos 15: 'color == null || decoration == null': // Cannot provide both a color and a decoration //color: colorBK, //onPressedFun为null时无效 width: width, height: height, decoration: BoxDecoration( border: bottomBorder ? Border(bottom: BorderSide(width: 1, color: Colors.blue)) : null, ), child: FlatButton( padding: EdgeInsets.all(0), textColor: txtColor, child: Text(title, style: TextStyle(fontSize: fontSize)), onPressed: onPressedFun, ), ); } Widget radioBtnInbox; Widget radioBtnOutbox; //第一次调用时widget.listIndex是由外面传入的,不能覆盖widget.listIndex, // 所以单独抽离出一个不会覆盖widget.listIndex的函数 onRadioBtnInboxSet() { radioBtnInbox = getBtnSizeColor( title: '收到的消息', width: 100.0, onPressedFun: onRadioBtnInbox, //colorBK: Colors.white, txtColor: Colors.blue, bottomBorder: true); radioBtnOutbox = getBtnSizeColor( title: '发送的消息', width: 100.0, onPressedFun: onRadioBtnOutbox, txtColor: Colors.black38); } onRadioBtnInbox() { setState(() { selectValue = '收到的消息'; onRadioBtnInboxSet(); widget.listMessages = widget.mapListMessagesData[selectValue].listMessages; widget.listIndex = widget.mapListMessagesData[selectValue].listIndex; getContent(); }); } onRadioBtnOutboxSet() { radioBtnInbox = getBtnSizeColor( title: '收到的消息', width: 100.0, onPressedFun: onRadioBtnInbox, txtColor: Colors.black38); radioBtnOutbox = getBtnSizeColor( title: '发送的消息', width: 100.0, onPressedFun: onRadioBtnOutbox, //colorBK: Colors.white, txtColor: Colors.blue, bottomBorder: true); } onRadioBtnOutbox() { setState(() { selectValue = '发送的消息'; onRadioBtnOutboxSet(); widget.listMessages = widget.mapListMessagesData[selectValue].listMessages; widget.listIndex = widget.mapListMessagesData[selectValue].listIndex; getContent(); }); } Future alertDialog(String title, String content, var onPresse) async { return await showDialog( barrierDismissible: false, //表示点击灰色背景的时候是否消失弹出框 context: context, builder: (context) { return AlertDialog( title: Text(title), content: Text(content), actions: [ FlatButton( child: Text("确定"), onPressed: onPresse, ), FlatButton( child: Text("取消"), onPressed: () { Navigator.pop(context, false); }, ), ], ); }, ); } @override Widget build(BuildContext context) { Size mediaSize = MediaQuery.of(context).size; return Scaffold( appBar: AppBar( leading: IconButton( icon: const Icon(Icons.arrow_back), onPressed: () { //SystemNavigator.pop(); //退出App Navigator.pop(context); //返回 }, ), centerTitle: true, elevation: 0, backgroundColor: Colors.white, title: Center( child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, // 主轴元素的排序方式(水平布局中,X轴是主轴) crossAxisAlignment: CrossAxisAlignment.end, // 次轴元素的排序方式 children: [ radioBtnInbox, radioBtnOutbox, ], ), ), actions: [ IconButton( icon: Icon(Icons.close), onPressed: () async { Navigator.pop(context); //关闭弹框,播放输入视频地址 }, ), SizedBox( width: 10, ), ], ), body: Container( alignment: Alignment(0, 0), child: Container( child: Column( children: [ Container( padding: EdgeInsets.fromLTRB(20, 10, 20, 10), width: double.infinity, height: mediaSize.height * 0.75, child: SingleChildScrollView( child: Container( child: Text(strContent), ), ), ), Divider( color: Colors.blue, ), SizedBox( height: 6, ), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ getBtnSizeX( text: "复制", onPressedFun: () { // Flutter 复制文本到剪贴板 Clipboard.setData(ClipboardData( text: ' ' + selectValue + '\n\n' + widget.listMessages[widget.listIndex]['content'])); //showToast('帮助信息已复制到剪贴板', textAlign: TextAlign.left); Fluttertoast.showToast(msg: '帮助信息已复制到剪贴板', gravity: ToastGravity.CENTER); //Navigator.pop(context, ret); }), getBtnSizeX( text: "删除", onPressedFun: () async { //Navigator.pop(context); //关闭弹框,播放输入视频地址 bool ret = await alertDialog('删除确认', '是否确定要删除当前项目?', () { if (0 == selectValue.compareTo('收到的消息')) { listMessagesInbox2.removeAt(widget.listIndex); widget.mapListMessagesData['收到的消息'].listMessages = listMessagesInbox2; writeJSON(json.encode(listMessagesInbox2), 'listMessagesInbox02.json'); } else if (0 == selectValue.compareTo('发送的消息')) { listMessagesOutbox2.removeAt(widget.listIndex); widget.mapListMessagesData['发送的消息'].listMessages = listMessagesOutbox2; writeJSON( json.encode(listMessagesOutbox2), 'listMessagesOutbox02.json'); } if (widget.listIndex > 0) { widget.listIndex--; } widget.listMessages = widget.mapListMessagesData[selectValue].listMessages; getContent(); Navigator.pop(context); }); }), preBtn, nextBtn, ], ), ], ), ), ), ); } Widget getBtnSizeX({@required text, width = 70.0, height = 35.0, onPressedFun}) { return Container( color: Colors.white12, //onPressedFun为null时无效 width: width, height: height, child: RaisedButton( padding: EdgeInsets.all(0), textColor: Colors.black, child: Text(text), onPressed: onPressedFun, ), ); } }