In this section, the target
- 4 state management
- Obx
- GetX
- GetBuilder
- ValueBuilder
- Anti – shake, current limiting function
- ever
- once
- debounce
- interval
- Dependency injection
- Get.put
- Get.lazyPut
- The view components
- GetView
video
www.bilibili.com/video/BV1PA…
code
Github.com/ducafecat/g…
reference
- Pub. Flutter – IO. Cn/packages/ge…
- The dart. Dev/guides/lang…
The body of the
State management
Obx
- lib/pages/state_obx/index.dart
class StateObxView extends StatelessWidget {
StateObxView({Key? key}) : super(key: key);
final count = 0.obs;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Obx(...) "),
),
body: Center(
child: Column(
children: [
Obx(() => Text("count1 -> " + count.toString())),
Obx(() => Text("count2 -> " + count.toString())),
//
Divider(),
ElevatedButton(
onPressed: () {
count.value++;
},
child: Text('add'),),),),); }}Copy the code
- Obs, Extension, RxInt, Rx
.extension StringExtension on String {
/// Returns a `RxString` with [this] `String` as initial value.
RxString get obs => RxString(this);
}
extension IntExtension on int {
/// Returns a `RxInt` with [this] `int` as initial value.
RxInt get obs => RxInt(this);
}
extension DoubleExtension on double {
/// Returns a `RxDouble` with [this] `double` as initial value.
RxDouble get obs => RxDouble(this);
}
extension BoolExtension on bool {
/// Returns a `RxBool` with [this] `bool` as initial value.
RxBool get obs => RxBool(this);
}
extension RxT<T> on T {
/// Returns a `Rx` instace with [this] `T` as initial value.
Rx<T> get obs => Rx<T>(this);
}
Copy the code
- summary
It is suitable for simple state management on the interface and is fast to write.
GetX
- Write controller lib/pages/state_getx/controller.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++;
}
Copy the code
- Write the view lib/pages/state_getx/index.dart
class StateGetxView extends StatelessWidget {
StateGetxView({Key? key}) : super(key: key);
final controller = CountController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Getx"),
),
body: Center(
child: Column(
children: [
GetX<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
print("GetX - 1");
return Text('value 1 -> ${_.count}');
},
),
GetX<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
print("GetX - 2");
return Text('value 2 -> ${_.count}');
},
),
Divider(),
//
GetX<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
print("GetX - 3");
return Column(
children: [
Text('value 3 -> ${_.count}'),
ElevatedButton(
onPressed: () {
_.add();
},
child: Text('count1'), a)],); }, ), Divider(),// count2
GetX<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
print("GetX - 4");
return Text('value 4 -> ${_.count2}');
},
),
Divider(),
/ / button
ElevatedButton(
onPressed: () {
controller.add();
},
child: Text('count1'),
),
ElevatedButton(
onPressed: () {
controller.add2();
},
child: Text('count2'),),),),); }}Copy the code
- summary
Suitable for control of multi-controller, multi-state update, fine control of initial and local rendering.
GetBuilder
- Controller lib/pages/state_getBuilder/controller. The dart is alexandrine, not repeat
- View the lib/pages/state_getBuilder/index. The dart
class StateGetBuilderView extends StatelessWidget {
StateGetBuilderView({Key? key}) : super(key: key);
final 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'), a)],); }, ), 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(),
/ / button
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)'),),),),); }}Copy the code
-
summary
Compared to GetX, there are two things to note about manual updates.
controller.update();
Triggered updateid: "id2",
Mark whichbuilder
, trigger modecontroller.update(["id2"]);
, can be multipleArray
Type.
ValueBuilder
- lib/pages/state_valueBuilder/index.dart
class StateValueBuilderView extends StatelessWidget {
StateValueBuilderView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("ValueBuilder"),
),
body: Column(
children: [
Center(
child: ValueBuilder<int?>(
initialValue: 10,
builder: (value, updateFn) {
return Column(
children: [
Text("count -> " + value.toString()),
ElevatedButton(
onPressed: () {
updateFn(value! + 1);
},
child: Text('ValueBuilder -> add'), a)],); },// builder: (value, updateFn) => Switch(
// value: value,
// onChanged:
// updateFn, // same signature! you could use ( newValue ) => updateFn( newValue )
// ),
// if you need to call something outside the builder method.
onUpdate: (value) => print("Value updated: $value"),
onDispose: () => print("Widget unmounted"(() [() [() [() [() }}Copy the code
- summary
Suitable for local state management, very flexible.
Anti – shake, limit current
- The controller lib/pages/state_workers/controller. The dart
class CountController extends GetxController {
final _count = 0.obs;
set count(value) => this._count.value = value;
get count => this._count.value;
add() => _count.value++;
@override
void onInit() {
super.onInit();
/ / each time
ever(_count, (value) {
print("ever -> " + value.toString());
});
/ / for the first time
once(_count, (value) {
print("once -> " + value.toString());
});
// For 2 seconds
debounce(
_count,
(value) {
print("debounce -> " + value.toString());
},
time: Duration(seconds: 2));// The timer is 1 second
interval(
_count,
(value) {
print("interval -> " + value.toString());
},
time: Duration(seconds: 1)); }}Copy the code
- View the lib/pages/state_workers/index. The dart
class StateWorkersView extends StatelessWidget {
StateWorkersView({Key? key}) : super(key: key);
final controller = CountController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GetBuilder"),
),
body: Center(
child: Column(
children: [
/ / show
GetX<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
return Text('value -> ${_.count}'); },),/ / button
ElevatedButton(
onPressed: () {
controller.add();
},
child: Text('add'),),),),); }}Copy the code
- summary
Ever is suitable for monitoring and log collection
Debounce is suitable for a search entry box
Dependency injection
Get.put
- The controller lib/pages/dependency_put_find/controller. The dart
class CountController extends GetxController {
final _count = 0.obs;
set count(value) => this._count.value = value;
get count => this._count.value;
add() => _count.value++;
@override
void onInit() {
super.onInit();
print("onInit");
}
@override
void onClose() {
super.onClose();
print("onClose"); }}Copy the code
- Dart: lib/pages/dependency_put_find/index.dart
class StateDependencyPutFindView extends StatelessWidget {
StateDependencyPutFindView({Key? key}) : super(key: key);
final controller = Get.put<CountController>(CountController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Dependency"),
),
body: Center(
child: Column(
children: [
GetX<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
return Text('value -> ${_.count}');
},
),
Divider(),
/ / button
ElevatedButton(
onPressed: () {
controller.add();
},
child: Text('add'),),/ / jump
ElevatedButton(
onPressed: () {
Get.to(NextPageView());
},
child: Text('next page'),),),),); }}Copy the code
- The second view lib/pages/dependency_put_find/next_page.dart
class NextPageView extends StatelessWidget {
NextPageView({Key? key}) : super(key: key);
final controller = Get.find<CountController>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("NextPage"),
),
body: Center(
child: Column(
children: [
GetX<CountController>(
init: controller,
initState: (_) {},
builder: (_) {
return Text('value -> ${_.count}'); }, ), Divider(), ], ), ), ); }}Copy the code
Get. LazyPut + GetView lazy loading
- The controller lib/pages/dependency_lazyPut/controller. The dart
class CountController extends GetxController {
final _count = 0.obs;
set count(value) => this._count.value = value;
get count => this._count.value;
add() => _count.value++;
@override
void onInit() {
super.onInit();
print("onInit");
}
@override
void onClose() {
super.onClose();
print("onClose"); }}Copy the code
- Lib /pages/dependency_lazyPut/index.dart
class StateDependencyLazyPutView extends StatelessWidget {
StateDependencyLazyPutView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Dependency - LazyPut"),
),
body: Center(
child: Column(
children: [
GetX<CountController>(
init: Get.find<CountController>(),
initState: (_) {},
builder: (_) {
return Text('value -> ${_.count}');
},
),
Divider(),
/ / button
ElevatedButton(
onPressed: () {
Get.find<CountController>().add();
},
child: Text('add'),),/ / jump
ElevatedButton(
onPressed: () {
Get.to(NextPageView());
},
child: Text('Next GetView Page'),),),),); }}Copy the code
- The second view lib/pages/dependency_lazyPut/ next_getView_page.dart
class NextPageView extends GetView<CountController> {
NextPageView({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GetView Page"),
),
body: Center(
child: Column(
children: [
Obx(() => Text('value -> ${controller.count}')),
Divider(),
/ / button
ElevatedButton(
onPressed: () {
controller.add();
},
child: Text('add'),),),),); }}Copy the code
- Binding lib/pages/dependency_lazyPut/bindings. Dart
class DependencyLazyPutBinding implements Bindings {
@override
voiddependencies() { Get.lazyPut<CountController>(() => CountController()); }}Copy the code
- Routing lib/common/routes/app_pages. Dart
GetPage(
name: AppRoutes.DependencyLazyPut,
binding: DependencyLazyPutBinding(),
page: () => StateDependencyLazyPutView()),
Copy the code
The elder brother of the © cat
ducafecat.tech/
github.com/ducafecat
The issue of
Open source
GetX Quick Start
Github.com/ducafecat/g…
News client
Github.com/ducafecat/f…
Strapi manual translation
getstrapi.cn
Wechat discussion group Ducafecat
A series of collections
Dart programming language basics
Space.bilibili.com/404904528/c…
Start Flutter with zero basics
Space.bilibili.com/404904528/c…
Flutter combat news client from scratch
Space.bilibili.com/404904528/c…
Flutter component development
Space.bilibili.com/404904528/c…
Flutter Bloc
Space.bilibili.com/404904528/c…
Flutter Getx4
Space.bilibili.com/404904528/c…
Docker Yapi
Space.bilibili.com/404904528/c…