Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Drawer Widget - change Scaffold.body content

Tags:

flutter

dart

When using the Flutter Scaffold.Drawer - is it possible to have the links in the Drawer change the content (Scaffold.body) on the page?

Similarly to having links in the navigation drawer change a Fragment in Android.

enter image description here

like image 434
Martin Avatar asked Dec 23 '17 14:12

Martin


1 Answers

Here is a easy and efficient way to do this with Full example

class _MyPageState extends State<MyPage> {
  Widget bodyWidget;

  Widget drawer() {
    return
  }

  Widget bodyFirst() {
    return Container(
        color: Colors.green, child: Center(child: Text("First Page")));
  }

  Widget bodySecond() {
    return Container(
        color: Colors.limeAccent, child: Center(child: Text("Second Page")));
  }

  Widget bodyThird() {
    return Container(
        color: Colors.lightBlueAccent,
        child: Center(child: Text("Third Page")));
  }
  
  @override
  void initState() {
    super.initState();
    bodyWidget = bodyFirst();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: drawer(),
      appBar: AppBar(title: Text("My App")),
      body: bodyWidget,
    );
  }
}

enter image description here

like image 75
Paresh Mangukiya Avatar answered Nov 03 '22 22:11

Paresh Mangukiya