Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.16 KB | None | 0 0
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:http/http.dart';
  4. import 'package:shared_preferences/shared_preferences.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import "package:dslink_schedule/ical.dart";
  7. import 'package:rxdart/rxdart.dart';
  8. import 'package:flutter/services.dart';
  9. import 'package:flutter_stream_friends/flutter_stream_friends.dart';
  10.  
  11. void main() {
  12.   runApp(new MyApp());
  13. }
  14.  
  15. class MyApp extends StatelessWidget {
  16.   // This widget is the root of your application.
  17.   @override
  18.   Widget build(BuildContext context) {
  19.     return new MaterialApp(
  20.       title: 'MAH Schema',
  21.       theme: new ThemeData(
  22.         primarySwatch: Colors.blue,
  23.       ),
  24.       home: new MyHomePage(title: 'MAH Schema'),
  25.     );
  26.   }
  27. }
  28.  
  29.  
  30. class MyHomePage extends StatefulWidget {
  31.   MyHomePage({Key key, this.title}) : super(key: key);
  32.  
  33.   final String title;
  34.  
  35.   @override
  36.   _MyHomePageState createState() => new _MyHomePageState();
  37. }
  38.  
  39.  
  40. class _MyHomePageState extends State<MyHomePage> {
  41.   final ValueChangedStreamCallback<String> onTextChanged = new ValueChangedStreamCallback<String>();
  42.   dynamic searchResults;
  43.   bool hasError = false;
  44.   bool isLoading = false;
  45.  
  46.   _MyHomePageState() {
  47.     new Observable<String>(onTextChanged)
  48.         // Use distinct() to ignore all keystrokes that don't have an impact on the input field's value (brake, ctrl, shift, ..)
  49.         .distinct((String prev, String next) => prev == next)
  50.         // Use debounce() to prevent calling the server on fast following keystrokes
  51.         .debounce(const Duration(milliseconds: 250))
  52.         // Use call(onData) to clear the previous results / errors and begin showing the loading state
  53.         .doOnEach((var _) {
  54.           setState(() {
  55.             hasError = false;
  56.             isLoading = true;
  57.             searchResults = null;
  58.           });
  59.         })
  60.         .flatMapLatest((String value) => fetchAutoComplete(value))
  61.         .listen((dynamic latestResult) {
  62.           debugPrint(latestResult.toString());
  63.         // If a result has been returned, disable the loading and error states and save the latest result
  64.           setState(() {
  65.             isLoading = false;
  66.             hasError = false;
  67.             searchResults = latestResult;
  68.           });
  69.         }, onError: (dynamic e) {
  70.           debugPrint("ERROR: ${e.toString()}");
  71.           setState(() {
  72.             isLoading = false;
  73.             hasError = true;
  74.             searchResults = null;
  75.           });
  76.         }, cancelOnError: false);
  77.   }
  78.  
  79.   Observable<dynamic> fetchAutoComplete(String searchString) {
  80.     var httpClient = createHttpClient();
  81.     return  new Observable<String>.fromFuture(
  82.         httpClient.read("https://kronox.mah.se/ajax/ajax_autocompleteResurser.jsp?typ=program&term=${searchString}")
  83.     )
  84.     .map((String response) => JSON.decode(response));
  85.   }
  86.  
  87.   @override
  88.   Widget build(BuildContext context) {
  89.  
  90.     return new Scaffold(
  91.       appBar: new AppBar(
  92.         title: new Text(widget.title),
  93.       ),
  94.       body: buildSearch(),
  95.     );
  96.   }
  97.  
  98.   Widget buildSearch() {
  99.     return new IconTheme(
  100.         data: new IconThemeData(color: Theme.of(context).accentColor),
  101.         child: new Container(
  102.           margin: const EdgeInsets.symmetric(horizontal: 8.0),
  103.           child: new Column(
  104.             children: <Widget>[
  105.               new Row(
  106.                   children: <Widget>[
  107.                     new Flexible(
  108.                       child: new TextField(
  109.                         onChanged: onTextChanged,
  110.                         decoration: new InputDecoration.collapsed(
  111.                             hintText: "Search for program or course"),
  112.                       ),
  113.                     ),
  114.                     new Container(
  115.                         margin: new EdgeInsets.symmetric(horizontal: 4.0),
  116.                         child: new Icon(Icons.search)
  117.                     ),
  118.                   ]
  119.               ),
  120.               new Text(
  121.                 searchResults == null ? "Nothing..." : searchResults.length == 0 ? "No results" : searchResults[0]["value"]
  122.               )
  123.             ],
  124.           )
  125.         )
  126.     );
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement