Flutter:有没有一种方法可以在Flutter中更改SearchDelegate搜索栏的高度?

4
我正在使用Flutter编写一个应用程序,我在整个应用程序中使用自定义的AppBar(我的项目AppBar)来设置自定义高度,但是我需要在其中创建一个搜索函数,我正在使用SearchDelegate,它已经具有可定制的SearchBar(Flutter SearchBar),但我找不到一种方法来自定义其高度而不改变Flutter代码。以下是我的SearchDelegate代码:
class ProjectSearch extends SearchDelegate {
  @override
  get searchFieldStyle => TextStyle();

  @override
  get searchFieldLabel => 'Pesquisar';

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(
          Icons.clear,
          size: 24.h,
        ), 
        onPressed: () {
          query = '';
        }
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: AnimatedIcon(
        icon: AnimatedIcons.menu_arrow,
        progress: transitionAnimation,
      ),
      onPressed: () {
        close(context, null);
      }
    );
  }

  @override
  // ignore: missing_return
  Widget buildResults(BuildContext context) {
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return FutureBuilder(
      future: ProjectsRepository().fetchAllProjects(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        List<Project> projects = List<Project>();
        if (snapshot.hasData) {
          projects = snapshot.data;
        } else {
          return Center(
            child:CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation(Theme.of(context).colorScheme.secondary),
            )
          );
        }

        projects = projects.where((p) => p.name.toLowerCase().startsWith(query.toLowerCase())).toList();

        return ListView.builder(
          itemBuilder: (context, index) => ListTile(
            leading: Icon(Icons.location_city_outlined),
            title: RichText(
              text: TextSpan(
                text: projects[index].name.substring(0, query.length),
                style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.bold
                ),
                children: [
                  TextSpan(
                    text: projects[index].name.substring(query.length),
                    style: TextStyle(
                      color: Colors.grey,
                    )
                  )
                ]
              )
            ),
            onTap: () async {
              
            },
          ),
          itemCount: projects.length,
        );
      
      });
  }
}
1个回答

2
你可以通过覆盖SearchDelegate类的appBarTheme方法来实现:
  @override
  ThemeData appBarTheme(BuildContext context) {
    final theme = Theme.of(context);
    return theme.copyWith(
      appBarTheme: theme.appBarTheme.copyWith(toolbarHeight: YOUR_HEIGHT),
    );
  }

如果您想为应用程序中所有的高度都定义一次,您可以MaterialApp构造函数提供ThemeData。例如:
MaterialApp(
  ...
  theme: ThemeData(
    ...
    appBarTheme: const AppBarTheme(
      toolbarHeight: kToolbarHeight + 16,
    ),
  ),
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接