Skip to content

Instantly share code, notes, and snippets.

@xeecos
Created August 21, 2024 07:56
Show Gist options
  • Select an option

  • Save xeecos/3147f84fbb0449a177f019e0f2a48723 to your computer and use it in GitHub Desktop.

Select an option

Save xeecos/3147f84fbb0449a177f019e0f2a48723 to your computer and use it in GitHub Desktop.
flutter tab bar and push pop page example
import 'package:flutter/material.dart';
import 'pages.dart';
void main() {
runApp(const CPMain());
}
// ignore: must_be_immutable
class CPMain extends StatefulWidget {
const CPMain({super.key});
@override
State<CPMain> createState() => _CPMain();
}
class _CPMain extends State<CPMain> {
final PageController _controller = PageController(initialPage: 0);
int _pageIndex = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
bottomNavigationBar: BottomNavigationBar(
currentIndex: _pageIndex,
onTap: (int page) {
setState(() {
_pageIndex = page;
});
_controller.jumpToPage(page);
debugPrint('movieTitle: $page');
},
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), label: '今日'),
BottomNavigationBarItem(
icon: Icon(Icons.business), label: '菜谱'),
BottomNavigationBarItem(
icon: Icon(Icons.school), label: '营养'),
]),
body: PageView(
controller: _controller,
physics: const NeverScrollableScrollPhysics(),
children: const [
Text(
'1',
),
FirstRoute(),
Text(
"3",
),
],
)));
}
}
import 'package:flutter/material.dart';
class FirstRoute extends StatelessWidget {
const FirstRoute({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Page1')),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (context) => const SecondRoute())),
child: const Text('Go to Page2'),
),
),
);
}
}
class SecondRoute extends StatelessWidget {
const SecondRoute({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Second Route'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Go back!'),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment