본문 바로가기

Flutter-플러터/플러터 공부

getNoteList() 을 만들어서 obx위젯에서 사용하게 만들기!

반응형

 

 

네, 물론입니다. getNoteList() 위젯은 nc.notes 상태 변수의 변경 사항을 수신하고 노트 목록을 표시하는 책임이 있습니다. 이 위젯은 Obx 위젯을 사용하여 nc.notes 상태 변수의 변경 사항을 수신합니다. Obx 위젯은 Stream에서 데이터를 수신하고 데이터가 변경될 때마다 위젯을 다시 빌드합니다.

getNoteList() 위젯은 ListView.builder() 위젯을 사용하여 노트 목록을 표시합니다. ListView.builder() 위젯은 nc.notes 목록을 반복하고 각 노트에 대한 Card 위젯을 생성합니다. 각 Card 위젯에는 ListTile 위젯이 포함되어 노트의 제목, 인덱스 및 사용자가 노트를 편집하거나 삭제할 수 있는 버튼 세트를 표시합니다.

다음은 getNoteList() 위젯의 구현입니다.

Widget getNoteList() {
  return Obx(
    () => nc.notes.isEmpty
        ? Center(child: Text('없음'))
        : ListView.builder(
            itemCount: nc.notes.length,
            itemBuilder: (context, index) => Card(
              child: ListTile(
                title: Text(nc.notes[index].title,
                    style: TextStyle(fontWeight: FontWeight.w500)),
                leading: Text(
                  (index + 1).toString() + ".",
                  style: TextStyle(fontSize: 15),
                ),
                trailing: Wrap(children: <Widget>[
                  IconButton(
                    icon: Icon(Icons.create),
                    onPressed: () => Get.to(Mynote(index: index)),
                  ),
                  IconButton(
                    icon: Icon(Icons.delete),
                    onPressed: () {
                      Get.defaultDialog(
                        title: 'Delete Note',
                        middleText: nc.notes[index].title,
                        onCancel: () => Get.back(),
                        confirmTextColor: Colors.white,
                        onConfirm: () {
                          nc.notes.removeAt(index);
                          Get.back();
                        },
                      );
                    },
                  )
                ]),
              ),
            )),
  );
}
반응형

'Flutter-플러터 > 플러터 공부' 카테고리의 다른 글

null 체크 late와 ? 의 차이  (0) 2023.06.14
네이버 개발자가 작성한 Flutter 상태관리지 비교분석  (0) 2023.06.13
Getx obx오류  (0) 2023.05.30
Getx bindings  (0) 2023.05.21
플러터 전용 게임키트  (0) 2023.05.15