flutter - 如何减少Flutter中抽屉图标旁边的空白?

标签 flutter dart

在我的 flutter 项目中,我设置了一个自定义抽屉。

这是自定义抽屉的代码-

class AppDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    double defaultScreenWidth = 400.0;
    double defaultScreenHeight = 810.0;

    ScreenUtil.instance = ScreenUtil(
      width: defaultScreenWidth,
      height: defaultScreenHeight,
      allowFontScaling: true,
    )..init(context);
    
    return SizedBox(
      width: MediaQuery.of(context).size.width * 0.70,
      child: Drawer(

        child: Container(
          color: Colors.black87,
          child: ListView(
            padding: EdgeInsets.zero,
            children: <Widget>[

              SizedBox(height: ScreenUtil.instance.setHeight(30),),

              _createDrawerItem(
                  icon: Icons.keyboard_arrow_right,
                  text: 'English to Bangla',
                  onTap: () =>
                      Navigator.pushReplacementNamed(context, Routes.englishToBangla)),

              Padding(
                padding:  EdgeInsets.only(left:ScreenUtil.instance.setWidth(20), right: ScreenUtil.instance.setWidth(20)),
                child: Divider(
                  height: ScreenUtil.instance.setHeight(10),
                  color: Colors.grey,
                ),
              ),


            ],
          ),
        ),
      ),
    );
  }


  Widget _createHeader() {
    return DrawerHeader(
        margin: EdgeInsets.zero,
        padding: EdgeInsets.zero,
        decoration: BoxDecoration(
            image: DecorationImage(
                fit: BoxFit.fill,
                image:  AssetImage('path/to/header_background.png'))),
        child: Stack(children: <Widget>[
          Positioned(
              bottom: 12.0,
              left: 16.0,
              child: Text("Flutter Step-by-Step",
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 20.0,
                      fontWeight: FontWeight.w500))),
        ]));
  }



  Widget _createDrawerItem(
      {IconData icon, String text, GestureTapCallback onTap}) {
    return ListTile(
      title: Padding(
        padding: EdgeInsets.only(left: ScreenUtil.instance.setWidth(10)),
        child: Row(
          children: <Widget>[
            Container(
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.teal
                ),
                child: Icon(icon, color: Colors.white,)
            ),
            Padding(
              padding: EdgeInsets.only(left: ScreenUtil.instance.setWidth(10)),
              child: Text(text, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: ScreenUtil.instance.setSp(14) ),),
            )
          ],
        ),
      ),
      onTap: onTap,
    );
  }

}

这是显示在抽屉图标旁边的工具栏的代码-

class SearchAppBar extends StatefulWidget implements PreferredSizeWidget {
  final PatternCallback onPatternSelected;
  SearchAppBar(this.onPatternSelected, {Key key})
      : preferredSize = Size.fromHeight(90),
        super(key: key);

  @override
  final Size preferredSize; // default is 56.0

  @override
  _SearchAppBarState createState() => _SearchAppBarState();
}

class _SearchAppBarState extends State<SearchAppBar> {
  TextEditingController _searchTextController = TextEditingController();


  @override
  Widget build(BuildContext context) {

    double defaultScreenWidth = 400.0;
    double defaultScreenHeight = 810.0;
    ScreenUtil.instance = ScreenUtil(
      width: defaultScreenWidth,
      height: defaultScreenHeight,
      allowFontScaling: true,
    )..init(context);

    return Container(
      color: Colors.white,

      child: Row(
        children: <Widget>[

          Expanded(
            child: Container(

              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3),

              ),
              child: Theme(
                data:
                Theme.of(context).copyWith(primaryColor: Color(0xFFff9900)),
                child: TextFormField(
                  autofocus: false,
                  style: TextStyle(fontSize: ScreenUtil.instance.setSp(18)),
                  keyboardType: TextInputType.text,
                  controller: _searchTextController,
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    hintText: 'Search for any word you want',
                    hintStyle:
                    TextStyle(fontSize: ScreenUtil.instance.setSp(16)),
                    contentPadding: EdgeInsets.symmetric(
                        vertical: 14,
                        horizontal: 10),
                  ),
                  onChanged: (String value) {
                    widget.onPatternSelected(value);
                  },
                ),
              ),
            ),



          ),


          Container(

              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(0),
                 ),
              child: InkWell(onTap: (){

                if(_searchTextController.text.isNotEmpty) {
                  Navigator.of(context).push(MaterialPageRoute(builder: (context)=>WordDetailScreen(_searchTextController.text.toLowerCase())));
                }

              },
                  child: Icon(Icons.search, color: Colors.blue,))),

          SizedBox(width: 15)
        ],
      ),
    );
  }
}

然后,在我想使用这个抽屉的类中,我在 Scaffold 内部进行了调用,如下所示 -

drawer: AppDrawer()

但问题是这会导致抽屉图标旁边出现空白,如下图所示 -

enter image description here

而且我不知道这个额外的填充或边距是从哪里发生的。因此,我需要一个解决方案来减少抽屉图标旁边的额外空白。

最佳答案

您可以使用Transform.translate将搜索栏向左移动:

@override
Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
            leading: Builder(builder: (context) {
              return IconButton(
                icon: Icon(Icons.menu),
                onPressed: () => Scaffold.of(context).openDrawer(),
              );
            }),
            title: Transform.translate(
                offset: Offset(-30.0, 0.0),
                child: Text('this is the title') // here you can put the search bar
            ),
          ),
      drawer: Drawer(
      ),
    );
}

关于flutter - 如何减少Flutter中抽屉图标旁边的空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63171694/

相关文章:

api - data!= null:必须将非null字符串提供给Text小部件

android - 您如何在保存 Flutter 时在 Android Studio 中自动格式化代码?

ios - 奇怪的错误,有些时候 flutter 自动停止并开始执行

android - Flutter - Flutter 中是否有类似同步适配器的东西

flutter - HTTP 异常 : Connection closed before full header was received

Flutter 我的谷歌地图在发布版本上不工作或不显示

flutter - 使用 persist_bottom_nav_bar 时导航器弹出 - Flutter

google-maps - Google Maps for Flutter - 拖动后获取标记的新位置

json - Dart 从 base64 转换整个 json 树

list - 在 Dart 列表的开头插入元素