Flutter

Flutter - routes 방식으로 데이터 전달하기

wdadaww 2023. 3. 7. 23:34

flutter에서 페이지 이동은 2가지 방법으로 사용가능합니다.

  • 1.Push방식

웹이나 ,페이지 이동시 현재 경로를 노출시키지 않는다 (보안상 좋음.)

  • 2.routes 방식

현재 경로를 보여주는 것이 가장 큰차이점 (페이지 이동시 찾아가기 쉽다.)

-> 앱이 커질수록 주로 routes 방식 을 많이 쓴다.

'/write_gratitude는 앞에서 추가한 routes 내부에 들어가는 이름이고, pushnamed 가장 뒤에 argument 추가하고, 데이터를 넘겨준다.

import 'package:flutter/material.dart';

class WriteGratitude extends StatefulWidget {
  final String title = "";
  const WriteGratitude({Key? key}) : super(key: key);

  @override
  State<WriteGratitude> createState() => _WriteGratitudeState();
}

class _WriteGratitudeState extends State<WriteGratitude> {

  int seq = -1;
  var arguments;

  @override
  Widget build(BuildContext context) {
    if (seq == -1) {
      arguments = (ModalRoute.of(context)?.settings.arguments ??
          <String, dynamic>{}) as Map;
      seq = arguments['seq'];
      loadDetail(seq);

    }

    return Scaffold(
      appBar: AppBar(
        title: Text(seq.toString()),
      ),
        body: const Placeholder());
  }

  void loadDetail(int seq){
    // Server data , setState draw page
  }
}

 

import 'package:flutter/material.dart';

class WriteGratitude extends StatefulWidget {
  final String title = "";

  const WriteGratitude({Key? key}) : super(key: key);

  @override
  State<WriteGratitude> createState() => _WriteGratitudeState();
}

class _WriteGratitudeState extends State<WriteGratitude> {
  int seq = -1;
  var arguments;

  @override
  Widget build(BuildContext context) {
    if (seq == -1) {
      arguments = (ModalRoute
          .of(context)
          ?.settings
          .arguments ??
          <String, dynamic>{}) as Map;
      seq = arguments['seq'];
      loadDetail(seq);
    }

    return Scaffold(
        appBar: AppBar(
        centerTitle: true,
        title: Text(seq.toString()),
    ),body
    :
    const
    Placeholder
    (
    )
    );
  }

  void loadDetail(int seq) {
    // Server data , setState draw page
  }
}

 

 

 

 

  • pop() 메서드를 통해 뒤로가기 버튼을 만들어 메인페이지로 이동