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

More than 1 year has passed since last update.

【Flutter】CupertinoAppではSnackBarは表示できない

Posted at

どうやらFlutterではCupertinoAppでSnackBarを表示することはできないようです。
私はFlutter3にアップデートしたタイミングで、 flutter_platform_widgets ライブラリのPlatformApp を使っている場所でSnackBarが表示されなくなったことでこの問題に気が付きました。

公式ドキュメントは見当たらなかったんですが、SnackBarはマテリアルデザインの概念なのでクパチーノ系では使えない方があるべき姿ということでしょうか。

表示できないことを検証したコード

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      // MaterialAppならSnackBar出る
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _showSnackBar() {
    final snackBar = SnackBar(content: Text("hogehoge"));
    // final snackBar = CupertinoPageS
    ScaffoldMessenger.of(context).showSnackBar(snackBar);
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      // ■ScaffoldならSnackBar出る
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
                onPressed: () => _showSnackBar(), child: Text("Show Snackbar"))
          ],
        ),
      ),
    );
  }
}
0
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
0
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?