how to hide with button and hide after a duration a Material banner or snackbar

21 views Asked by At

I want to hide a material banner or snackbarafter 1 second and there should be a button to hide the current material banner or snackbar. I've did this way and worked. Waht do you think? Do you have more stylish and suggest best practice for this pattern?

MaterialBanner(
  content: Text('Saved'),
  actions: [
    FutureBuilder(
        future:
            Future.delayed(Duration(seconds: 2)),
        builder: (context, snapshot) {
          if (snapshot.connectionState ==
              ConnectionState.done) {
            //auto hide scaffold messenger
            ScaffoldMessenger.of(context)
                .hideCurrentMaterialBanner();
          }
          return TextButton(
            onPressed: () {
              ScaffoldMessenger.of(context)
                  .hideCurrentMaterialBanner();
            },
            child: Text('hide'),
          );
        }),
  ],
),
0

There are 0 answers