Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 65 additions & 93 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_chips_input/flutter_chips_input.dart';

const String avatarPlaceholder = "https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png";

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
Expand All @@ -24,39 +27,27 @@ class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();
MyHomePageState createState() => MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
class MyHomePageState extends State<MyHomePage> {
final _chipKey = GlobalKey<ChipsInputState>();

@override
Widget build(BuildContext context) {
const mockResults = <AppProfile>[
AppProfile('John Doe', '[email protected]',
'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg'),
AppProfile('Paul', '[email protected]',
'https://mbtskoudsalg.com/images/person-stock-image-png.png'),
AppProfile('Fred', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Brian', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('John', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Thomas', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Nelly', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Marie', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Charlie', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Diana', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Ernie', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('Gina', '[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
AppProfile('John Doe', '[email protected]', 'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg'),
AppProfile('Paul', '[email protected]', avatarPlaceholder),
AppProfile('Fred', '[email protected]', avatarPlaceholder),
AppProfile('Brian', '[email protected]', avatarPlaceholder),
AppProfile('John', '[email protected]', avatarPlaceholder),
AppProfile('Thomas', '[email protected]', avatarPlaceholder),
AppProfile('Nelly', '[email protected]', avatarPlaceholder),
AppProfile('Marie', '[email protected]', avatarPlaceholder),
AppProfile('Charlie', '[email protected]', avatarPlaceholder),
AppProfile('Diana', '[email protected]', avatarPlaceholder),
AppProfile('Ernie', '[email protected]', avatarPlaceholder),
AppProfile('Gina', '[email protected]', avatarPlaceholder),
];

return Scaffold(
Expand All @@ -67,7 +58,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: SingleChildScrollView(
child: Column(
children: <Widget>[
ChipsInput(
ChipsInput<AppProfile>(
key: _chipKey,
/*initialValue: [
AppProfile('John Doe', '[email protected]',
Expand All @@ -77,132 +68,117 @@ class _MyHomePageState extends State<MyHomePage> {
// allowChipEditing: true,
keyboardAppearance: Brightness.dark,
textCapitalization: TextCapitalization.words,
submitKeys: const [LogicalKeyboardKey.tab],
// enabled: false,
// maxChips: 5,
textStyle: const TextStyle(
height: 1.5, fontFamily: 'Roboto', fontSize: 16),
textStyle: const TextStyle(height: 1.5, fontFamily: 'Roboto', fontSize: 16),
decoration: const InputDecoration(
// prefixIcon: Icon(Icons.search),
prefixIcon: Icon(Icons.search),
// hintText: formControl.hint,
labelText: 'Select People',
// enabled: false,
enabled: false,
// errorText: field.errorText,
border: OutlineInputBorder(),
),
findSuggestions: (String query) {
findSuggestions: (query) {
// print("Query: '$query'");
if (query.isNotEmpty) {
var lowercaseQuery = query.toLowerCase();
return mockResults.where((profile) {
return profile.name
.toLowerCase()
.contains(query.toLowerCase()) ||
profile.email
.toLowerCase()
.contains(query.toLowerCase());
return profile.name.toLowerCase().contains(query.toLowerCase()) || profile.email.toLowerCase().contains(query.toLowerCase());
}).toList(growable: false)
..sort((a, b) => a.name
.toLowerCase()
.indexOf(lowercaseQuery)
.compareTo(
b.name.toLowerCase().indexOf(lowercaseQuery)));
..sort((a, b) => a.name.toLowerCase().indexOf(lowercaseQuery).compareTo(b.name.toLowerCase().indexOf(lowercaseQuery)));
}
// return <AppProfile>[];
return mockResults;
},
onChanged: (data) {
// print(data);
},
chipBuilder: (context, state, dynamic profile) {
chipBuilder: (context, state, index, profile) {
return InputChip(
key: ObjectKey(profile),
label: Text(profile.name),
avatar: CircleAvatar(
backgroundImage: NetworkImage(profile.imageUrl),
),
onDeleted: () => state.deleteChip(profile),
avatar: CircleAvatar(backgroundImage: NetworkImage(profile.imageUrl)),
onDeleted: () => state.deleteChip(index),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
);
},
suggestionBuilder: (context, state, dynamic profile) {
return ListTile(
suggestionBuilder: (context, state, index, profile) {
return InputChip(
key: ObjectKey(profile),
leading: CircleAvatar(
backgroundImage: NetworkImage(profile.imageUrl),
),
title: Text(profile.name),
subtitle: Text(profile.email),
onTap: () => state.selectSuggestion(profile),
label: Text(profile.name),
avatar: CircleAvatar(backgroundImage: NetworkImage(profile.imageUrl)),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
onPressed: () => state.selectSuggestion(profile),
);
},
onSubmit: (txt) {
if (txt.isEmpty) return null;
return mockResults.firstWhere(
(profile) => profile.name == txt,
orElse: () => AppProfile(txt, "", "https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png"),
);
},
),
const TextField(),
/*ChipsInput(
initialValue: [
AppProfile('John Doe', '[email protected]',
'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg'),
ChipsInput(
initialValue: const [
AppProfile('John Doe', '[email protected]', 'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg'),
],
enabled: true,
maxChips: 10,
textStyle: TextStyle(height: 1.5, fontFamily: "Roboto", fontSize: 16),
decoration: InputDecoration(
textStyle: const TextStyle(height: 1.5, fontFamily: "Roboto", fontSize: 16),
decoration: const InputDecoration(
// prefixIcon: Icon(Icons.search),
// hintText: formControl.hint,
labelText: "Select People",
// enabled: false,
// errorText: field.errorText,
),
findSuggestions: (String query) {
if (query.length != 0) {
if (query.isNotEmpty) {
var lowercaseQuery = query.toLowerCase();
return mockResults.where((profile) {
return profile.name
.toLowerCase()
.contains(query.toLowerCase()) ||
profile.email
.toLowerCase()
.contains(query.toLowerCase());
return profile.name.toLowerCase().contains(query.toLowerCase()) || profile.email.toLowerCase().contains(query.toLowerCase());
}).toList(growable: false)
..sort((a, b) => a.name
.toLowerCase()
.indexOf(lowercaseQuery)
.compareTo(
b.name.toLowerCase().indexOf(lowercaseQuery)));
..sort((a, b) => a.name.toLowerCase().indexOf(lowercaseQuery).compareTo(b.name.toLowerCase().indexOf(lowercaseQuery)));
} else {
return mockResults;
}
},
onChanged: (data) {
print(data);
},
chipBuilder: (context, state, profile) {
chipBuilder: (context, state, index, profile) {
return InputChip(
key: ObjectKey(profile),
label: Text(profile.name),
avatar: CircleAvatar(
backgroundImage: NetworkImage(profile.imageUrl),
),
onDeleted: () => state.deleteChip(profile),
avatar: CircleAvatar(backgroundImage: NetworkImage(profile.imageUrl)),
onDeleted: () => state.deleteChip(index),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
);
},
suggestionBuilder: (context, state, profile) {
suggestionBuilder: (context, state, index, profile) {
return ListTile(
key: ObjectKey(profile),
leading: CircleAvatar(
backgroundImage: NetworkImage(profile.imageUrl),
),
leading: CircleAvatar(backgroundImage: NetworkImage(profile.imageUrl)),
title: Text(profile.name),
subtitle: Text(profile.email),
onTap: () => state.selectSuggestion(profile),
);
},
),*/
onSubmit: (txt) {
if (txt.isEmpty) return null;
return mockResults.firstWhere(
(profile) => profile.name == txt,
orElse: () => AppProfile(txt, "", avatarPlaceholder),
);
},
),
ElevatedButton(
onPressed: () {
_chipKey.currentState!.selectSuggestion(const AppProfile(
'Gina',
'[email protected]',
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'));
_chipKey.currentState!.selectSuggestion(const AppProfile('Gina', '[email protected]', avatarPlaceholder));
},
child: const Text('Add Chip'),
),
Expand All @@ -222,11 +198,7 @@ class AppProfile {
const AppProfile(this.name, this.email, this.imageUrl);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is AppProfile &&
runtimeType == other.runtimeType &&
name == other.name;
bool operator ==(Object other) => identical(this, other) || other is AppProfile && runtimeType == other.runtimeType && name == other.name;

@override
int get hashCode => name.hashCode;
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: '>=3.0.0'
flutter: ">=1.22.0"

dependencies:
Expand Down
Loading