Skip to content

Instantly share code, notes, and snippets.

@lslv1243
Created February 11, 2022 01:40
Show Gist options
  • Save lslv1243/2b1004e92494fffeb7e167e7e057c3cb to your computer and use it in GitHub Desktop.
Save lslv1243/2b1004e92494fffeb7e167e7e057c3cb to your computer and use it in GitHub Desktop.
ModalRoute didPop breaks in flutter 2.10.1 and used to work in version 2.5.3
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const HomePage(title: 'Flutter Demo Home Page'),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
void _showOtherPage() async {
final result = await Navigator.of(context).push(
RawDialogRoute(
// it breaks when popping if transition duration is zero
// it breaks on next push if transition duration is not zero
transitionDuration: Duration.zero,
pageBuilder: (context, _, __) => const OtherPage(),
),
);
print(result);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
child: const Text('show other page'),
onPressed: _showOtherPage,
),
),
);
}
}
class OtherPage extends StatefulWidget {
const OtherPage({Key? key}) : super(key: key);
@override
State<OtherPage> createState() => _OtherPageState();
}
class _OtherPageState extends State<OtherPage> {
void _breakIt() async {
const someResult = 'some result';
// this does not break in version 2.5.3
// this does break in version 2.10.1
ModalRoute.of(context)!.didPop(someResult);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
child: const Text('break it'),
onPressed: _breakIt,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment