Skip to content

Instantly share code, notes, and snippets.

@felipecastrosales
Created August 25, 2022 01:45
Show Gist options
  • Save felipecastrosales/d4a28640dfe8bd3d3707fd99535c9fcf to your computer and use it in GitHub Desktop.
Save felipecastrosales/d4a28640dfe8bd3d3707fd99535c9fcf to your computer and use it in GitHub Desktop.
navigator-in-same-screen.dart
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: 'Navigation',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
final controller = PageController();
final list = ["Home","Services", "Work", "About"];
final colors = [Colors.orange, Colors.blue, Colors.red, Colors.green];
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Row(
children: <Widget>[
Container(
width: 50,
height: 50,
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(10),
),
),
const Spacer(),
Row(
children: List.generate(3, (index){
return GestureDetector(
onTap: (){
_scrollToIndex(index);
},
child: Container(
margin: const EdgeInsets.all(8),
child: Text(
list[index+1],
),
),
);
}),
)
],
),
Expanded(
child : PageView(
scrollDirection: Axis.vertical,
pageSnapping: false,
controller: controller,
children: List.generate(list.length, (index){
return Container(
width: MediaQuery.of(context).size.width,
height: double.maxFinite,
color: colors[index],
child: Center(
child: Text(
list[index],
style: const TextStyle(color: Colors.white),
),
),
);
})
),
),
],
)
),
);
}
void _scrollToIndex(int index) {
controller.animateToPage(
index + 1,
duration: const Duration(seconds: 2),
curve: Curves.fastLinearToSlowEaseIn,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment