Skip to content

Instantly share code, notes, and snippets.

@ProZhar
Forked from eduardoflorence/main.dart
Created February 18, 2023 13:17
Show Gist options
  • Save ProZhar/d29ce129660ee778ba4c49cdd36489fb to your computer and use it in GitHub Desktop.
Save ProZhar/d29ce129660ee778ba4c49cdd36489fb to your computer and use it in GitHub Desktop.
GetX - Sample MixinBuilder
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(GetMaterialApp(home: HomePage()));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('HOME')),
body: Center(
child: ElevatedButton(
child: Text('Go to page 2'),
onPressed: () => Get.to(() => Page2()),
),
),
);
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Page2')),
body: Container(
child: MixinBuilder<Page2Controller>(
init: Page2Controller(),
builder: (controller) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(controller.counter1.toString()),
Text(controller.counter2.toString()),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: controller.increment1,
child: Text('Increment1'),
),
ElevatedButton(
onPressed: controller.increment2,
child: Text('Increment2'),
),
],
)
],
);
},
),
),
);
}
}
class Page2Controller extends GetxController {
RxInt counter1 = 1.obs;
int counter2 = 1;
increment1() {
counter1.value++;
}
increment2() {
counter2++;
update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment