Skip to content

Instantly share code, notes, and snippets.

@kelvinwieth
Last active February 4, 2025 11:08
Show Gist options
  • Save kelvinwieth/ba12c6267cc732c2d49287136c9ac3d9 to your computer and use it in GitHub Desktop.
Save kelvinwieth/ba12c6267cc732c2d49287136c9ac3d9 to your computer and use it in GitHub Desktop.
Flutter navigation with then
import 'package:flutter/material.dart';
void main() => runApp(const App());
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: LoginScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: const Center(
child: Text('Home Screen'),
),
);
}
}
class LoginScreen extends StatelessWidget {
const LoginScreen({super.key});
Future<bool> login() async {
print('Login...');
await Future.delayed(const Duration(seconds: 1));
return true;
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
login().then((result) {
print('Login result: $result');
if (result) {
print('Navigating to home');
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const HomeScreen()),
);
}
});
},
child: const Icon(Icons.login),
),
body: const Center(
child: Text('Login Screen'),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment