부모위젯의 username 변수를 자식위젯 Dialog에서 이용하려했는데 자식위젯에서 부모위젯 변수를 인식을 못하네요
1. 보내기, 2. 등록하기, 3. 쓰기의 모든 방법을 다 지켰는데도 안돼서 여쭤봅니다
도와주새요 흑흑
*username 변수는 사용자가 모달창에 정보를 입력할 수 있도록 만든 변수입니다
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyApp()),
);
}
class MyApp extends StatefulWidget {
MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var name = ['김영숙', '홍길동', '피자집'];
var like = [0, 0, 0 ];
var username = '';
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(context: context, builder: (context) {
return DialogUI(state : username);
},
);
}),
appBar: AppBar(),
body: ListView.builder(
itemCount: 3,
itemBuilder: (c, i) {
return ListTile(
leading: Icon(Icons.person_pin),
title: Text(name[i]),
);
},
),
);
}
}
class DialogUI extends StatelessWidget {
DialogUI({Key? key, this.state}) : super(key: key);
final state;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('CANCLE'),
content: TextField(
onChanged: (value) {
username = value; // 인식 못하는 부분
},
),
actions: [
TextButton(onPressed: (){
Navigator.of(context).pop();
},
child: Text('CANCLE')),
TextButton(onPressed: (){
Navigator.of(context).pop();
},
child: Text('OK')),
],
);
}
}