Skip to content

Instantly share code, notes, and snippets.

@filiph
Created August 8, 2020 02:31
Show Gist options
  • Save filiph/b84641713571200d24ef145085764060 to your computer and use it in GitHub Desktop.
Save filiph/b84641713571200d24ef145085764060 to your computer and use it in GitHub Desktop.
SnackBar floating regression
import 'package:flutter/material.dart';
void main() => runApp(SnackBarDemo());
class SnackBarDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SnackBar Demo',
home: Scaffold(
appBar: AppBar(
title: Text('SnackBar Demo'),
),
body: SnackBarPage(),
bottomSheet: Container(
color: Colors.yellow,
child: SizedBox(
height: 200,
child: Text('Bottom sheet'),
),
),
// This is a workaround that makes snack bars appear _above_
// the bottom sheet. Floating snackbars are shown above FABs,
// so we just create an invisible, zero-size FAB here.
floatingActionButton: const SizedBox(height: 1),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
),
);
}
}
class SnackBarPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: RaisedButton(
onPressed: () {
final snackBar = SnackBar(
behavior: SnackBarBehavior.floating,
content: Text('Yay! A SnackBar!'),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
// Some code to undo the change.
},
),
);
// Find the Scaffold in the widget tree and use
// it to show a SnackBar.
Scaffold.of(context).showSnackBar(snackBar);
},
child: Text('Show SnackBar'),
),
);
}
}
@maheshj01
Copy link

Updated code as per latest version of flutter

import 'package:flutter/material.dart';

void main() => runApp(SnackBarDemo());

class SnackBarDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SnackBar Demo',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('SnackBar Demo'),
        ),
        body: SnackBarPage(),
        bottomSheet: Container(
          color: Colors.yellow,
          child: const SizedBox(
            height: 200,
            child: Text('Bottom sheet'),
          ),
        ),
        // This is a workaround that makes snack bars appear _above_
        // the bottom sheet. Floating snackbars are shown above FABs,
        // so we just create an invisible, zero-size FAB here.
        floatingActionButton: const SizedBox(height: 1),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      ),
    );
  }
}

class SnackBarPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () {
          final snackBar = SnackBar(
            behavior: SnackBarBehavior.floating,
            content: const Text('Yay! A SnackBar!'),
            action: SnackBarAction(
              label: 'Undo',
              onPressed: () {
                // Some code to undo the change.
              },
            ),
          );

          // Find the Scaffold in the widget tree and use
          // it to show a SnackBar.
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
        },
        child: const Text('Show SnackBar'),
      ),
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment