map_choice_view.dart 3.08 KB
Newer Older
CLOUARD Regis's avatar
CLOUARD Regis committed
1
2
3
4
5
6
7
8
9
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:vikazimut/constants.dart';
import 'package:vikazimut/map_choice/map_list_view.dart';

import 'course_search_field_view.dart';
import 'map_choice_presenter.dart';
import 'memory_info_presenter.dart';
import 'memory_info_view.dart';
CLOUARD Regis's avatar
CLOUARD Regis committed
10
import 'title_view.dart';
CLOUARD Regis's avatar
CLOUARD Regis committed
11
12
13
import 'title_presenter.dart';

class MapChoiceView extends StatefulWidget {
CLOUARD Regis's avatar
CLOUARD Regis committed
14
  final CourseMode mode;
CLOUARD Regis's avatar
CLOUARD Regis committed
15

CLOUARD Regis's avatar
CLOUARD Regis committed
16
  MapChoiceView(this.mode);
CLOUARD Regis's avatar
CLOUARD Regis committed
17
18

  @override
CLOUARD Regis's avatar
CLOUARD Regis committed
19
  MapChoiceViewState createState() => new MapChoiceViewState(mode);
CLOUARD Regis's avatar
CLOUARD Regis committed
20
21
22
23
}

class MapChoiceViewState extends State<MapChoiceView> {
  final TextEditingController _searchQueryController = TextEditingController();
CLOUARD Regis's avatar
CLOUARD Regis committed
24
  final MapChoicePresenter _presenter;
CLOUARD Regis's avatar
CLOUARD Regis committed
25
26
27
  bool _isSearching = false;
  String searchQuery = "";

Clouard Regis's avatar
Clouard Regis committed
28
  MapChoiceViewState(CourseMode mode): _presenter = MapChoicePresenter(mode);
CLOUARD Regis's avatar
CLOUARD Regis committed
29

CLOUARD Regis's avatar
CLOUARD Regis committed
30
31
32
33
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
CLOUARD Regis's avatar
CLOUARD Regis committed
34
        ChangeNotifierProvider<TitlePresenter>.value(
CLOUARD Regis's avatar
CLOUARD Regis committed
35
          value: _presenter.titlePresenter,
CLOUARD Regis's avatar
CLOUARD Regis committed
36
37
        ),
        ChangeNotifierProvider<MemoryInfoPresenter>.value(
CLOUARD Regis's avatar
CLOUARD Regis committed
38
          value: _presenter.memoryInfoPresenter,
CLOUARD Regis's avatar
CLOUARD Regis committed
39
40
41
42
43
44
        ),
      ],
      child: Scaffold(
        appBar: AppBar(
          title: Consumer<TitlePresenter>(builder: (context, presenter, child) {
            return _isSearching
CLOUARD Regis's avatar
CLOUARD Regis committed
45
                ? SearchFieldView(
CLOUARD Regis's avatar
CLOUARD Regis committed
46
47
48
49
50
                    controller: _searchQueryController,
                    onValueChanged: updateSearchQuery,
                  )
                : TitleView(numberOfMaps: presenter.numberOfMaps);
          }),
51
          leading: _isSearching ? const BackButton() : null,
CLOUARD Regis's avatar
CLOUARD Regis committed
52
53
          actions: _buildActions(),
          bottom: PreferredSize(
CLOUARD Regis's avatar
CLOUARD Regis committed
54
            preferredSize: const Size.fromHeight(14.0),
CLOUARD Regis's avatar
CLOUARD Regis committed
55
56
57
58
59
            child: Container(child: Consumer<MemoryInfoPresenter>(builder: (context, presenter, child) {
              return MemoryInfoView(presenter: presenter);
            })),
          ),
        ),
Clouard Regis's avatar
Clouard Regis committed
60
        body: MapListView(searchQuery: searchQuery, presenter: _presenter),
CLOUARD Regis's avatar
CLOUARD Regis committed
61
62
63
64
      ),
    );
  }

CLOUARD Regis's avatar
CLOUARD Regis committed
65
  List<Widget> _buildActions() {
CLOUARD Regis's avatar
CLOUARD Regis committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    if (_isSearching) {
      return <Widget>[
        IconButton(
          icon: const Icon(Icons.clear),
          onPressed: () {
            if (_searchQueryController.text.isEmpty) {
              Navigator.pop(context);
              return;
            }
            _clearSearchQuery();
          },
        ),
      ];
    } else {
      return <Widget>[
        IconButton(
          icon: const Icon(Icons.search),
          onPressed: _startSearch,
        ),
      ];
    }
  }

  void _startSearch() {
    ModalRoute.of(context)!.addLocalHistoryEntry(LocalHistoryEntry(onRemove: _stopSearching));
    setState(() {
      _isSearching = true;
    });
  }

  void updateSearchQuery(String newQuery) {
    setState(() {
      searchQuery = newQuery;
    });
  }

  void _stopSearching() {
    _clearSearchQuery();
    setState(() {
      _isSearching = false;
    });
  }

  void _clearSearchQuery() {
    setState(() {
      _searchQueryController.clear();
      updateSearchQuery("");
    });
  }
}