• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Flutter 게시판 자식이 부모 state값 바꾸는 것 질문

자식이 부모 state값 바꾸는 것 질문

  • 이 주제에는 1개 답변, 1명 참여가 있으며 익명8 월, 3 주 전에 전에 마지막으로 업데이트했습니다.
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #132237

    익명
    비활성
    FloatingActionButton 누르고 '확인' 버튼을 눌러야 부모 state의 값이 +1 되는 게 정상인데요
    
    FloatingActionButton 버튼을 누름과 동시에 부모 state 값이 올라가네요 
    
    이유가 뭘까요?
    
    
    
    --------코드--------
    
    
    import 'package:flutter/material.dart';
    void main() {
      runApp(const MaterialApp(
          home: MyApp()
      )
      );
    }
    class MyApp extends StatefulWidget {
      const MyApp({super.key});
      @override
      State<MyApp> createState() => _MyAppState();
    }
    class _MyAppState extends State<MyApp> {
      var name = ['이모씨', '김모씨', '박모씨'];
      var a = 3;
      var total = 1;
      addTotal(){
        setState(() {
          total++;
        });
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
              floatingActionButton: FloatingActionButton(
                child: Text('버튼'),
                onPressed: (){
                  return showAboutDialog(
                    context: context,
                    children: [
                      DialogUI(total:total,addTotal:addTotal())
                    ]
                  );
                }
              ),
                appBar: AppBar(title: Text(total.toString()),),
                bottomNavigationBar: BottomAppBar(),
                body: ListView.builder(
                    itemCount: 3,
                    itemBuilder: (c,i){
                      return ListTile(
                        title: Text(name[i])
                      );
                    },
                ),
            );
      }
    }
    class DialogUI extends StatelessWidget {
      const DialogUI({super.key, this.total, this.addTotal});
      final total;
      final addTotal;
      @override
      Widget build(BuildContext context) {
        return Dialog(
          child: SizedBox(
            width: 300,
            height: 300,
            child: Column(
              children: [
                TextField(),
                Row(
                  children: [
                    TextButton(
                      child: Text('확인'),
                      onPressed: (){
                        addTotal();
                      },
                    ),
                    TextButton(
                      child: Text('취소'),
                      onPressed: (){
                        Navigator.pop(context);
                      },
                    ),
                  ],
                )
              ],
            ),
          ),
        );
      }
    }
     
    
    
    
    화면 캡처 2024-11-12 133338
    
    
    
    #132241

    익명
    비활성
    해결했습니다
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관, 개인정보처리방침
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠