RepoSelector: Auto select exact matching item when the user types it in

This commit is contained in:
Vishesh Handa
2020-11-13 16:25:45 +01:00
parent daf63b438a
commit 7ffb3f86ac
2 changed files with 40 additions and 5 deletions

View File

@ -2,6 +2,8 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
typedef OAuthCallback = void Function(GitHostException);
abstract class GitHost {
@ -79,8 +81,19 @@ class GitHostRepo {
@override
String toString() => toJson().toString();
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is GitHostRepo &&
runtimeType == other.runtimeType &&
_mapEquals(toJson(), other.toJson());
@override
int get hashCode => toJson().hashCode;
}
var _mapEquals = (const MapEquality()).equals;
class GitHostException implements Exception {
static const OAuthFailed = GitHostException("OAuthFailed");
static const MissingAccessCode = GitHostException("MissingAccessCode");

View File

@ -50,10 +50,27 @@ class GitHostSetupRepoSelectorState extends State<GitHostSetupRepoSelector> {
super.initState();
_textController.addListener(() {
var q = _textController.text.toLowerCase();
if (q.isEmpty) {
setState(() {
selectedRepo = null;
createRepo = false;
});
return;
}
var repoIndex = repos.indexWhere((r) =>
r.name.toLowerCase() == q && r.username == widget.userInfo.username);
if (repoIndex == -1) {
setState(() {
selectedRepo = null;
createRepo = false;
});
} else {
setState(() {
selectedRepo = repos[repoIndex];
createRepo = false;
});
}
});
_initStateAysnc();
}
@ -336,13 +353,18 @@ class _RepoTile extends StatelessWidget {
);
}
return ListTile(
var tile = ListTile(
title: title,
trailing: _SmartDateTime(repo.updatedAt, textTheme.caption),
selected: selected,
contentPadding: const EdgeInsets.all(0.0),
onTap: onTap,
);
return Ink(
color: selected ? Theme.of(context).highlightColor : Colors.transparent,
child: tile,
);
}
}