Flutterで画面遷移

画面遷移のサンプルコード。

main.dart

import 'package:flutter/material.dart';

void main() {
    runApp(const MaterialApp(
        title: 'Sample App',
        home: FirstPage(),
    ));
}

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

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
                title: const Text('First Page'),
            ),
            body: Center(
                child: ElevatedButton(
                    child: const Text('Next page'),
                    onPressed: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(builder: (context) => const SecondPage()),
                        );
                    },
                ),
            ),
        );
    }
}

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

    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
                title: const Text("Second Page"),
            ),
            body: Center(
                child: ElevatedButton(
                    onPressed: () {
                        Navigator.pop(context);
                    },
                    child: const Text('Back'),
                ),
            ),
        );
    }
}