Skip to content

Instantly share code, notes, and snippets.

@pedromassango
Created November 18, 2020 19:58
Show Gist options
  • Save pedromassango/16ad67c5d2549243b96a5548654c1920 to your computer and use it in GitHub Desktop.
Save pedromassango/16ad67c5d2549243b96a5548654c1920 to your computer and use it in GitHub Desktop.
ScaffoldMessenger Example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isLoading = false;
void _loadData(BuildContext context) {
setState(() => _isLoading = !_isLoading);
Future.delayed(Duration(seconds: 5), () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Problemas de conexão a internet.'),
));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_isLoading? 'Loading data...' : 'Page 1'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Builder(
builder: (context) {
return ElevatedButton(child: Text('Navigate to Chat page'),
onPressed: () {
Navigator.push(context, MaterialPageRoute(
builder: (_) => ChatPage()
));
},
);
}
),
SizedBox(
height: 50,
width: 110,
child: Builder(
builder: (context) {
return ElevatedButton(child: _isLoading ? Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.white),
),
) : Text('Load Data'),
onPressed: () => _loadData(context),
);
}
),
),
ElevatedButton(child: Text('Navigate to Users page'),
onPressed: () {
Navigator.push(context, MaterialPageRoute(
builder: (_) => UsersPage()
));
},
),
],
)
),
);
}
}
class ChatPage extends StatefulWidget {
@override
_ChatPageState createState() => _ChatPageState();
}
class _ChatPageState extends State<ChatPage> {
bool _isLoading = false;
void _loadData(BuildContext context) {
setState(() => _isLoading = !_isLoading);
Future.delayed(Duration(seconds: 5), () {
setState(() => _isLoading = !_isLoading);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Problemas de conexão a internet.'),
));
});
}
@override
Widget build(BuildContext context) {
return ScaffoldMessenger(
child: Scaffold(
appBar: AppBar(
title: Text('Chat'),
),
body: Center(
child: Builder(
builder: (context) {
if(_isLoading) {
return CircularProgressIndicator();
}
return ElevatedButton(
child: Text('Load data'),
onPressed: () => _loadData(context),
);
}
),
),
),
);
}
}
class UsersPage extends StatelessWidget {
final style = TextStyle(fontWeight: FontWeight.bold);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Users'),
),
body: ListView.builder(
itemCount: 30,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(child: Text('$index', style: style),),
title: Text('User $index', style: style),
subtitle: Text('[email protected]'),
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment