LoginSignup
1
0

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

Posted at

なにこれ

この戻る機能を無くしたいので、それを実装した時のメモを書く
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

1
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
1
0