Flutter : PopupMenuから画面遷移。AndroidとiPhone

Flutterでアプリ開発のための部品あつめ

Popupメニューからの画面遷移を試す。

flutter_popupmenu

lib/main.dart

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import './google_sign_in.dart';
import './firestore_cloud_vision.dart';

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

class MyApp extends StatelessWidget {
  final message = "Initial Message.";
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Sample',
      home: MyHomePage(message:this.message),
    );
  }
}

enum Menu { google_sign_in, firestore_cloud_vision }
class MyHomePageState extends State<MyHomePage>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.message)
      ),
      body: PopupMenuButton(
        onSelected: popupMenuSelected,
        itemBuilder: (BuildContext context) =>
        <PopupMenuEntry<Menu>>[
          const PopupMenuItem( child: const ListTile( leading:Icon(Icons.supervisor_account),
              title:Text("Google Sign In")), value: Menu.google_sign_in),
          const PopupMenuItem( child: const ListTile( leading:Icon(Icons.crop_original),
              title:Text("Firestore,CloudVision")), value: Menu.firestore_cloud_vision,),
        ],
      ),
    );
  }

  void popupMenuSelected(Menu selectedMenu){
    switch(selectedMenu) {
      case Menu.google_sign_in:
        _pushPage(context, GoogleSignInPage());
        break;
      case Menu.firestore_cloud_vision:
        _pushPage(context, FirestoreCloudVisionPage());
        break;
      default:
        break;
    }
  }

  void _pushPage(BuildContext context, Widget page) {
    Navigator.of(context).push(
      MaterialPageRoute<void>(builder: (_) => page)
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String message;
  MyHomePage({this.message}):super() {}
  @override
  State<StatefulWidget> createState() => new MyHomePageState();
}

Follow me!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です