๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ Cross Platform/Flutter

[Flutter] ํ™”๋ฉด ์ด๋™ํ•˜๊ธฐ (Navigator Push & Pop)

by Fomagran ๐Ÿ’ป 2021. 6. 12.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ‘Ÿ ์ž…๋‹ˆ๋‹ค!

์˜ค๋Š˜์€ ํ”Œ๋Ÿฌํ„ฐ์—์„œ ๋‘ ํ™”๋ฉด(ScreenA์™€ ScreenB)์„ ์„œ๋กœ ์ด๋™ํ•˜๊ณ  ๋˜๋Œ์•„์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


ScreenA

 

ScreenA๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

import 'package:flutter/material.dart';

class ScreenA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Screen A page"),
      ),
    );
  }
}

ScreenB

 

ScreenB๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

import 'package:flutter/material.dart';

class ScreenB extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Screen B page"),
      ),
    );
  }
}

ScreenA์—์„œ ScreenB๋กœ ์ด๋™ํ•˜๊ธฐ

 

ScreenA์—์„œ ScreenB.dartํŒŒ์ผ์„ import ํ•ด์ค๋‹ˆ๋‹ค.

 

import 'ScreenB.dart';

 

Scaffold body์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

 Scaffold(
      appBar: AppBar(
        title: Text("Screen A page"),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text("Go to Main Page"),
        ),
      ),
    );

 

๊ทธ๋ฆฌ๊ณค ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ Navigator๋ฅผ pushํ•ด์ฃผ๊ณ  ์–ด๋–ค ์Šคํฌ๋ฆฐ์œผ๋กœ ์ด๋™ํ• ์ง€ ์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

ElevatedButton(
          child: Text("Go to Screen B Page"),
          onPressed: () {
            Navigator.push(
                context, MaterialPageRoute(builder: (_) => ScreenB()));
          },
        ),

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ScreenB ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 


ScreenB์—์„œ ScreenA๋กœ ๋˜๋Œ์•„๊ฐ€๊ธฐ

 

ScreenB๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฒ„ํŠผ์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์ด๋ฒˆ์— ๋˜๋Œ์•„๊ฐ€๊ธฐ ์œ„ํ•ด Navigator์— ํ˜„์žฌ context๋ฅผ pop ํ•ด์ค๋‹ˆ๋‹ค.

 

   body: Center(
        child: ElevatedButton(
          child: Text("Go to Screen A Page"),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),

 

์•„๋ž˜์™€ ๊ฐ™์ด ScreenB์—์„œ ScreenB๋กœ ๋˜๋Œ์•„๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ™”๋ฉด ์ด๋™ํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

ํ˜น์‹œ๋ผ๋„ ํ‹€๋ฆฐ ์ ์ด ์žˆ์œผ๋ฉด ์ง€์ ํ•ด์ฃผ์„ธ์š”!


Referece 

 

 

์ƒˆ๋กœ์šด ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•˜๊ณ , ๋˜๋Œ์•„์˜ค๊ธฐ

ํ™”๋ฉด๊ฐ„ ์ด๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•

flutter-ko.dev

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€