I’m developing a taxi-hailing app and currently implementing Bloc for state management. While I’ve aimed to utilize stateless widgets where possible, I’ve found that for disposing controllers and building UIs more efficiently, a stateful widget seems necessary. However, I’ve encountered performance issues with the StatefulWidget. Should I persist with its usage, or do you have any alternative recommendations for maintaining performance while still leveraging the benefits of state management with Bloc?
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
late GoogleMapController mapController;
Position? _currentPosition;
Set<Marker> markers = {};
String _placeNameFromCurrentPosition = "";
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
void initState() {
super.initState();
getCurrentPostion();
}
@override
void dispose() {
mapController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
body: Stack(
children: [
GoogleMap(
mapType: MapType.normal,
// polylines: Set<Polyline>.of(polylines.values),
initialCameraPosition: CameraPosition(
target: LatLng(_currentPosition?.latitude ?? 9.005401,
_currentPosition?.longitude ?? 38.763611),
zoom: 14.46,
),
onTap: (argument) {},
onLongPress: (argument) {},
markers: {
Marker(
markerId: MarkerId('marker_2'),
position: LatLng(_currentPosition?.latitude ?? 9.005401,
_currentPosition?.longitude ?? 38.763611),
infoWindow: InfoWindow(
title: 'Marker Title 2',
snippet: 'Marker Snippet 2',
),
icon: BitmapDescriptor.defaultMarkerWithHue(
BitmapDescriptor.hueBlue), // Custom marker icon
),
},
onMapCreated: _onMapCreated,
),
CustomBottomSheetModal(
child: Column(
children: [
GestureDetector(
onTap: () {
AppNavigator.push(Routes.destinationSelection, {
"position": _currentPosition,
"address": _placeNameFromCurrentPosition
});
// sl<SocketManager>().goLive();
},
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: AppTheme.lightGray,
borderRadius: BorderRadius.circular(5.r)),
child: Padding(
padding: EdgeInsets.all(14.r),
child: const Text("Where to?")),
),
),
SizedBox(
height: 5.h,
)
],
))
],
),
);
}
Future<void> getCurrentPostion() async {
try {
_currentPosition = await LocationService().determineCurrentPosition();
await getPlaceNameFromCurrentPosition();
} catch (e, stackTrace) {
showSnackBar(context, e.toString());
LogUtils.logExceptions(e, stackTrace: stackTrace);
}
}
Future<void> getPlaceNameFromCurrentPosition() async {
if (_currentPosition != null) {
_placeNameFromCurrentPosition = await LocationService()
.getAddressFromCoordinates(
_currentPosition!.latitude, _currentPosition!.longitude);
}
}
@override
bool get wantKeepAlive => true;
}
How could I improve this code performance? Any suggestion is appreciated.