Skip to content

Instantly share code, notes, and snippets.

@eduardoflorence
Created April 5, 2021 20:06
Show Gist options
  • Save eduardoflorence/cc664ebae5142358de39df2a3a98e6df to your computer and use it in GitHub Desktop.
Save eduardoflorence/cc664ebae5142358de39df2a3a98e6df 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