Home > OS >  Can I use one class many time just chainging Scaffold.Drawer.body?
Can I use one class many time just chainging Scaffold.Drawer.body?

Time:08-03

I want to use class contain Scaffold.drawer like reusable class. I searched everyday but I can't find solution I can enough understand to apply.

Concleatly, I want to change class belong to body (part of Chart() in my code). hmmm I think It's like variable. What can I do for it?

 return Scaffold (
  appBar: AppBar(title: const Text("")),
  body: Chart(),
  drawer: Drawer(
    child: ListView(
        padding: EdgeInsets.zero,
        children: [
          const DrawerHeader(
            decoration: BoxDecoration(
              color: Colors.blue,
            ),
            child: Text(""),
          ),
          ListTile(
            title: const Text('#'),
            onTap:(){
              Navigator.push(
                  context, MaterialPageRoute(builder: (_) => ChartPage()));
            },
          ),
          ListTile(
            title: const Text('##'),
            onTap:(){
              Navigator.push(
                  context, MaterialPageRoute(builder: (_) => NoticeBoard()));
            },
          ),
          ListTile(
            title: const Text('###'),
            onTap:(){},
          ),
        ]
    ),
  ),
);

Thank you.

CodePudding user response:

You can create a new class named CustomDrawer.dart then add

import 'package:flutter/material.dart';
 

class CustomDrawer extends StatelessWidget {

  const CustomDrawer({Key? key}) : super(key: key);
 

  @override

  Widget build(BuildContext context) {

    return Drawer(
    child: ListView(
        padding: EdgeInsets.zero,
        children: [
          const DrawerHeader(
            decoration: BoxDecoration(
              color: Colors.blue,
            ),
            child: Text(""),
          ),
          ListTile(
            title: const Text('#'),
            onTap:(){
              Navigator.push(
                  context, MaterialPageRoute(builder: (_) => ChartPage()));
            },
          ),
          ListTile(
            title: const Text('##'),
            onTap:(){
              Navigator.push(
                  context, MaterialPageRoute(builder: (_) => NoticeBoard()));
            },
          ),
          ListTile(
            title: const Text('###'),
            onTap:(){},
          ),
        ]
    ),
  );
  }
}

Then in all other classes you only need to use

Scaffold(
  drawer: CustomDrawer()
)

CodePudding user response:

yes it's possible you can create your customDrawer widget with static keyword and just use this widget any where you want like this..

class CustomDrawer {
  static Widget customDrawer(BuildContext context) {
    return Drawer(
      child: ListView(padding: EdgeInsets.zero, children: [
        DrawerHeader(
          decoration: BoxDecoration(
            color: Colors.blue,
          ),
          child: Text(""),
        ),
        ListTile(
          title: const Text('#'),
          onTap: () {
            // Navigator.push(
            //     context, MaterialPageRoute(builder: (_) => ChartPage()));
          },
        ),
        ListTile(
          title: const Text('##'),
          onTap: () {
            // Navigator.push(
            //     context, MaterialPageRoute(builder: (_) => NoticeBoard()));
          },
        ),
        ListTile(
          title: const Text('###'),
          onTap: () {},
        ),
      ]),
    );
  }
}

and then other classes you only need call this widget with class name like this just like a variable..

return Scaffold(
      drawer: CustomDrawer.customDrawer(context),
      appBar: AppBar(
        title: Text("Custom drawer screen"),
      ),
    );
  • Related