Dart 如何使用没有脚手架的抽屉?
我注意到Scaffold.Drawer只有在有Scaffold的AppBar时才会出现 但我使用了BottomNavigationBar中的BottomAppBar,而不是AppBar 如何让抽屉使用BottomAppBar? 下面是我的代码,抽屉没有出现Dart 如何使用没有脚手架的抽屉?,dart,flutter,Dart,Flutter,我注意到Scaffold.Drawer只有在有Scaffold的AppBar时才会出现 但我使用了BottomNavigationBar中的BottomAppBar,而不是AppBar 如何让抽屉使用BottomAppBar? 下面是我的代码,抽屉没有出现 class homieclass extends State<homie>{ 类homieclass扩展状态{ @凌驾 小部件构建(构建上下文){ 返回材料PP( debugShowCheckedModeBanner:fals
class homieclass extends State<homie>{
类homieclass扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
家:新脚手架(
backgroundColor:Colors.white70.不透明度(0.9),
floatingActionButtonLocation:floatingActionButtonLocation.centerDocked,
floatingActionButton:floatingActionButton(按下时:(){},背景色:Colors.redAccent,子项:图像图标(新资产估计(“ast/hello123.png”),),
bottomNavigationBar:BottomAppBar(子项:行)(
mainAxisAlignment:mainAxisAlignment.spaceAround,mainAxisSize:mainAxisSize.max,子项:[
图标按钮(图标:图标(Icons.menu),按下时:(){}),图标按钮(图标:图标(Icons.message),按下时:(){}),
],
),
),
正文:新栏目(
儿童:[新尺寸盒子(高度:50.0,),
容器(边距:EdgeInsets.only(左:0.0),子级:新文本(“事件”,textAlign:textAlign.left,样式:TextStyle(fontFamily:'ssfr',fontSize:35.0,fontWeight:fontWeight.bold),),)
,容器(边距:仅限边集(左:10.0,右:10.0),宽度:360.0,高度:40.0,装饰:新框装饰(颜色:颜色:蓝灰色,不透明度为0.2),
边框:新边框。全部(颜色:颜色。蓝灰色。不透明度(0.0),宽度:2.0),
borderRadius:new borderRadius.circular(10.0),、子项:新行(子项:[大小框(宽度:10.0),)、图标(图标。搜索,颜色:颜色。蓝灰色。不透明度(0.9),),文本(“搜索”,样式:TextStyle(fontFamily:'ssft',颜色:颜色。蓝灰色,fontSize:20.0),),)
,新尺寸箱(高度:10.0,),新尺寸箱(
身高:5.0,
孩子:新中心(
子容器:新容器(
边距:新边仅限方向(起点:1.0,终点:1.0),
身高:2.0
,
颜色:颜色。红色调。不透明度(0.8),
),
),
),],
),抽屉:新抽屉(
子:新列表视图(
children:[listile(标题:Text(“hello”),)],
),
),
),
);
}它对我来说非常有效。下面是一个工作示例,在底部栏中有一个专用的“显示抽屉”按钮(抽屉也可以从左侧拖动):
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“飞舞游乐场”,
主页:TestPage(),
);
}
}
类TestPage扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
子项:文本(“正文”),
),
底部导航栏:生成器(生成器:(BuildContext上下文){
返回底部AppBar(
颜色:颜色。橙色,
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceAround,
mainAxisSize:mainAxisSize.max,
儿童:[
图标按钮(图标:图标(Icons.menu),ON按下:(){
Scaffold.of(context.openDrawer();;
}),
图标按钮(icon:icon(Icons.message),按下:(){}),
],
),
);
},),
抽屉(
儿童:安全区(
右:错,
儿童:中心(
子项:文本(“抽屉内容”),
),
),
),
);
}
}
颤振版本:最新的主版本(尽管我也很确定它与测试版一起工作)您可以使用抽屉,但您必须提供一个
DrawerController
,并安排抽屉覆盖您的其他内容。使用堆栈
很容易做到这一点。堆栈中必须有一个不透明的容器,否则在绘图滑入滑出时会出现渲染瑕疵。Scaffold不需要然而,令人烦恼的是,当绘图移动时,它也会重建其他内容(这正是他们试图避免的类型)
import'package:flatter/signatures.dart';
进口“包装:颤振/材料.省道”;
导入“package:flatterui/util/layout_util.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文)=>
MaterialApp(标题:“颤振运动场”,
家:材料(孩子:抽屉钉(主体:主体(),抽屉:_抽屉());
}
抽屉_Drawer()=>抽屉(
儿童:安全区(
儿童:中心(
子:列(
儿童:[
文本(“收尾抽屉内容”),
生成器(生成器:(上下文)=>RaisedButton(
子项:文本('Click',语义标签:'Click 2'),
已按下:(){
Navigator.pop(上下文);
},
)),
],
),
),
),
);
Widget body()=>容器(
装饰:框装饰(颜色:color.fromARGB(255,255,255,255)),
儿童:安全区(
儿童:中心(
子项:列(子项:[
Text('Body'),//样式:TextStyle(fontSize:14.0,颜色:Colors.black)),
生成器(生成器:(上下文)=>RaisedButton(
子项:文本(“打开的抽屉”),
已按下:(){
(context.ancestorWidgetOfExactType(DrawerStack)作为DrawerStack.openDrawer();
//DrawerStack.of(context.openDrawer();
})),
]))));
类DrawerStack扩展了无状态小部件{
最终的全球银行=
GlobalKey();
最终抽屉颜色=颜色。来自argb(90100100128);
最终双抽屉redgedragwidth=null;
最终DragStartBehavior DragStartBehavior=DragStartBehavior.start;
最终窗口小部件主体;
最终出票人;
DrawerStack({Key-Key,this.body,this.drawer}):super(Key:Key);
void openDrawer(){
backgroundColor: Colors.white70.withOpacity(0.9),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(onPressed: (){},backgroundColor: Colors.redAccent,child: ImageIcon(new AssetImage("ast/hello123.png")),),
bottomNavigationBar: BottomAppBar(child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,mainAxisSize: MainAxisSize.max,children: <Widget>[
IconButton(icon: Icon(Icons.menu), onPressed: (){}),IconButton(icon: Icon(Icons.message), onPressed: (){}),
],
),
),
body: new Column(
children: <Widget>[new SizedBox(height: 50.0, ),
Container(margin: EdgeInsets.only(left: 0.0),child: new Text("Events",textAlign: TextAlign.left,style: TextStyle(fontFamily: 'ssfr',fontSize: 35.0,fontWeight: FontWeight.bold),),)
, Container(margin: EdgeInsets.only(left: 10.0,right: 10.0) ,width: 360.0,height: 40.0,decoration: new BoxDecoration(color: Colors.blueGrey.withOpacity(0.2),
border: new Border.all(color: Colors.blueGrey.withOpacity(0.0), width: 2.0),
borderRadius: new BorderRadius.circular(10.0),),child: new Row(children: <Widget>[SizedBox(width: 10.0,),Icon(Icons.search,color: Colors.blueGrey.withOpacity(0.9),),Text(" Search",style: TextStyle(fontFamily: 'ssft',color: Colors.blueGrey,fontSize: 20.0),)],),)
,new SizedBox(height: 10.0,),new SizedBox(
height: 5.0,
child: new Center(
child: new Container(
margin: new EdgeInsetsDirectional.only(start: 1.0, end: 1.0),
height: 2.0
,
color: Colors.redAccent.withOpacity(0.8),
),
),
),],
),drawer: new Drawer(
child: new ListView(
children: <Widget>[ListTile(title: Text("hello"),)],
),
),
),
);
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Playground',
home: TestPage(),
);
}
}
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Body'),
),
bottomNavigationBar: Builder(builder: (BuildContext context) {
return BottomAppBar(
color: Colors.orange,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
IconButton(icon: Icon(Icons.menu), onPressed: () {
Scaffold.of(context).openDrawer();;
}),
IconButton(icon: Icon(Icons.message), onPressed: () {}),
],
),
);
},),
drawer: Drawer(
child: SafeArea(
right: false,
child: Center(
child: Text('Drawer content'),
),
),
),
);
}
}
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutterui/util/layout_util.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) =>
MaterialApp(title: 'Flutter Playground',
home: Material(child:DrawerStack(body: body(), drawer: _drawer())));
}
Drawer _drawer() =>Drawer(
child: SafeArea(
child: Center(
child: Column(
children: [
Text('endDrawer content'),
Builder(builder:(context) => RaisedButton(
child: Text('Click', semanticsLabel: 'Click 2'),
onPressed: () {
Navigator.pop(context);
},
)),
],
),
),
),
);
Widget body() => Container(
decoration: BoxDecoration(color: Color.fromARGB(255, 255, 255, 255)),
child: SafeArea(
child: Center(
child: Column(children: [
Text('Body'), // style:TextStyle(fontSize: 14.0,color: Colors.black)),
Builder(builder:(context) => RaisedButton(
child: Text('Open drawer'),
onPressed: () {
(context.ancestorWidgetOfExactType(DrawerStack) as DrawerStack).openDrawer();
// DrawerStack.of(context).openDrawer();
})),
]))));
class DrawerStack extends StatelessWidget {
final GlobalKey<DrawerControllerState> _drawerKey =
GlobalKey<DrawerControllerState>();
final drawerScrimColor = Color.fromARGB(90, 100, 100, 128);
final double drawerEdgeDragWidth = null;
final DragStartBehavior drawerDragStartBehavior = DragStartBehavior.start;
final Widget body;
final Drawer drawer;
DrawerStack({Key key, this.body, this.drawer}) : super(key: key);
void openDrawer() {
_drawerKey.currentState?.open();
}
@override
Widget build(BuildContext context) => Stack(
children: [
// body
body,
DrawerController(
key: _drawerKey,
alignment: DrawerAlignment.end,
child: drawer,
drawerCallback: (_){},
dragStartBehavior: drawerDragStartBehavior,
//widget.drawerDragStartBehavior,
scrimColor: drawerScrimColor,
edgeDragWidth: drawerEdgeDragWidth,
),
],
);
}
bottomNavigationBar:BottomAppBar(
elevation: 10,
shape: CircularNotchedRectangle(),
child: Container(
height: 80,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: GestureDetector(
onTap: () {
Scaffold.of(context).openDrawer();
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.dashboard),
Text(
'DASHBOARD',
style: TextStyle(color: Colors.black),
),
],
),
),
),
Expanded(
child: GestureDetector(
onTap: () {},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.home),
Text(
'CHALLENGES',
style: TextStyle(color: Colors.black),
),
],
),
),
),
Expanded(
child: GestureDetector(
onTap: (){},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.public),
Text(
'Public',
style: TextStyle(color: Colors.black),
),
],
),
),
),
Expanded(
child: GestureDetector(
onTap: () {},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.sentiment_satisfied),
Text(
'Settings',
style: TextStyle(color: Colors.black),
),
],
),
),
),
],
),
),
color: AppColors.WHITE,
);
class BottomBar {
final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
bottomNavigationBar: BottomAppBar(
elevation: 10,
shape: CircularNotchedRectangle(),
child: Container(
height: 80,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: GestureDetector(
onTap: () {
scaffoldKey.currentState.openDrawer();
// scaffoldKey.currentState.openEndDrawer(); // use to open end drawer
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.dashboard),
Text(
'DASHBOARD',
style: TextStyle(color: Colors.black),
),
],
),
),
),
Expanded(
child: GestureDetector(
onTap: () {},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.home),
Text(
'CHALLENGES',
style: TextStyle(color: Colors.black),
),
],
),
),
),
Expanded(
child: GestureDetector(
onTap: () {},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.public),
Text(
'Public',
style: TextStyle(color: Colors.black),
),
],
),
),
),
Expanded(
child: GestureDetector(
onTap: () {},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.sentiment_satisfied),
Text(
'Settings',
style: TextStyle(color: Colors.black),
),
],
),
),
),
],
),
),
color: AppColors.WHITE,
));
}
}