Skip to content

Instantly share code, notes, and snippets.

@ProZhar
Forked from eduardoflorence/main.dart
Created February 18, 2023 13:18
Show Gist options
  • Save ProZhar/d1ded914b65bb973a36d976b00f24c80 to your computer and use it in GitHub Desktop.
Save ProZhar/d1ded914b65bb973a36d976b00f24c80 to your computer and use it in GitHub Desktop.
GetX - Sample StateManager without GetMaterialApp
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(MaterialApp(home: HomePage()));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('HOME')),
body: Center(
child: RaisedButton(
child: Text('Go to page 2'),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => Page2()),
),
),
),
);
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Page2')),
body: Container(
child: GetX<Page2Controller>(
init: Page2Controller(),
builder: (_) {
return Center(
child: Text('${_.name.value}/${_.anotherController.name.value}'),
);
},
),
),
);
}
}
class Page2Controller extends GetxController {
final name = 'Page2'.obs;
final anotherController = Get.put(AnotherController());
@override
void onInit() {
print('onInit');
super.onInit();
}
@override
void onReady() {
print('onReady');
super.onReady();
}
@override
void onClose() {
Get.delete<AnotherController>();
print('onClose');
super.onClose();
}
}
class AnotherController extends GetxController {
final name = 'Another'.obs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment