Skip to content

Instantly share code, notes, and snippets.

@stansidel
Created Sep 6, 2021
Embed
What would you like to do?
Flutter Cupertino Basic Navigation
import 'package:flutter/cupertino.dart';
void main() {
runApp(CupertinoApp(
home: MyAppHome(), // becomes the route named '/'
routes: <String, WidgetBuilder>{
'/a': (BuildContext context) => const MyPage(title: 'page A'),
'/b': (BuildContext context) => const MyPage(title: 'page B'),
'/c': (BuildContext context) => const MyPage(title: 'page C'),
},
));
}
class MyAppHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
// Uncomment to change the background color
// backgroundColor: CupertinoColors.systemPink,
navigationBar: const CupertinoNavigationBar(
middle: Text('Sample Code'),
),
child: ListView(
children: <Widget>[
CupertinoButton(
child: const Text('Go to A'),
onPressed: () => Navigator.of(context).pushNamed('/a'),
),
CupertinoButton(
child: const Text('Go to B'),
onPressed: () => Navigator.of(context).pushNamed('/b'),
),
CupertinoButton(
child: const Text('Go to C'),
onPressed: () => Navigator.of(context).pushNamed('/c'),
),
],
),
);
}
}
class MyPage extends StatelessWidget {
final String title;
const MyPage({required this.title});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
// Uncomment to change the background color
// backgroundColor: CupertinoColors.systemPink,
navigationBar: CupertinoNavigationBar(
automaticallyImplyLeading: true,
middle: Text(title),
),
child: Center(
child: Text(title),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment