2 글 보임 - 1 에서 2 까지 (총 2 중에서)
-
글쓴이글
-
2024년 11월 12일 13:34 #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); },
), ], )
], ), ), ); } }
-
글쓴이글
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
- 답변은 로그인 후 가능합니다.