// Copyright 2014 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Template: dev/snippets/config/templates/freeform.tmpl // // Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring // of samples, and may be ignored if you are just exploring the sample. // Flutter code sample for ScaffoldMessenger.of // //*************************************************************************** //* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker) // Sometimes [SnackBar]s are produced by code that doesn't have ready access // to a valid [BuildContext]. One such example of this is when you show a // SnackBar from a method outside of the `build` function. In these // cases, you can assign a [GlobalKey] to the [ScaffoldMessenger]. This // example shows a key being used to obtain the [ScaffoldMessengerState] // provided by the [MaterialApp]. //* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker) //*************************************************************************** //**************************************************************************** //* ▼▼▼▼▼▼▼▼ code-imports ▼▼▼▼▼▼▼▼ (do not modify or remove section marker) import 'package:flutter/material.dart'; //* ▲▲▲▲▲▲▲▲ code-imports ▲▲▲▲▲▲▲▲ (do not modify or remove section marker) //**************************************************************************** //******************************************************************** //* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker) void main() => runApp(const MyApp()); class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override State createState() => _MyAppState(); } class _MyAppState extends State { final GlobalKey _scaffoldMessengerKey = GlobalKey(); int _counter = 0; void _incrementCounter() { setState(() { _counter++; }); if (_counter % 10 == 0) { _scaffoldMessengerKey.currentState!.showSnackBar(const SnackBar( content: Text('A multiple of ten!'), )); } } @override Widget build(BuildContext context) { return MaterialApp( scaffoldMessengerKey: _scaffoldMessengerKey, home: Scaffold( appBar: AppBar(title: const Text('ScaffoldMessenger Demo')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), ), ); } } //* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker) //********************************************************************