You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Flutter中如何减少抽屉图标旁的空白区域?

Fixing Extra Space Next to Drawer Icon in Flutter Scaffold

Let's break down why you're seeing that unwanted gap next to your drawer icon and walk through actionable fixes:

Root Cause

The extra space comes from a mismatch between Flutter's default Scaffold behavior and your custom app bar setup. When you use a drawer with a custom PreferredSizeWidget (your SearchAppBar), the Scaffold automatically adds a leading drawer icon with built-in padding. Since your SearchAppBar has a custom height (90px) and doesn't account for this auto-generated leading widget's spacing, it creates the awkward gap you're seeing.

Solution 1: Take Control by Adding the Drawer Icon to Your Custom App Bar

Integrate the drawer icon directly into your SearchAppBar to eliminate default padding and align everything perfectly. This lets you define exactly how much space sits between the icon and your search field.

Modify your _SearchAppBarState's build method:

@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>[
        // Custom drawer icon with adjustable spacing
        IconButton(
          icon: Icon(Icons.menu, color: Colors.blue),
          onPressed: () => Scaffold.of(context).openDrawer(),
          padding: EdgeInsets.symmetric(horizontal: 10), // Tweak this for desired spacing
          constraints: BoxConstraints(), // Remove default icon button size constraints
        ),
        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)
      ],
    ),
  );
}

Then, update your Scaffold to disable the auto-generated leading icon:

Scaffold(
  appBar: SearchAppBar(onPatternSelected),
  drawer: AppDrawer(),
  // ... other scaffold properties
)

Solution 2: Wrap Your Custom App Bar in Flutter's Default AppBar

Leverage the built-in AppBar to handle the drawer icon, then embed your SearchAppBar as the title. This lets you fine-tune the leading icon's padding while using Flutter's native layout logic.

Update your Scaffold code:

Scaffold(
  appBar: AppBar(
    leading: IconButton(
      icon: Icon(Icons.menu, color: Colors.blue),
      onPressed: () => Scaffold.of(context).openDrawer(),
      padding: EdgeInsets.zero, // Remove default padding to eliminate the gap
      splashRadius: 20, // Adjust for better touch target behavior
    ),
    title: SearchAppBar(onPatternSelected),
    backgroundColor: Colors.white,
    elevation: 0, // Remove shadow if you don't need it
    toolbarHeight: 90, // Match your custom app bar's height
  ),
  drawer: AppDrawer(),
  // ... other scaffold properties
)

Quick Check: ScreenUtil Consistency

Make sure your ScreenUtil spacing values (like setWidth, setHeight) are consistent across your AppDrawer and SearchAppBar components. Inconsistent scaling can also lead to unexpected gaps or misalignment.


内容的提问来源于stack exchange,提问作者S. M. Asif

火山引擎 最新活动