Tell me, I have a page and I need to display dialog
after the page has loaded, but I need it to open itself not on click, but after the page has loaded. I tried adding the showDialog
method to initState
but got an error. How can I display dialog after page load?
dialog
showDialog(
context: context,
builder: (BuildContext context) => const SendEmailDialog(),
);
body
class Body extends StatefulWidget {
const Body({Key? key, this.isUserAuth = false}) : super(key: key);
final bool isUserAuth;
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
bool isMapList = false;
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
final double paddingTop = MediaQuery.of(context).padding.top;
return BlocBuilder<StationCubit, StationState>(
builder: (context, state) {
final StationCubit cubit = BlocProvider.of<StationCubit>(context);
return BlocBuilder<StationMarkerCubit, StationMarkerState>(
builder: (context, stationMarkerState) {
final StationMarkerCubit stationMarkerCubit =
BlocProvider.of<StationMarkerCubit>(context);
return Container(
height: size.height,
width: size.width,
decoration: isMapList
? const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
constants.Colors.blue7xDark,
constants.Colors.blue6xDark,
constants.Colors.blueXxxxxDark,
],
),
)
: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/background/main_background.png'),
fit: BoxFit.cover,
),
),
child:
_child(context, size, cubit, stationMarkerCubit, paddingTop),
);
},
);
},
);
}
Widget _child(BuildContext context, Size size, StationCubit cubit,
StationMarkerCubit stationMarkerCubit, double paddingTop) =>
Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
isMapList
? const SizedBox()
: GestureDetector(
onTap: () {
cubit.selectStation(null);
},
child: MapWidget(
stationCubit: cubit,
stationMarkerCubit: stationMarkerCubit,
isUserAuth: widget.isUserAuth,
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
children: [
SizedBox(
height: paddingTop 24,
),
SizedBox(
height: 45,
child: SearchWidget(
isMapList: isMapList,
isMapListChanged: (value) => setState(() {
isMapList = value;
_onDismissedStationCard(stationMarkerCubit, cubit);
}),
isUserAuth: widget.isUserAuth,
),
),
const SizedBox(
height: 24,
),
isMapList ? const MapListWidget() : const SizedBox(),
isMapList
? const SizedBox(height: 0)
: const SizedBox(height: 100)
],
),
),
CodePudding user response:
You can use addPostFrameCallback
on initState.
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(),
);
});
}
CodePudding user response:
You can use WidgetsBinding.instance?.addPostFrameCallback()
:
void initState() {
// TODO: implement initState
WidgetsBinding.instance?.addPostFrameCallback((_) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Hello'),
content: Text('Hello World'),
));
});
super.initState();
}