my_search_plugin 0.0.1
my_search_plugin: ^0.0.1 copied to clipboard
A new Flutter project.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:my_search_plugin/my_search_plugin.dart';
import 'package:my_search_plugin/model.dart';
void main() => runApp(const MyMaterialApp());
class MyMaterialApp extends StatelessWidget {
const MyMaterialApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'My Search App',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 1,
child: Scaffold(
appBar: AppBar(
title: const Text("My Search App"),
),
body: const MyHomepage()),
);
}
}
class MyHomepage extends StatefulWidget {
const MyHomepage({Key? key}) : super(key: key);
@override
_MyHomepageState createState() => _MyHomepageState();
}
class _MyHomepageState extends State<MyHomepage> {
Future<List<SearchResultItem>>? _futureSearchResultItem;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(22.0, 22.0, 22.0, 8),
child: Column(
children: <Widget>[
const SizedBox(
height: 10.0,
),
MySearchField(
textFieldConfiguration: TextFieldConfiguration(
autofocus: true,
style: DefaultTextStyle
.of(context)
.style
.copyWith(fontStyle: FontStyle.italic),
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'What are you looking for?'),
),
onSearchResultFound: (List<SearchResultItem> searchResult) {
_futureSearchResultItem =
Future<List<SearchResultItem>>.value(searchResult);
setState(() {});
},
),
const SizedBox(
height: 10.0,
),
(_futureSearchResultItem == null)
? const Text("Search Anything")
: Flexible(
child: Container(
child: buildFutureBuilder(),
))
],
),
);
}
FutureBuilder<List<SearchResultItem>> buildFutureBuilder() {
return FutureBuilder<List<SearchResultItem>>(
future: _futureSearchResultItem,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
SearchResultItem project = snapshot.data![index];
return Container(
padding: const EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 12.0),
color: Colors.grey[100],
child: Column(children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Text(
project.service_id,
style: const TextStyle(
fontWeight: FontWeight.normal, color: Colors.grey,fontSize: 12),
textAlign: TextAlign.left,
),
),
const SizedBox(
height: 4.0,
),
Align(
alignment: Alignment.centerLeft,
child: Text(
project.name,
style: const TextStyle(
fontWeight: FontWeight.bold, color: Colors.grey),
),
),
const SizedBox(
height: 4.0,
),Align(
alignment: Alignment.centerLeft,
child: Text(
"Department : ${project.department}",
style: const TextStyle(
fontWeight: FontWeight.normal, color: Colors.grey),
),
), Align(
alignment: Alignment.centerLeft,
child: Text(
"Office : ${project.office}",
style: const TextStyle(
fontWeight: FontWeight.normal, color: Colors.grey),
),
),
]),
);
},
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
);
}
}