[Flutter] 플러터 페이지 이동

ShinDasom·2023년 7월 20일

Flutter 화면생성

FirstPage

 import 'package:flutter/material.dart';

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

  
  State<FirstPage> createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('FirstPage'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
              ElevatedButton(onPressed: (){
             
              }, child: Text('Go To SecondPage')),
          ],
        ),
      ),

    );
  }
}

SecondPage

import 'package:flutter/material.dart';

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

  
  State<SecondPage> createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SecondPage'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: (){

            }, child: Text('Go To FirstPage')),
          ],
        ),
      ),
    );
  }
}



Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage()));
 Navigator.pop(context);

FirstPage

버튼을 클릭하면 페이지 이동을 하도록 ElevatedButton의 onPressed 메서드 안에 작성해줍니다.
import 'package:flutter/material.dart';
import 'package:testpage/secondPage.dart';

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

 
 State<FirstPage> createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
 
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text('FirstPage'),
     ),
     body: Center(
       child: Column(
         mainAxisAlignment: MainAxisAlignment.center,
         children: [
             ElevatedButton(onPressed: (){
               Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage()));

             }, child: Text('Go To SecondPage')),
         ],
       ),
     ),

   );
 }
}

SecondPage

import 'package:flutter/material.dart';

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

 
 State<SecondPage> createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
 
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text('SecondPage'),
     ),
     body: Center(
       child: Column(
         mainAxisAlignment: MainAxisAlignment.center,
         children: [
           ElevatedButton(onPressed: (){
             Navigator.pop(context);

           }, child: Text('Go To FirstPage')),
         ],
       ),
     ),
   );
 }
}

1개의 댓글

comment-user-thumbnail
2023년 7월 20일

가치 있는 정보 공유해주셔서 감사합니다.

답글 달기