flutter - 抽屉侧面出现意外的填充物
问题描述
我不确定这是否是由于使用了嵌套脚手架,但我的抽屉在展开时左侧有一个神秘的填充或边距。改变 sizedbox 的宽度也不会改变空白的大小。抽屉也真的很挑剔。
import 'package:flutter/material.dart';
class NewHome extends StatefulWidget {
@override
_NewHomeState createState() => _NewHomeState();
}
class _NewHomeState extends State<NewHome> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("App"),
),
body: Stack(
children: <Widget>[
Align(
alignment: Alignment.centerRight,
child: SizedBox(
width: 285.0,
child: Scaffold(
drawer: Drawer(
child: Container(
color: Theme.of(context).accentColor,
),
),
),
),
),
Container(
color: Theme.of(context).accentColor,
width: 75.0,
child: Column(
children: <Widget>[],
),
),
],
),
);
}
}
截图:
关闭:
https ://i.imgur.com/E4vgoqr.png
解决方案
首先让我解释一下为什么它不能正常工作。首先,它与中心对齐,因此它从中心开始并导致此问题。其次,由于它位于中心并且您使用堆栈,因此您需要找到启动抽屉的确切位置,这就是它表现得不稳定的原因。为了解决它:
class NewHome extends StatefulWidget {
@override
_NewHomeState createState() => _NewHomeState();
}
class _NewHomeState extends State<NewHome> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("App"),
),
body: Stack(
children: <Widget>[
//Deleted the blue box because I don't understand why you need it and it was blocking the swipe zone of the drawer
Container(
width: 285.0,
child: Scaffold(
drawer: Drawer(
child: Container(
color: Theme.of(context).accentColor,
),
),
),
),
],
),
);
}
}
对于解决方案,我从这里玩弄了官方文档
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final appTitle = 'Drawer Demo';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title)),
body: Scaffold(
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the Drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
),
)
);
}
}
推荐阅读
- python - 当我从 excel 文件中获取数据时,我遇到了内存错误
- function - 遇到函数超时后将队列触发自动重试
- python - Ajax 调用的替代方案
- javascript - 如何在 javascript 中使用 audioWorklet 和 AudioWorkletProcessor 录制音频?
- elasticsearch - 我有一个数据文件。我想将该数据插入到弹性 search-kibana 中。有什么自动化的方法可以做到这一点吗?
- node.js - 理解节点 js 中的承诺?
- python - 如何通过opencv不间断地读取视频中的所有帧
- python - 如何从以列为关键的熊猫数据框中创建 json
- vim - 为什么从 Vim 命令行执行反向搜索时需要 ENTER?
- c# - Android杀死了我仍然需要的线程