DEV Community

Gülsen Keskin
Gülsen Keskin

Posted on

Global Key ile Snackbar Kullanımı

1. Snackbar oluştur:

import 'package:flutter/material.dart';

class Utils {
 static final messengerKey = GlobalKey<ScaffoldMessengerState>();

  static showSnackBar(String? text) {
    if (text == null) return;
    final snackBar = SnackBar(
      content: Text(text),
      backgroundColor: Colors.red,
    );

    messengerKey.currentState!
      ..removeCurrentSnackBar()
      ..showSnackBar(snackBar);
  }
}
Enter fullscreen mode Exit fullscreen mode

2. MaterialApp'e key i ekle

MaterialApp(
      scaffoldMessengerKey: Utils.messengerKey,
...

Enter fullscreen mode Exit fullscreen mode

3. İstediğin yerde kullan

  Utils.showSnackBar("mesajını yaz");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)