2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Flutter で Android と iOS のスワイプして戻る画面遷移機能を無効化する

Last updated at Posted at 2024-04-28

なにこれ

この戻る機能を無くしたいので、それを実装した時のメモを書く
image.png

解決方法

PopScope を利用して canPop: false を実装するだけで大丈夫でした。

Before

class _HomeViewState extends State<HomeView> {
  int selectedIndex;

  _HomeViewState({Key? key, required this.selectedIndex});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // コードが続く

After

class _HomeViewState extends State<HomeView> {
  int selectedIndex;

  _HomeViewState({Key? key, required this.selectedIndex});

  @override
  Widget build(BuildContext context) {
    return PopScope( // ここを追加
      canPop: false, // false で無効化
      child: Scaffold( // Scaffold は child に入れる
        // コードが続く

おわりに

この実装方法については公式サイトで紹介されていました。
デフォルトでは canPop は true ですよということでした。
https://api.flutter.dev/flutter/widgets/PopScope-class.html
image.png

追記

2024/08/03

Android の戻るボタンによってアプリをバックグラウンドへ移動させて、ホームに戻る処理を実装したくなったので以下をまとめました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?