Skip to content

Commit

Permalink
More powerful search bar
Browse files Browse the repository at this point in the history
Now searching cities can be achieved by simply pressing enter button (which can be seen as tick mark in recent android versions).
  • Loading branch information
sapatevaibhav authored Oct 28, 2023
1 parent 13a2171 commit 4667748
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions lib/activity/home.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -74,36 +72,33 @@ class _HomeState extends State<Home> {
// 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),
child: Icon(Icons.search),
),
),
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();
},
),
),
],
),
),
Expand Down Expand Up @@ -286,8 +281,19 @@ class _HomeState extends State<Home> {
),
);
}

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.
Expand Down

0 comments on commit 4667748

Please sign in to comment.