Skip to content

Instantly share code, notes, and snippets.

@MariaMelnik
Created September 26, 2018 14:34
Show Gist options
  • Save MariaMelnik/6df9b2837515a906ddd3d862508562f1 to your computer and use it in GitHub Desktop.
Save MariaMelnik/6df9b2837515a906ddd3d862508562f1 to your computer and use it in GitHub Desktop.
flutter navigation rebuild for stateless widgets
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
void _goToNewScreen(BuildContext context) {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => Page2()));
}
@override
Widget build(BuildContext context) {
print("build");
return new Scaffold(
appBar: new AppBar(
title: new Text(title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'Simple text',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: () => _goToNewScreen(context),
tooltip: 'GoTo2',
child: new Icon(Icons.navigate_next),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("build page2");
return Scaffold(
appBar: AppBar(
title: Text("page2"),
),
body: Text("new page2"),
floatingActionButton: FloatingActionButton(
onPressed: () => _goToNewScreen2(context),
tooltip: 'GoTo3',
child: new Icon(Icons.navigate_next),
),
);
}
void _goToNewScreen2(BuildContext context) {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
print("build page3");
return Scaffold(
appBar: AppBar(
title: Text("page3"),
),
body: Text("new page3"),
);
}
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment