-
statelees / stateful 일때 다음화면에 데이터 전달하기프로그래밍 공부 메모/flutter 2022. 6. 16. 19:11
1. statelees 화면으로 데이터 전달
Navigator.push( context, MaterialPageRoute( builder: (context) => DetailScreen(todo: todos[index]), ),
class DetailScreen extends StatelessWidget { // Todo를 들고 있을 필드를 선언합니다. final Todo todo; // 생성자는 Todo를 인자로 받습니다. DetailScreen({Key key, @required this.todo}) : super(key: key); @override Widget build(BuildContext context) { // UI를 그리기 위해 Todo를 사용합니다. return Scaffold( appBar: AppBar( title: Text(todo.title), ), body: Padding( padding: EdgeInsets.all(16.0), child: Text(todo.description), ), ); } }
2. stateful 화면에 데이터 전달
Stateful 위젯은 두개의 클래스로 구성된다
이전 화면에서 넘겨받은 데이터는 MyRecord 클래스에 있으며
실질적으로 화면에 그려지는 부분은 State<MyRecord >클래스를 상속받은 MyRecordState클래스에서 그려진다
이 처럼 다른 클래스에서 선언된 recordName 변수에 접근하기 위해서 widget속성 (State 위젯에 widget속성이 있다)을 사용한다
즉 widget속성을 사용해서 state객체가 부모 위젯인 MyRecord Statefulwidget이 가지고 있는 모든 데이터에 접근이 가능하게 한다
Navigator.of(context).push(MaterialPageRoute(builder: (context) => MyRecord("WonderWorld")));
class MyRecord extends StatefulWidget { final String recordName; const MyRecord(this.recordName); @override MyRecordState createState() => MyRecordState(); } class MyRecordState extends State<MyRecord> { @override Widget build(BuildContext context) { return Text(widget.recordName); // Here you direct access using widget } }
반응형'프로그래밍 공부 메모 > flutter' 카테고리의 다른 글
앱이 시작할때 세팅하기 (0) 2022.06.18 AppBar 투명색(transparent)으로 겹치기 (0) 2022.06.17 firstwhere(),where(), toList(), toSet(), any(), map() (0) 2022.06.14 provider 다루기 (0) 2022.06.10 다이얼로그 창 (0) 2022.06.09