import 'package:flutter/material.dart'; import '../../../widget/JdButton.dart'; import '../../../services/EventBus.dart'; import '../01_messages/messages_manage.dart'; import '../../../components/doJSON.dart'; import '../../../components/hyxx_data_handle.dart'; import 'dart:convert'; import '../../../res/listContacts.dart'; class ContactAdd extends StatefulWidget { ContactAdd({@required this.contactIndex, Key key}) : super(key: key); int contactIndex; _LoginPageState createState() => _LoginPageState(); } class _LoginPageState extends State { //List listController; bool changed = false; void initState() { // TODO: implement initState super.initState(); getListFlields(); } getListFlields() { //在listContacts2末尾添加一条记录,这种方式会导致添加后,同时修改被拷贝的记录 // listContacts2.add(listContacts2[widget.contactIndex]); // widget.contactIndex = listContacts2.length - 1; // // listController = List.generate(listContacts2[widget.contactIndex].length, (index) { // String key = listContacts2[widget.contactIndex].keys.elementAt(index); // listContacts2[widget.contactIndex][key] = ''; //清空内容 // return TextEditingController(); // }); //在listContacts2末尾添加一条记录 //添加硬数据方式不好,若listContacts2的字段修改后,便可能导致问题 // var item = { // "姓名": '张三', // "登录名称": 'ZhangSan', // "登录密码": '**********', // "部门": '办公室', // "职务": '主任', // "手机": '133xxxxxxxx', // "办公电话": '0831xxxxxxx', // "邮箱": '1234@qq.com', // "权限": '管理员', // "备注": '示例用户', // }; // //map遍历 // //usrMap.forEach((k,v) => print('${k}: ${v}')); // item.forEach((key, value) { // item[key] = ''; //清空内容 // }); //在listContacts2末尾添加一条记录,动态生成item中的元素 Map item = {}; listContacts2[widget.contactIndex].forEach((key, value) { item[key] = ''; //为item添加元素 }); listContacts2.add(item); widget.contactIndex = listContacts2.length - 1; //setState(() {}); } //监听登录页面销毁的事件 dispose() { super.dispose(); if (changed) { print("writeJSON()"); writeJSON(json.encode(listContacts2), 'listContacts02.json'); } else { print("removeLast()"); listContacts2.removeLast(); //删除添加的末尾元素 } eventBus.fire(new UserEvent('登录成功...')); } doLogin() async { Navigator.pop(context); //返回 return; } OnTap_messages_manage() { Navigator.of(context).push(MaterialPageRoute(builder: (context) => MessagesManagePage())); } //自定义方法 static onNullFun() {} Widget getTrail(String key, int index, double widthTrail) { return Container( alignment: Alignment(1, 0), //widthTrail = 400报错,360刚能显示,300换行,260 width: widthTrail, child: TextField( //textAlign: TextAlign.right, style: TextStyle(fontSize: 16), decoration: InputDecoration( hintText: '請輸入字段信息', //border: InputBorder.none, //TextField去掉下划线 contentPadding: EdgeInsets.only(right: 0), ), //controller: listController[index], //利用控制器初始化文本 onChanged: (value) { listContacts2[widget.contactIndex][key] = value; bFlash = changed = true; print("ContactAdd bFlash = $bFlash"); }, ), ); } Widget _getListTile(String key, int index, double widthTrail, {onTapFun = onNullFun, onLongPressFun = onNullFun, size = 16.0}) { return ListTile( //leading: new Icon(Icons.phone), title: Text('${mapUserInfoText[key]} :', style: TextStyle(fontSize: 16)), trailing: getTrail(key, index, widthTrail), contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 0), enabled: true, onTap: () {}, onLongPress: () {}, ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("修改联系人"), centerTitle: true, ), body: Container( child: Column( children: [ Expanded( //Flutter Column套ListView不显示,可将ListView用Expanded包裹起来。 // child: ListView.builder( // itemCount: listFlields.length, // itemBuilder: _getListTileFields, // ), //https://www.it1352.com/2028416.html //用Map而不是List的Flutter ListView(Flutter listview with Map instead of List) //listContacts2[widget.contactIndex] child: ListView.builder( itemCount: listContacts2[widget.contactIndex].length, itemBuilder: (BuildContext context, index) { String key = listContacts2[widget.contactIndex].keys.elementAt(index); return Column( children: [ _getListTile(key, index, 220.0), Divider( height: 1.0, ), ], ); }, ), ), ], ), ), ); } //https://www.it1352.com/2028416.html //用Map而不是List的ListView.builder //Its a little late but You could also try this. Map values = snapshot.data; getMap() { Map values = listContacts2[widget.contactIndex]; return new ListView.builder( itemCount: values.length, itemBuilder: (BuildContext context, int index) { String key = values.keys.elementAt(index); return new Column( children: [ new ListTile( title: new Text("$key"), subtitle: new Text("${values[key]}"), ), new Divider( height: 2.0, ), ], ); }, ); } }