Skip to content

Commit

Permalink
Feature/network search (#54)
Browse files Browse the repository at this point in the history
Merge pull request #54 from insolite-dev/feature/network-search
  • Loading branch information
theiskaa committed Feb 22, 2023
2 parents bafef40 + 6e0496b commit 0244ab5
Show file tree
Hide file tree
Showing 12 changed files with 857 additions and 235 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: subosito/flutter-action@v1
with:
channel: "stable"
flutter-version: "2.5.0"
flutter-version: "3.7.3"

- name: Test the application
run: flutter test --coverage
Expand All @@ -30,4 +30,4 @@ jobs:
uses: codecov/codecov-action@v1.2.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage/lcov.info
file: coverage/lcov.info
8 changes: 3 additions & 5 deletions .github/workflows/doctor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ jobs:
# Setup the Flutter environment.
- uses: subosito/flutter-action@v1
with:
channel: "stable" # "alpha", "beta", "dev", default to: "stable"
#
# # You can also specify exact version of Flutter.
flutter-version: "2.5.0"
channel: "stable"
flutter-version: "3.7.3"

- name: Print Flutter SDK version
run: flutter --version
Expand All @@ -33,7 +31,7 @@ jobs:

# Consider passing '--output=none' to 'format' for no standard output.
- name: Verify formatting
run: flutter format --set-exit-if-changed .
run: dart format --set-exit-if-changed .

# Consider passing '--fatal-infos' for slightly stricter analysis.
- name: Statically analyze the Dart code for any errors
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ jobs:
# Setup the Flutter environment.
- uses: subosito/flutter-action@v1
with:
channel: "stable" # "alpha", "beta", "dev", default to: "stable"
#
# # You can also specify exact version of Flutter.
flutter-version: "2.5.0"
channel: "stable"
flutter-version: "3.7.3"

- name: Test the application
run: flutter test
47 changes: 42 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,48 @@ TITLE LOGO: https://user-images.githubusercontent.com/59066341/137352996-0d132ee
</a>
</div><br>

### Installing
- See the official installing guidline from [field_suggestion/install](https://github.com/theiskaa/field_suggestion/wiki/Installing)
## Installing
- See the official installing guidline from [field_suggestion/install](https://github.com/insolite-dev/field_suggestion/wiki/Installing)

### Official Documentation (Wiki)
- Read the official documentation from [field_suggestion/wiki](https://github.com/theiskaa/field_suggestion/wiki)
## Usage

### Contributing
- For more detailed examples refer to the official documentation from [field_suggestion/wiki](https://github.com/insolite-dev/field_suggestion/wiki)

In this example, we're using the `FieldSuggestion.network` widget to display suggestions for a username input field. We've provided an `inputDecoration` with a hint text, a `future` function that fetches the suggestions based on the user input, a `textController` that controls the text in the input field, a `boxController` that controls the visibility of the suggestion box, and a builder function that defines how the suggestions are displayed in the suggestion box.

Note that `FieldSuggestion` uses generics to allow for suggestions of different types. In the example above, we're using `String` as the type parameter for `FieldSuggestion.network` However, any other type can be used, depending on the type of suggestions being displayed.

<img align=right height=250px width=400px src="https://user-images.githubusercontent.com/59066341/220667553-d4d83c93-6e15-424c-b2d5-7a221f30069c.gif">

```dart
FieldSuggestion<String>.network(
inputDecoration: InputDecoration(
hintText: 'Username', // optional
),
future: (input) => future.call(input),
textController: controller,
boxController: boxController, // optional
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Center(child: CircularProgressIndicator());
}
final result = snapshot.data ?? [];
return ListView.builder(
itemCount: result.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
setState(() => controller.text = result[index]);
boxControllerNetwork.close?.call();
},
child: Card(child: ListTile(title: Text(result[index]))),
);
},
);
},
)
```

## Contributing
- For information regarding contributions, please refer to [CONTRIBUTING.md](https://github.com/theiskaa/field_suggestion/blob/develop/CONTRIBUTING.md) file.
73 changes: 70 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:field_suggestion/field_suggestion.dart';
import 'package:flutter/material.dart';
import 'package:field_suggestion/field_suggestion.dart';

import 'user_model.dart';

Expand All @@ -24,9 +24,19 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
// A box controller for default and local usable FieldSuggestion.
final boxController = BoxController();

// A box controller for network usable FieldSuggestion.
final boxControllerNetwork = BoxController();

// A text editing controller for default and local usable FieldSuggestion.
final textController = TextEditingController();

// A text editing controller for network usable FieldSuggestion.
final textControllerNetwork = TextEditingController();

// A ready data, that's used as suggestions for default widget and network future.
List<UserModel> suggestions = [
UserModel(
email: 'john-doe@gmail.com',
Expand All @@ -45,17 +55,31 @@ class _HomePageState extends State<HomePage> {
)
];

// A fake future builder that waits for 1 second to complete search.
final strSuggestions = ['Rasul', 'Andro', 'Onur', 'Ismael', 'Davit'];
Future<List<String>> future(String input) => Future<List<String>>.delayed(
const Duration(seconds: 1),
() => strSuggestions
.where((s) => s.toLowerCase().contains(input.toLowerCase()))
.toList(),
);

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => boxController.close?.call(),
onTap: () {
boxController.close?.call();
boxControllerNetwork.close?.call();
},
child: Scaffold(
appBar: AppBar(title: const Text("FieldSuggestion Example")),
body: SingleChildScrollView(
padding: const EdgeInsets.all(15),
child: Center(
child: Column(
children: [
/// The default local usage, which requires already built suggestions list.
/// See line [135] for .network() variant implementation.
FieldSuggestion<UserModel>(
inputDecoration: InputDecoration(
hintText: 'Email', // optional
Expand Down Expand Up @@ -101,7 +125,50 @@ class _HomePageState extends State<HomePage> {
);
},
),
const SizedBox(height: 100),

SizedBox(height: 50),
const Divider(),
SizedBox(height: 50),

/// A network usage of [FieldSuggestion].
FieldSuggestion<String>.network(
future: (input) => future.call(input),
boxController: boxControllerNetwork,
textController: textControllerNetwork,
inputDecoration: InputDecoration(
hintText: 'Username', // optional
),
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Center(child: CircularProgressIndicator());
}

final result = snapshot.data ?? [];
return ListView.builder(
itemCount: result.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
setState(
() => textControllerNetwork.text = result[index],
);

textControllerNetwork.selection =
TextSelection.fromPosition(
TextPosition(
offset: textControllerNetwork.text.length),
);

boxControllerNetwork.close?.call();
},
child: Card(
child: ListTile(title: Text(result[index])),
),
);
},
);
},
),
],
),
),
Expand Down

0 comments on commit 0244ab5

Please sign in to comment.