Je veux ouvrir Drawer
programmatiquement pas en le faisant glisser, comment désactiver cette fonctionnalité coulissante (fonctionnalité tactile du tiroir)
Réponses
Trop de publicités?
CopsOnRoad
Points
4705
Code NULL Safe
-
Utilisation
GlobalKey
:final GlobalKey<ScaffoldState> _key = GlobalKey(); // Create a key @override Widget build(BuildContext context) { return Scaffold( key: _key, // Assign the key to Scaffold. drawer: Drawer(), floatingActionButton: FloatingActionButton( onPressed: () => _drawerKey.currentState!.openDrawer(), // <-- Opens drawer ), ); }
-
Utilisation
Builder
:@override Widget build(BuildContext context) { return Scaffold( drawer: Drawer(), floatingActionButton: Builder(builder: (context) { return FloatingActionButton( onPressed: () => Scaffold.of(context).openDrawer(), // <-- Opens drawer. ); }), ); }
Gagandeep Gambhir
Points
546
Voici un autre exemple d'ouverture programmatique du tiroir à partir d'une icône de hamburger et sans la barre d'application :-
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
var scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
key: scaffoldKey,
drawer: new Drawer(
child: new ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blue,
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
//Do some stuff here
//Closing programmatically - very less practical use
scaffoldKey.currentState.openEndDrawer();
},
)
],
),
),
body: Stack(
children: <Widget>[
new Center(
child: new Column(
children: <Widget>[],
)),
Positioned(
left: 10,
top: 20,
child: IconButton(
icon: Icon(Icons.menu),
onPressed: () => scaffoldKey.currentState.openDrawer(),
),
),
],
),
),
);
}
}
Krolaw
Points
149
L'appel de Scaffold.of ne fonctionne pas car le contexte ne contient pas l'échafaudage. Certaines solutions ci-dessus l'ont ignoré, d'autres ont utilisé GlobalKey. Je crois que la solution la plus propre est d'envelopper le bouton dans un Builder :
Scaffold(
drawerEnableOpenDragGesture: false, // Prevent user sliding open
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text("Some Title"),
actions: [
Builder(builder: (context) => // Ensure Scaffold is in context
IconButton(
icon: Icon(Icons.settings),
onPressed: () => Scaffold.of(context).openDrawer()
)),
],
),
// TODO ...
)
Hardik Kumbhani
Points
595
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(
"Infilon Technologies",
style:
TextStyle(fontFamily: "Poppins", fontWeight: FontWeight.w600),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.menu),
onPressed: () {
if (_scaffoldKey.currentState.isEndDrawerOpen) {
_scaffoldKey.currentState.openDrawer();
} else {
_scaffoldKey.currentState.openEndDrawer();
}
},
),
],
),