Skip to content

Commit

Permalink
add -> GetBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ducafecat committed Apr 1, 2021
1 parent 276097b commit fceea45
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/common/routes/app_pages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:getx_quick_start/pages/list/index.dart';
import 'package:getx_quick_start/pages/login/index.dart';
import 'package:getx_quick_start/pages/my/index.dart';
import 'package:getx_quick_start/pages/notfound/index.dart';
import 'package:getx_quick_start/pages/state_getBuilder/index.dart';
import 'package:getx_quick_start/pages/state_getx/index.dart';
import 'package:getx_quick_start/pages/state_obx/index.dart';
import 'package:getx_quick_start/pages/state_valueBuilder/index.dart';
Expand Down Expand Up @@ -39,6 +40,7 @@ class AppPages {
GetPage(
name: AppRoutes.ValueBuilder, page: () => StateValueBuilderView()),
GetPage(name: AppRoutes.Getx, page: () => StateGetxView()),
GetPage(name: AppRoutes.GetBuilder, page: () => StateGetBuilderView()),
]),

// 其它
Expand Down
1 change: 1 addition & 0 deletions lib/common/routes/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ abstract class AppRoutes {
static const Obx = '/obx';
static const ValueBuilder = '/valueBuilder';
static const Getx = '/getx';
static const GetBuilder = '/getBuilder';
}
5 changes: 5 additions & 0 deletions lib/pages/home/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ class HomeView extends StatelessWidget {
subtitle: Text('Get.toNamed(AppRoutes.Getx)'),
onTap: () => Get.toNamed(AppRoutes.State + AppRoutes.Getx),
),
ListTile(
title: Text("State-GetBuilder"),
subtitle: Text('Get.toNamed(AppRoutes.GetBuilder)'),
onTap: () => Get.toNamed(AppRoutes.State + AppRoutes.GetBuilder),
),
ListTile(
title: Text("State-ValueBuilder"),
subtitle: Text('Get.toNamed(AppRoutes.ValueBuilder)'),
Expand Down
15 changes: 15 additions & 0 deletions lib/pages/state_getBuilder/controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:get/get.dart';

class CountController extends GetxController {
final _count = 0.obs;
set count(value) => this._count.value = value;
get count => this._count.value;

final _count2 = 0.obs;
set count2(value) => this._count2.value = value;
get count2 => this._count2.value;

add() => _count.value++;

add2() => _count2.value++;
}
121 changes: 121 additions & 0 deletions lib/pages/state_getBuilder/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import 'package:flutter/material.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:get/get.dart';
import './controller.dart';

/*
* GetBuilder 手动控制
* controller -> update
*/

// ignore: must_be_immutable
class StateGetBuilderView extends StatelessWidget {
StateGetBuilderView({Key? key}) : super(key: key);

var controller = CountController();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GetBuilder"),
),
body: Center(
child: Column(
children: [
GetBuilder<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
print("GetBuilder - 1");
return Text('value -> ${_.count}');
},
),
GetBuilder<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
print("GetBuilder - 2");
return Text('value -> ${_.count}');
},
),
Divider(),

//
GetBuilder<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
print("GetBuilder - 3");
return Column(
children: [
Text('value -> ${_.count}'),
ElevatedButton(
onPressed: () {
_.add();
},
child: Text('GetBuilder -> add'),
)
],
);
},
),
Divider(),

// count2
GetBuilder<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
print("GetBuilder - 4");
return Text('value count2 -> ${_.count2}');
},
),
Divider(),

// id2
GetBuilder<CountController>(
id: "id2",
init: controller,
initState: (_) {},
builder: (_) {
print("GetBuilder - 4");
return Text('id2 -> value count2 -> ${_.count2}');
},
),
Divider(),

// 按钮
ElevatedButton(
onPressed: () {
controller.add();
},
child: Text('add'),
),

ElevatedButton(
onPressed: () {
controller.add2();
},
child: Text('add2'),
),

ElevatedButton(
onPressed: () {
controller.update();
},
child: Text('controller.update()'),
),

ElevatedButton(
onPressed: () {
controller.update(["id2"]);
},
child: Text('controller.update(id2)'),
),
],
),
),
);
}
}
13 changes: 9 additions & 4 deletions lib/pages/state_getx/index.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import 'package:flutter/material.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:get/get.dart';
import 'package:getx_quick_start/pages/state_getx/controller.dart';
import './controller.dart';

/*
* GetX 局部自动控制
* controller -> update
*/

// ignore: must_be_immutable
class StateGetxView extends StatelessWidget {
Expand All @@ -13,7 +18,7 @@ class StateGetxView extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("ValueBuilder"),
title: Text("Getx"),
),
body: Center(
child: Column(
Expand Down Expand Up @@ -63,7 +68,7 @@ class StateGetxView extends StatelessWidget {
initState: (_) {},
builder: (_) {
print("GetX - 4");
return Text('value2 -> ${_.count2}');
return Text('value -> ${_.count2}');
},
),
Divider(),
Expand All @@ -80,7 +85,7 @@ class StateGetxView extends StatelessWidget {
onPressed: () {
controller.add2();
},
child: Text('add2'),
child: Text('add2 + update'),
),
],
),
Expand Down

0 comments on commit fceea45

Please sign in to comment.