[ Flutter ] Navigator

from.kยท2023๋…„ 11์›” 29์ผ
0

Mobile-Software

๋ชฉ๋ก ๋ณด๊ธฐ
14/15
post-thumbnail

๐Ÿ”Š Rout

๋‹จ์ˆœํžˆ ์Šค๋งˆํŠธํฐ์ƒ์—์„œ ๋ณด์—ฌ์ง€๋Š” ํ•˜๋‚˜์˜ ํŽ˜์ด์ง€ ํ˜น์€ ํ™”๋ฉด

๐Ÿงญ Navigator

๋ผ์šฐํ„ฐ(๋ชจ๋“  ์•ฑํŽ˜์ด์ง€)๋ฅผ ๋„ค๋น„๊ฒŒ์ดํ„ฐ ์œ„์ ฏ์ด ๊ด€๋ฆฌํ•˜๋ฉฐ, ์Šคํƒ ๊ตฌ์กฐ๋กœ ๋ผ์šฐํ„ฐ ๊ฐ์ฒด๋“ค์„ ๊ด€๋ฆฌ
๋„ค๋น„๊ฒŒ์ดํ„ฐ๋Š” ์•ฑ ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจ๊ณก์ฐจ๊ณก ์Œ“๋Š” ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ ์‚ฌ์šฉ
์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ด€๋ฆฌ ์œ„ํ•ด push, pop ๋ฉ”์†Œ๋“œ ์ œ๊ณต


๋ผ์šฐํ„ฐ๋Š” ๋ฐ˜๋“œ์‹œ MaterialAPP ์•„๋ž˜์— child ๋กœ ์ƒ์„ฑ๋˜์–ด์•ผํ•จ
์œ„์ ฏํŠธ๋ฆฌ์ƒ ๋ชจ๋“  ์œ„์ ฏ์€ MaterialApp์ด ๊ฐ์‹ธ๊ณ  ์žˆ์–ด์•ผํ•จ


๋„ค์ด๊ฒŒ์ดํ„ฐ ์œ„์ ฏ์„ ๋ถˆ๋Ÿฌ์˜ด

  • ๋„ค์ด๊ฒŒ์ดํ„ฐ์™€ ๊ด€๋ฆฌํ•˜๋Š” ๋ผ์šฐํ„ฐ ์Šคํƒ ๊ตฌ์กฐ ๊ฐ€์žฅ ์œ„์— ํ˜„์žฌ ๋™๋–จ์–ด์ง„ 2๋ฒˆ์งธ ํŽ˜์ด์ง€๊ฐ€ ์˜ฌ๋ผ๊ฐ€์•ผํ•จ
    => ์ฆ‰, ๋ฒ„ํŠผ์ด ๋ˆŒ๋Ÿฌ์ง€๋Š” ๋™์‹œ์— 2๋ฒˆ์งธ ํŽ˜์ด์ง€๋Š” ์œ„์ ฏ ํŠธ๋ฆฌ ๊ตฌ์กฐ์— ๋ผ์–ด๋“ค ์ˆ˜ ์žˆ์Œ
    ์ด๋ฅผ ์œ„ํ•ด push ํ•จ์ˆ˜ ์‚ฌ์šฉ -> push ๋Š” context ์™€ route ๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผํ•จ
    - context ์˜ ๊ฒฝ์šฐ BuildContext ์˜ context ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ๊ณ ,
    - rout => ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๊ฒฝ์šฐ ์ด๋™ํ•  ํŽ˜์ด์ง€

๋ช‡๊ฐ€์ง€ ์„ค์ • ํ•„์š”
ํ”Œ๋Ÿฌํ„ฐ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” MaterialPageRout() ์—
builder(์–ด๋–ค ์œ„์ ฏ์ด ๋ผ์šฐํ„ฐ์˜ ๋„์›€์„ ๋ฐ›์•„ ์ƒ์„ฑ๋˜์–ด์•ผํ• ์ง€๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ) ์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ 
-> BuildContext ์˜ context ๋ฅผ ์ธ์ž๋กœ ๋ฐ”๊พธ๊ณ , SecondPage ๋ฅผ ๋ฆฌํ„ดํ•ด์คŒ
builder ์€ ์ผ์ข…์˜ ์•ˆ์ „์žฅ์น˜


secont page -> firstpage ์ด๋™

: pop ์„ ํ•ด์คŒ
SecondPage ์˜ BuildContext ๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ์ด๋ฅผ ์‚ญ์ œํ•จ.


import 'package:flutter/material.dart';

void main() => runApp(MyAPP());

class MyAPP extends StatelessWidget {
  const MyAPP({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstPage(),
    );
  }
}

class FirstPage extends StatelessWidget {
  const FirstPage({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First page'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Go to the Second Page'),
          onPressed: () {
            Navigator.push(context,
                MaterialPageRoute(builder: (BuildContext context) {
              return SecondPage();
            }));
          },
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  
  Widget build(BuildContext context2) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second page'),
      ),
      body: Center(
        child: ElevatedButton(
          child: Text('Go to the First Page'),
          onPressed: () {
            Navigator.pop(context2);
          },
        ),
      ),
    );
  }
}



initailRoute -> ๋ฉ€ํ‹ฐ ํŽ˜์ด์ง€ ๋ผ์šฐํŠธํ•  ๋–„ ํ™”๋ฉด์˜ ์ œ์ผ ์ฒ˜์Œ ์ถœ๋ ฅ๋˜๋Š” ๋ผ์šฐํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์ผํ‚ค๋Š” ์—ญํ• 

routes argument๋Š” ์ด๋™ํ•  ํŽ˜์ด์ง€๋“ค์˜ ์ด๋ฆ„ ์ง€์ •,
string๊ณผ Widget ์ด Map ํ˜•ํƒœ๋กœ ํ•œ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์•ผํ•จ

import 'package:flutter/material.dart';
import 'package:navigator/ScreenA.dart';
import 'package:navigator/ScreenB.dart';
import 'package:navigator/ScrennC.dart';

void main() => runApp(MyAPP());

class MyAPP extends StatelessWidget {
  const MyAPP({super.key});

  
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (context) => ScreenA(),
        '/b': (context) => ScreenB(),
        '/c': (context) => ScreenC(),
      },
    );
  }
}
import 'package:flutter/material.dart';
import 'package:navigator/main.dart';

void main() => runApp(ScreenA());

class ScreenA extends StatelessWidget {
  const ScreenA({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ScreenA'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/b');
              },
              child: Text('Go to ScreenB'),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.redAccent,
              ),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/c');
              },
              child: Text('Go to ScreenC'),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.redAccent,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
import 'package:flutter/material.dart';
import 'package:navigator/main.dart';

void main() => runApp(ScreenB());

class ScreenB extends StatelessWidget {
  const ScreenB({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ScreenB'),
      ),
      body: Center(
        child: Text(
          'ScreenB',
          style: TextStyle(
            fontSize: 24.0,
          ),
        ),
      ),
    );
  }
}
import 'package:flutter/material.dart';
import 'package:navigator/main.dart';

void main() => runApp(ScreenC());

class ScreenC extends StatelessWidget {
  const ScreenC({super.key});

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ScreenC'),
      ),
      body: Center(
        child: Text(
          'ScreenC',
          style: TextStyle(
            fontSize: 24.0,
          ),
        ),
      ),
    );
  }
}

ScreenAScreenBScreenC






๐Ÿ“š ์ฝ”๋”ฉ์…ฐํ”„ < ํ”Œ๋Ÿฌํ„ฐ(flutter) ์ˆœํ•œ๋ง› ๊ฐ•์ขŒ >

0๊ฐœ์˜ ๋Œ“๊ธ€