Skip to content

Instantly share code, notes, and snippets.

@moksh-mahajan
Created October 2, 2020 08:03
Show Gist options
  • Save moksh-mahajan/ded0bab73c0858555d212874c1637122 to your computer and use it in GitHub Desktop.
Save moksh-mahajan/ded0bab73c0858555d212874c1637122 to your computer and use it in GitHub Desktop.
To display snackbar from an alert dialog
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[],
),
),
floatingActionButton: FloatingActionButton.extended(
label: Text('Show Dialog'),
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Tap OK to show Snackbar'),
actions: [
FlatButton(
child: Text('OK'),
onPressed: () {
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text('I am a snackbar'),
),
);
Navigator.pop(context);
}),
],
),
);
},
tooltip: 'Increment',
icon: Icon(Icons.error),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment