How to make an AlertDialog in Flutter?

前端 未结 9 2835
小蘑菇
小蘑菇 2020-11-29 00:56

I am learning to build apps in Flutter. Now I have come to alert dialogs. I have done them before in Android and iOS, but how do I make an alert in Flutter?

Here are

9条回答
  •  被撕碎了的回忆
    2020-11-29 01:02

    Check out Flutter Dropdown Banner to easily alert users of events and prompt action without having to manage the complexity of presenting, delaying, and dismissing the component.

    To set it up:

    import 'packages:dropdown_banner/dropdown_banner.dart';
    ...
    class MainApp extends StatelessWidget {
      ...
      @override
      Widget build(BuildContext context) {
        final navigatorKey = GlobalKey();
        ...
        return MaterialApp(
            ...
            home: DropdownBanner(
              child: Scaffold(...),
              navigatorKey: navigatorKey,
            ),
        );
      }
    }
    

    To use it:

    import 'packages:dropdown_banner/dropdown_banner.dart';
    ...
    class SomeClass {
      ...
      void doSomethingThenFail() {
        DropdownBanner.showBanner(
          text: 'Failed to complete network request',
          color: Colors.red,
          textStyle: TextStyle(color: Colors.white),
        );
      }
    }
    
    Click here to see an example

提交回复
热议问题