From 466774899fe86fc38a20da0ae3d7bf8467110f92 Mon Sep 17 00:00:00 2001 From: Sapate Vaibhav <64630274+sapatevaibhav@users.noreply.github.com> Date: Sat, 28 Oct 2023 23:25:09 +0530 Subject: [PATCH] More powerful search bar Now searching cities can be achieved by simply pressing enter button (which can be seen as tick mark in recent android versions). --- lib/activity/home.dart | 44 ++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/activity/home.dart b/lib/activity/home.dart index b917a6f..ab31eea 100644 --- a/lib/activity/home.dart +++ b/lib/activity/home.dart @@ -1,5 +1,3 @@ -// ignore_for_file: prefer_const_constructors, avoid_print - import 'dart:math'; import "package:weather_icons/weather_icons.dart"; import 'package:flutter/material.dart'; @@ -74,23 +72,15 @@ class _HomeState extends State { // Container for search padding: EdgeInsets.symmetric(horizontal: 10), margin: EdgeInsets.symmetric(horizontal: 14, vertical: 30), - decoration: BoxDecoration( - color: Colors.black12, - borderRadius: BorderRadius.circular(25)), + color: Colors.black12, + borderRadius: BorderRadius.circular(25), + ), child: Row( children: [ GestureDetector( onTap: () { - if (searchController.text.replaceAll(" ", "") == "") { - //pass - } else { - Navigator.pushReplacementNamed( context, "/loading", - arguments: { - "searchText": - searchController.text.replaceAll(" ", ""), - }); - } + performSearch(); }, child: Container( margin: EdgeInsets.fromLTRB(5, 0, 10, 0), @@ -98,12 +88,17 @@ class _HomeState extends State { ), ), Expanded( - child: TextField( - controller: searchController, - decoration: InputDecoration( + child: TextField( + controller: searchController, + decoration: InputDecoration( border: InputBorder.none, - hintText: "Search for $city..."), - )) + hintText: "Search for $city...", + ), + onSubmitted: (value) { + performSearch(); + }, + ), + ), ], ), ), @@ -286,8 +281,19 @@ class _HomeState extends State { ), ); } + + void performSearch() { + if (searchController.text.replaceAll(" ", "") == "") { + // Do nothing or display an error message. + } else { + Navigator.pushReplacementNamed(context, "/loading", arguments: { + "searchText": searchController.text.replaceAll(" ", ""), + }); + } + } } + String capitalizeFirstLetter(String input) { if (input.isEmpty) { return input; // Return the input string unchanged if it's empty.