incrementer_snackbar.dart

import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Snackbars', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Flutter Snackbars'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int _counter = 0; void _incrementCounter() => setState(() => _counter++); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.display1, ), ], ), ), // The button is wrapped in a Builder as Scaffold.of(context) cannot // be called in the same build function that creates the Scaffold. floatingActionButton: Builder( builder: (context) => FloatingActionButton( onPressed: () { _incrementCounter(); Scaffold.of(context).showSnackBar(SnackBar( content: Text('Counter is now $_counter'), )); }, tooltip: 'Increment', child: Icon(Icons.add), ), ), ); } }
The classic Flutter incrementer app with added SnackBar

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.