How to access Scaffold methods in stateless Flutter widget
Tags: flutter, beginners
Published: 2019-11-19 17:07:14

---
title: How to access Scaffold methods in stateless Flutter widget
published: true
description: Quick explanation of scaffold key and how to use it
tags: flutter,beginners
---

Built with

[![Git Tutor](https://git-tutor-assets.s3.eu-west-2.amazonaws.com/git-tutor-logo-50.png)](https://github.com/lesnitsky/git-tutor)

[![GitHub stars](https://img.shields.io/github/stars/lesnitsky/flutter_blog.svg?style=social&hash=20191119)](https://github.com/lesnitsky/flutter_blog)
[![Twitter Follow](https://img.shields.io/twitter/follow/lesnitsky_a.svg?label=Follow%20me&style=social&hash=20191119)](https://twitter.com/lesnitsky_a)


Hi 👋

This post will demonstrate how to access `ScaffoldState` methods (like `showSnackBar`) in `StatelessWidget`.

Let's reproduce a simple example demonstrating the issue and create a `HomePage` stateless widget with a `Scaffold`

📄 lib/main.dart
```diff
      );
    }
  }
+ 
+ class HomePage extends StatelessWidget {
+   @override
+   Widget build(BuildContext context) {
+     return Scaffold(
+       appBar: AppBar(
+         title: Text('Stateless Widget Scaffold'),
+       ),
+     );
+   }
+ }

```
Add a button which will call `_showSnackbar` when pressed

📄 lib/main.dart
```diff
        appBar: AppBar(
          title: Text('Stateless Widget Scaffold'),
        ),
+       body: Center(
+         child: FlatButton(
+           child: Text("Show snackbar"),
+           onPressed: _showSnackbar,
+         ),
+       ),
      );
    }
  }

```
We can use `GlobalKey` to access `ScaffoldState`, so let's create one

📄 lib/main.dart
```diff
  }
  
  class HomePage extends StatelessWidget {
+   final scaffoldKey = new GlobalKey();
+ 
    @override
    Widget build(BuildContext context) {
      return Scaffold(

```
pass it to `Scaffold`

📄 lib/main.dart
```diff
    @override
    Widget build(BuildContext context) {
      return Scaffold(
+       key: scaffoldKey,
        appBar: AppBar(
          title: Text('Stateless Widget Scaffold'),
        ),

```
and implement method `_showSnackbar`

📄 lib/main.dart
```diff
  class HomePage extends StatelessWidget {
    final scaffoldKey = new GlobalKey();
  
+   _showSnackbar() {
+     (scaffoldKey.currentState as ScaffoldState).showSnackBar(
+       SnackBar(
+         content: Text("I'm snackbar!"),
+       ),
+     );
+   }
+ 
    @override
    Widget build(BuildContext context) {
      return Scaffold(

```
That's it! 🎉

[Soruce code is available here](https://github.com/lesnitsky/flutter_blog/tree/blog/stateless_widget_scaffold)

![Stateless widget scaffold screenshot](https://screenshots-lesnitsky.s3.eu-west-2.amazonaws.com/stateless-scaffold-screenshot.png)

Built with

[![Git Tutor](https://git-tutor-assets.s3.eu-west-2.amazonaws.com/git-tutor-logo-50.png)](https://github.com/lesnitsky/git-tutor)

[![GitHub stars](https://img.shields.io/github/stars/lesnitsky/flutter_blog.svg?style=social&hash=20191119)](https://github.com/lesnitsky/flutter_blog)
[![Twitter Follow](https://img.shields.io/twitter/follow/lesnitsky_a.svg?label=Follow%20me&style=social&hash=20191119)](https://twitter.com/lesnitsky_a)