Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Created April 14, 2018 19:06
Show Gist options
  • Save Blasanka/addcfeacb78ca24e0f0503f22b064368 to your computer and use it in GitHub Desktop.
Save Blasanka/addcfeacb78ca24e0f0503f22b064368 to your computer and use it in GitHub Desktop.
If you want complete demonstration about this code please go to slcoderlk.blogspot.com to get full understanding. This code example show you how to change the content of the page without going to a new page(new route) like fragment in android SDK. This snippet used a drawer to change context but you can also use anything rather than a drawer.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Demo',
theme: new ThemeData(
primarySwatch: Colors.green,
),
home: new HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new HomePageState();
}
}
class HomePageState extends State<HomePage> {
int _index = 0;
List<Text> _texts = [
new Text('card 1'),
new Text('card 2')
];
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: _texts[_index],
),
drawer: new Drawer(
child: new SafeArea(
child: Column(
children: <Widget>[
new Column(
children: [
new ListTile(
title: _texts[0],
selected: _index == 0,
onTap: () {
setState(() => _index = 0);
Navigator.of(context).pop();
},
),
new ListTile(
title: _texts[1],
selected: _index == 1,
onTap: () {
setState(() => _index = 1);
Navigator.of(context).pop();
},
),
],
),
],
),
),
),
body: new Center(
child: _texts[_index],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment