Created
August 19, 2022 00:20
-
-
Save austinevick/6a8bf7a029ef08d311e9accabcb40b40 to your computer and use it in GitHub Desktop.
A code snippet to search through a list and return result from another page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
appBarTheme: const AppBarTheme( | |
backgroundColor: Colors.transparent, | |
elevation: 0, | |
)), | |
home: Page1()); | |
} | |
} | |
class Page1 extends StatefulWidget { | |
const Page1({Key? key}) : super(key: key); | |
@override | |
State<Page1> createState() => _Page1State(); | |
} | |
class _Page1State extends State<Page1> { | |
final controller = TextEditingController(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text('Enter name of your country'), | |
const SizedBox(height: 8), | |
TextField( | |
readOnly: true, | |
onTap: () async { | |
final result = await Navigator.of(context) | |
.push(MaterialPageRoute(builder: (ctx) => Page2())); | |
if (result == null) return; | |
setState(() => controller.text = result); | |
}, | |
controller: controller, | |
decoration: const InputDecoration( | |
focusedBorder: OutlineInputBorder(), | |
hintText: 'E.g: Ghana', | |
enabledBorder: OutlineInputBorder()), | |
) | |
], | |
), | |
), | |
); | |
} | |
} | |
class Page2 extends StatefulWidget { | |
const Page2({Key? key}) : super(key: key); | |
@override | |
State<Page2> createState() => _Page2State(); | |
} | |
class _Page2State extends State<Page2> { | |
final controller = TextEditingController(); | |
List<String> countries = [ | |
'Nigeria', | |
'Ghana', | |
'Liberia', | |
'South Africa', | |
'Kenya' | |
]; | |
List<String> searchTerm = []; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
children: [ | |
const SizedBox(height: 35), | |
TextField( | |
onChanged: (val) { | |
setState(() { | |
searchTerm = countries | |
.where((element) => | |
element.toLowerCase().contains(val.toLowerCase())) | |
.toList(); | |
}); | |
}, | |
controller: controller, | |
decoration: const InputDecoration( | |
focusedBorder: OutlineInputBorder(), | |
hintText: 'E.g: Ghana', | |
enabledBorder: OutlineInputBorder()), | |
), | |
Expanded( | |
child: ListView( | |
children: List.generate( | |
searchTerm.length, | |
(i) => ListTile( | |
onTap: () => Navigator.of(context).pop(searchTerm[i]), | |
title: Text(searchTerm[i]), | |
)), | |
)) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment