I am finding it difficult to add a drawer in the latest version of flutter. My code looks like this
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: buildAppbar(),
body: const Body(),
drawer: DrawerSlide(),
);
}
AppBar buildAppbar() {
return AppBar(
elevation: 3,
leading: GestureDetector(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: SvgPicture.asset('assets/icons/menu.svg', width: 10, height: 10, fit: BoxFit.fitWidth,),
),
onTap: () => Scaffold.of(context).openDrawer(),
),
);
}
}
There is an error shown in Scaffold.of(context).openDrawe() that the context is not defined. It was working till I updated my flutter but in new flutter I think it is not working.
CodePudding user response:
I think the buildAppbar
method requires context
as a positional argument :
AppBar buildAppbar(BuildContext context) {
return AppBar(
elevation: 3,
leading: GestureDetector(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: SvgPicture.asset('assets/icons/menu.svg', width: 10, height: 10, fit: BoxFit.fitWidth,),
),
onTap: () => Scaffold.of(context).openDrawer(),
),
);
}
use it like :
return Scaffold(
appBar: buildAppbar(context),
body: const Body(),
drawer: DrawerSlide(),
);