Skip to content

Instantly share code, notes, and snippets.

@olaide-ekolere
Last active November 27, 2020 20:36
Show Gist options
  • Save olaide-ekolere/1594bb03ee71751446e23f77ea242986 to your computer and use it in GitHub Desktop.
Save olaide-ekolere/1594bb03ee71751446e23f77ea242986 to your computer and use it in GitHub Desktop.
Navigation -Introduction to flutter
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: NavigationTest(),
);
}
}
class NavigationTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home page"),
),
body: Container(
color: Colors.red,
child: Center(
child: GestureDetector(
onTap: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (_) => PushedWidgetTest()));
},
child: Text(
"Click to push new page on navigation stack",
),
),
),
),
);
}
}
class PushedWidgetTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Pushed Route"),
),
body: Container(
color: Colors.blue,
child: Center(
child: GestureDetector(
onTap: () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (_) => ReplaceWidgetTest(
title: "Push and Replaced Route",
),
),
);
},
child: Text(
"Click to push a replacement",
),
),
),
),
);
}
}
class ReplaceWidgetTest extends StatelessWidget {
final String title;
ReplaceWidgetTest({@required this.title});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(this.title),
),
body: Container(
color: Colors.green,
child: Center(
child: GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Text(
"Click to pop this page",
),
),
),
),
);
}
}
class DynamicWidget extends StatefulWidget {
createState() => _DynamicWidgetState();
}
class _DynamicWidgetState extends State<DynamicWidget>{
Color color = Colors.red;
String text = "Click to change color to green";
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(title: Text("Stateful Widget")),
body: Container(
color: color,
child: Center(
child: GestureDetector(
onTap: () {
setState((){
color = Colors.green;
text = "Color is now green";
});
},
child: Text(
text,
),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment