반응형

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    initialRoute: '/first',
    routes: {
      '/first': (context) => const FirstPage(),
      '/second': (context) => const SecondPage(),
    },
  ));
}

 

first_page.dart

class FirstPage extends StatelessWidget {
  const FirstPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Welcome to Flutter'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () async {
            Navigator.pushNamed(context, '/second', arguments: '인자로 데이터를 넘깁니다.');
          },
          child: const Text('다음화면'),
        ),
      ),
    );
  }
}

 

second_page.dart

class SecondPage extends StatelessWidget {
  const SecondPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String argument = ModalRoute.of(context)!.settings.arguments as String;

    return Scaffold(
      appBar: AppBar(
        title: const Text('Second Page'),
      ),
      body: Center(
        child: Text(argument),
      ),
    );
  }
}
반응형

+ Recent posts