Skip to content

Instantly share code, notes, and snippets.

@diegoveloper
Created September 16, 2020 01:12
Show Gist options
  • Save diegoveloper/3c4212a0f5bdcdb295656ae60bffef5f to your computer and use it in GitHub Desktop.
Save diegoveloper/3c4212a0f5bdcdb295656ae60bffef5f to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter_colombia/main.dart';
class _DiegoveloperX {
final navigatorKey = GlobalKey<NavigatorState>();
void showMyCustomDialog(Widget widget) {
showDialog(
context: navigatorKey.currentContext,
builder: (_) => AlertDialog(
content: widget,
),
);
}
void navigateTo(MyNewWidget myNewWidget) {
navigatorKey.currentState.push(MaterialPageRoute(
builder: (_) => myNewWidget,
));
}
void pop() {
navigatorKey.currentState.pop();
}
}
// ignore: non_constant_identifier_names
final DiegoveloperX = _DiegoveloperX();
import 'package:flutter/material.dart';
class HomeBloc extends ChangeNotifier {
int counter = 0;
void increment() {
counter++;
notifyListeners();
}
void decrement() {
counter--;
notifyListeners();
}
}
import 'package:flutter/material.dart';
import 'package:flutter_colombia/diegoveloper.dart';
import 'home_bloc.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
navigatorKey: DiegoveloperX.navigatorKey,
theme: ThemeData.dark(),
home: HomeWidget(),
);
}
}
class HomeWidget extends StatelessWidget {
final bloc = HomeBloc();
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: bloc,
builder: (context, _) {
return Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(Icons.remove_from_queue),
onPressed: () {
DiegoveloperX.showMyCustomDialog(
Text('Hello Colombia'),
);
},
),
IconButton(
icon: Icon(Icons.pages),
onPressed: () {
DiegoveloperX.navigateTo(MyNewWidget());
},
),
],
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: Container(
color: Colors.blue,
alignment: Alignment.center,
child: Text(
bloc.counter.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 90,
),
),
),
),
Expanded(
child: Container(
color: Colors.green,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: RaisedButton(
child: Icon(
Icons.add,
size: 100,
),
onPressed: () {
bloc.increment();
},
),
),
const SizedBox(width: 10),
Expanded(
child: RaisedButton(
child: Icon(
Icons.remove,
size: 100,
),
onPressed: () {
bloc.decrement();
},
),
),
],
),
),
),
],
),
);
});
}
}
class MyNewWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(Icons.backspace),
onPressed: () {
DiegoveloperX.pop();
},
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment