'Flutter Search Delegate Taking Full Width

I made a master-detail type view app using flutter, running on flutter desktop. I implemented the SearchDelegate on appBar, but it takes the full width of the app.

Here are some screenshots.

enter image description here

When the search is active enter image description here

Here is some code:

main_page.dart

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LayoutBuilder(
        builder: (context, constraints){
          if(!(constraints.maxWidth > 768)){
              return MasterTab();
          }
          return Row(
            children: [
              Container(
                width: constraints.maxWidth / 4,
                child: MasterTab(),
              ),
              Expanded(
                child: DetailTab(),
              )
            ],
          );
        },
      ),
    );
  }
}

master_tab.dart (left side, which is I want the search view to show)

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Primary"),
        actions: [
          IconButton(
              onPressed: () {
                showSearch(context: context, delegate: SearchView());
              },
              icon: Icon(Icons.search))
        ],
      ),
      body: ListView.builder(
        itemCount: _notes.length,
        itemBuilder: (context, index){
          return ItemNote(noteEntity: _notes[index]);
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _onAddNoteClick,
        child: Icon(Icons.create),
      ),
    );
  }

detail_tab.dart (right side, the red)

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Align(
            alignment: Alignment.topRight,
            child: DetailAction(),
          ),
          Expanded(child: Container(
            color: Colors.red,

          ))
        ],
      )
    );
  }

search_delegate.dart

class SearchView extends SearchDelegate {
  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(onPressed: () {
        query = "";
      }, icon: Icon(Icons.close))
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(onPressed: () {
      Navigator.pop(context);
    }, icon: Icon(Icons.arrow_back));
  }

  @override
  Widget buildResults(BuildContext context) {
    return Container(
      color: Colors.amber,
      child: Center(
        child: Text("Search query"),
      ),
    );
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Container(
      child: Center(
        child: Text("Suggestion"),
      ),
    );
  }
  
}

I want to make the search view only works on the left side only (not taking the fullwidth), how to achieve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source