2 글 보임 - 1 에서 2 까지 (총 2 중에서)
-
글쓴이글
-
2023년 1월 16일 17:52 #62929
김윤재참가자import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:contacts_service/contacts_service.dart';
void main() { runApp(MaterialApp( home: MyApp(), )); }
class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key);
@override State<MyApp> createState() => _MyAppState(); }
class _MyAppState extends State<MyApp> {
getPermission() async { var status = await Permission.contacts.status; if (status.isGranted) { print('허락됨'); var contacts = await ContactsService.getContacts(); setState(() { name=contacts; });
} else if (status.isDenied) { print('거절됨'); Permission.contacts.request(); } }
var name=[];
addName(a){ setState(() { name.add(a); }); }
@override Widget build(BuildContext context) { return Scaffold( floatingActionButton: FloatingActionButton( child: Text('button'), onPressed: (){ showDialog(context: context, builder: (context){ return DialogUI(addName:addName); }); }, ), appBar: AppBar( title: Text('연락처',style: TextStyle(fontSize: 30)),actions: [ IconButton(onPressed: (){ getPermission(); }, icon: Icon(Icons.contacts)) ], ), body: ListView.builder( itemCount: name.length, itemBuilder: (c,i){ return ListTile( leading: Text((i+1).toString()), title: Text(name[i].displayName ?? '이름없음'), trailing: ElevatedButton( child: Text('삭제'), onPressed: (){ setState(() { name.removeAt(i); }); }, ) ); }), ); } }
class DialogUI extends StatelessWidget { DialogUI({Key? key,this.addName}) : super(key: key); final addName; var inputData=TextEditingController(); @override
Widget build(BuildContext context) { return Dialog( child: SizedBox( width: 300, height: 300, child: Column( children: [ TextField(controller: inputData), TextButton(onPressed: (){ if(inputData.text=='') { Navigator.pop(context); } else { Navigator.pop(context); var newPerson = Contact(); newPerson.givenName = inputData.text; ContactsService.addContact(newPerson); addName(newPerson); } }, child: Text('완료')), TextButton(onPressed: (){ Navigator.pop(context); }, child: Text('취소')) ], ), ), ); } } 이코드에서 appbar에있는 연락처불러오기 버튼 눌렀을때 전화번호 저장이 되어있어도 1번은 무조건 null이 나오는데 왜이런건가요 ??
-
글쓴이글
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
- 답변은 로그인 후 가능합니다.