Advertisement
narimetisaigopi

MaterialBanner

Sep 22nd, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() {
  4. runApp(const MyApp());
  5. }
  6.  
  7. class MyApp extends StatelessWidget {
  8. const MyApp({Key? key}) : super(key: key);
  9.  
  10. // This widget is the root of your application.
  11. @override
  12. Widget build(BuildContext context) {
  13. return MaterialApp(
  14. title: 'Flutter Demo',
  15. theme: ThemeData(
  16. // This is the theme of your application.
  17. //
  18. // Try running your application with "flutter run". You'll see the
  19. // application has a blue toolbar. Then, without quitting the app, try
  20. // changing the primarySwatch below to Colors.green and then invoke
  21. // "hot reload" (press "r" in the console where you ran "flutter run",
  22. // or simply save your changes to "hot reload" in a Flutter IDE).
  23. // Notice that the counter didn't reset back to zero; the application
  24. // is not restarted.
  25. primarySwatch: Colors.blue,
  26. ),
  27. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  28. );
  29. }
  30. }
  31.  
  32. class MyHomePage extends StatefulWidget {
  33. const MyHomePage({Key? key, required this.title}) : super(key: key);
  34.  
  35. // This widget is the home page of your application. It is stateful, meaning
  36. // that it has a State object (defined below) that contains fields that affect
  37. // how it looks.
  38.  
  39. // This class is the configuration for the state. It holds the values (in this
  40. // case the title) provided by the parent (in this case the App widget) and
  41. // used by the build method of the State. Fields in a Widget subclass are
  42. // always marked "final".
  43.  
  44. final String title;
  45.  
  46. @override
  47. State<MyHomePage> createState() => _MyHomePageState();
  48. }
  49.  
  50. class _MyHomePageState extends State<MyHomePage> {
  51. int _counter = 0;
  52.  
  53. void _incrementCounter() {
  54. setState(() {
  55. // This call to setState tells the Flutter framework that something has
  56. // changed in this State, which causes it to rerun the build method below
  57. // so that the display can reflect the updated values. If we changed
  58. // _counter without calling setState(), then the build method would not be
  59. // called again, and so nothing would appear to happen.
  60. _counter++;
  61. });
  62. }
  63.  
  64. @override
  65. Widget build(BuildContext context) {
  66. return Scaffold(
  67. appBar: AppBar(
  68. title: const Text("MaterialBanner"),
  69. ),
  70. body: Center(
  71. child: Column(
  72. mainAxisAlignment: MainAxisAlignment.center,
  73. children: <Widget>[
  74. ElevatedButton(
  75. onPressed: () {
  76. MaterialBanner materialBanner = MaterialBanner(
  77. backgroundColor: Colors.amber,
  78. leading: Icon(Icons.wallet_giftcard),
  79. content: Text("I am new material banner"),
  80. contentTextStyle: TextStyle(
  81. color: Colors.black, fontWeight: FontWeight.bold),
  82. actions: [
  83. TextButton(
  84. onPressed: () {
  85. ScaffoldMessenger.of(context)
  86. .hideCurrentMaterialBanner();
  87. },
  88. child: Text("Open")),
  89. TextButton(
  90. onPressed: () {
  91. ScaffoldMessenger.of(context)
  92. .hideCurrentMaterialBanner();
  93. },
  94. child: Text("Close"))
  95. ],
  96. );
  97.  
  98. ScaffoldMessenger.of(context)
  99. .showMaterialBanner(materialBanner);
  100. },
  101. child: const Text('Show Banner'))
  102. ],
  103. ),
  104. ),
  105. // This trailing comma makes auto-formatting nicer for build methods.
  106. );
  107. }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement