From 8e86e85b553b21269e2eab965d130bafaeb93e36 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sun, 10 Feb 2019 13:42:02 +0800 Subject: [PATCH] feat: add search screen --- lib/screens/search.dart | 111 +++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/lib/screens/search.dart b/lib/screens/search.dart index a09b6cc..27da887 100644 --- a/lib/screens/search.dart +++ b/lib/screens/search.dart @@ -1,6 +1,10 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import '../providers/settings.dart'; +import '../scaffolds/simple.dart'; +import '../utils/utils.dart'; +import '../widgets/repo_item.dart'; +import '../widgets/loading.dart'; class SearchScreen extends StatefulWidget { @override @@ -9,59 +13,62 @@ class SearchScreen extends StatefulWidget { class _SearchScreenState extends State { int active = 0; - List users = []; + bool loading = false; + List repos = []; - @override - Widget build(BuildContext context) { - switch (SettingsProvider.of(context).theme) { - case ThemeMap.cupertino: - return CupertinoPageScaffold( - navigationBar: CupertinoNavigationBar( - middle: CupertinoTextField( - placeholder: 'Type to search', - onChanged: (String value) { - // - }, - onSubmitted: (String value) { - // - }, - ), - ), - child: SafeArea( - child: Column( - children: [ - CupertinoSegmentedControl( - children: {0: Text('Repos'), 1: Text('Users')}, - onValueChanged: (int value) { - // - }, - ), - ListView.builder( - shrinkWrap: true, - itemCount: users.length, - itemBuilder: (context, index) { - var user = users[index]; - return Row( - children: [ - Image.network(user['avatarUrl']), - Text(user['login']), - ], - ); - }, - ), - ], - ), - ), - ); - - default: - return Scaffold( - appBar: AppBar(title: Text('Search')), - body: ListView.builder( - itemCount: users.length, - itemBuilder: (context, index) => Text(''), - ), - ); + _onSubmitted(String value) async { + setState(() { + loading = true; + }); + try { + // TODO: search other types + var data = await SettingsProvider.of(context).query(''' +{ + search(first: $pageSize, type: REPOSITORY, query: "$value") { + nodes { + ... on Repository { + $repoChunk + } } } +} + '''); + repos = data['search']['nodes']; + } finally { + setState(() { + loading = false; + }); + } + } + + Widget _buildInput() { + switch (SettingsProvider.of(context).theme) { + case ThemeMap.cupertino: + return CupertinoTextField( + // padding: EdgeInsets.all(10), + placeholder: 'Type to search', + clearButtonMode: OverlayVisibilityMode.editing, + onSubmitted: _onSubmitted, + ); + default: + return TextField(onSubmitted: _onSubmitted); + } + } + + @override + Widget build(BuildContext context) { + return SimpleScaffold( + title: Text('Search GitHub Repositories'), + bodyBuilder: () { + return Column(children: [ + Container(padding: EdgeInsets.all(8), child: _buildInput()), + loading + ? Loading() + : Column( + children: repos.map((repo) => RepoItem(repo)).toList(), + ) + ]); + }, + ); + } }