-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request] Mixin equivalent to AutomaticKeepAliveClientMixin #822
Comments
Hy @lpylpyleo,
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(GetMaterialApp(home: Home()));
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
final controller = Get.put(HomeController());
return Scaffold(
appBar: AppBar(
bottom: TabBar(
controller: controller.tabController,
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
title: Text('Tabs Demo'),
),
body: TabBarView(
controller: controller.tabController,
children: [
CarPage(),
BikePage(),
],
),
);
}
}
class HomeController extends GetxController with SingleGetTickerProviderMixin {
TabController tabController;
@override
void onInit() {
tabController = TabController(vsync: this, length: 2);
super.onInit();
}
}
class CarPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final controller = Get.put(CarPageController());
return Center(
child: Obx(() => Text(controller.car.value)),
);
}
}
class CarPageController extends GetxController {
final car = ''.obs;
@override
void onInit() {
print('Call API Car'); // called only once
car.value = 'Ferrari';
super.onInit();
}
}
class BikePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final controller = Get.put(BikePageController());
return Center(
child: Obx(() => Text(controller.bike.value)),
);
}
}
class BikePageController extends GetxController {
final bike = ''.obs;
@override
void onInit() {
print('Call API Bike'); // called only once
bike.value = 'BMC';
super.onInit();
}
} |
Thanks for you sample code. @eduardoflorence I edited your second class BikePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
print('BikePage build'); // called every time
final controller = Get.put(BikePageController());
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Text(index.toString());
},
);
}
}
class BikePageController extends GetxController {
final bike = ''.obs;
@override
void onInit() {
print('Call API Bike'); // called only once
bike.value = 'BMC';
super.onInit();
}
} As a comparison, the class BikePage1 extends StatefulWidget {
@override
_BikePage1State createState() => _BikePage1State();
}
class _BikePage1State extends State<BikePage1>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
print('BikePage1 build'); //called only once
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Text(index.toString());
},
);
}
@override
bool get wantKeepAlive => true;
} |
You're right, AutomaticKeepAliveClientMixin does not execute the build method again on StatefullWidget. I will mark this issue as a feature request |
I think this should not be on the controller side, since it changes how the build method behaves. The view is responsible for this. |
Any update? I wanna keep state between two tab. How to? |
@pqtrong17, use the example I provided above, but do what @lpylpyleo informed, replacing the StatelessWidget of each page with a Statefulwidget with AutomaticKeepAliveClientMixin |
@eduardoflorence I use StatefulWidget and work for me but I wanna use GetView or GetWidget, any solution? |
Hello, I think AutomaticKeepAliveClientMixin is useful. If you add it on GetxController,please notice me.Thanks |
@jy6c9w08 If you made it, you will get the error:
|
@pqtrong17 Thanks for your reply, I know your mean. So,how can I useing getx to keep page state?I want avoid to use StatefulWidget. I see you mark this issue as a feature request.If you realize this feature,Please notice me.Thsnks. |
Any update, please? @eduardoflorence |
???? |
I use StatefulWidget and work for me but I wanna use GetView or GetWidget, any solution? |
@lpylpyleo an alternative to keep the Lisview off set position would be to use PageStorageKey() for every tabBarView or PageView. You can follow this example here. This feature would be more than appreciated, I'm experiencing this issue myself as my App is nested with TabBars. |
@maares Thanks. Now I use a simple TabBarView(
controller: controller.tabController,
children: const [
KeepAliveWrapper(child: CarPage()),
KeepAliveWrapper(child: BikePage()),
],
) My class KeepAliveWrapper extends StatefulWidget {
final Widget child;
const KeepAliveWrapper({Key key, this.child}) : super(key: key);
@override
_KeepAliveWrapperState createState() => _KeepAliveWrapperState();
}
class _KeepAliveWrapperState extends State<KeepAliveWrapper>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return widget.child;
}
@override
bool get wantKeepAlive => true;
} |
Anyone found a solution to keep the ScrollController level please ? |
See above (Stateful) |
I've been using a similar approach to the one provided by @lpylpyleo for a while now, but you should consider passing a closure that returns a |
Any updates? |
3 similar comments
Any updates? |
Any updates? |
Any updates? |
Update on this ? |
I think this should be taken into consideration. |
Nice!!! Update
|
Any updates on AutomaticKeepAliveClientMixin in getx ?? |
+1 for this feature :) |
Update Support for null security class KeepAliveWrapper extends StatefulWidget {
final Widget child;
const KeepAliveWrapper(this.child, {super.key});
@override
State<KeepAliveWrapper> createState() => _KeepAliveWrapperState();
}
class _KeepAliveWrapperState extends State<KeepAliveWrapper>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return widget.child;
}
@override
bool get wantKeepAlive => true;
} |
Try this
Change your existed view from |
Any updates ? |
AutomaticKeepAliveClientMixin
is useful it comes toTabView
orPageView
.Tried to use
TextEditingController
andPageStorageKey
to keep user input andListView
's scroll position. But it will be more convinient if we haveAutomaticKeepAliveClientMixin
.I have noticed that there is a
SingleGetTickerProviderMixin
equivalent toSingleTickerProviderMixin
in Getx.So is it possible to make a mixin like
AutomaticKeepAliveClientMixin
as well or I just don't know the correct usage?Not sure if it is appropriate to use Feature request. Thank you advance.
The text was updated successfully, but these errors were encountered: