Flutter: Routing and Navigator
Mobile apps typically display full-screen elements called “screens” or “pages”. In Flutter these elements are called routes and they’re managed by a Navigator widget.
METHOD 1
PLEASE DEFINE ALSO widget_test.dart
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame. await tester.pumpWidget(HomePage());
// Verify that our counter starts at 0. expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
// Tap the '+' icon and trigger a frame. await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented. expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
});
}
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: HomePage(),
));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: new Center(
child: RaisedButton(
onPressed: () {
Route route = MaterialPageRoute(builder: (context) => SecondHome());
Navigator.push(context, route);
},
child: Text('Second Home Welcome'),
),
),
);
}
}
class SecondHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Hoem'),
),
body: new Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go Back'),
),
),
);
}
}
void main() {
runApp(MaterialApp(
home: HomePage(),
));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: new Center(
child: RaisedButton(
onPressed: () {
Route route = MaterialPageRoute(builder: (context) => SecondHome());
Navigator.push(context, route);
},
child: Text('Second Home Welcome'),
),
),
);
}
}
class SecondHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Hoem'),
),
body: new Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go Back'),
),
),
);
}
}
METHOD 2
// Copyright 2018 The Flutter team. 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(MaterialApp( initialRoute: '/', routes: <String, WidgetBuilder>{ '/': (context) => HomePage(), '/second': (context) => SecondHome(), }, )); } class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home'), ), body: new Center( child: RaisedButton( onPressed: () { /* Route route = MaterialPageRoute(builder: (context) => SecondHome()); //MaterialPageRoute is responsible for page Transition Navigator.push(context, route); //Navigator maintain Stack-based history of routes*/ Navigator.pushNamed(context, '/second'); }, child: Text('Second Home'), ), ), ); } } class SecondHome extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Second Hoem'), ), body: new Center( child: RaisedButton( onPressed: () { Navigator.pop(context); }, child: Text('Go Back'), ), ), ); } }
Post a Comment
0Comments