basic state management
This commit is contained in:
8
lib/extensions/obtainProviderExtension.dart
Normal file
8
lib/extensions/obtainProviderExtension.dart
Normal file
@ -0,0 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
extension ObtainProviderExtension on BuildContext {
|
||||
T obtainState<T extends ChangeNotifier>() {
|
||||
return Provider.of<T>(this, listen: false);
|
||||
}
|
||||
}
|
@ -1,16 +1,24 @@
|
||||
import 'package:app/state/legalStatusState.dart';
|
||||
import 'package:app/state/mapState.dart';
|
||||
import 'package:app/view/home.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
void main() {
|
||||
runApp(
|
||||
const MyApp(),
|
||||
MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider<LegalStatusState>(create: (context) => LegalStatusState()),
|
||||
ChangeNotifierProvider<MapState>(create: (context) => MapState()),
|
||||
],
|
||||
builder: (context, child) => const MyApp(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
|
6
lib/model/legelState.dart
Normal file
6
lib/model/legelState.dart
Normal file
@ -0,0 +1,6 @@
|
||||
enum LegalState {
|
||||
unknown,
|
||||
allowed,
|
||||
disallowedTime,
|
||||
disallowedRegion
|
||||
}
|
18
lib/state/legalStatusState.dart
Normal file
18
lib/state/legalStatusState.dart
Normal file
@ -0,0 +1,18 @@
|
||||
import 'package:app/model/legelState.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
class LegalStatusState extends ChangeNotifier {
|
||||
LegalState _legalState = LegalState.unknown;
|
||||
|
||||
LegalState get getLegalState => _legalState;
|
||||
|
||||
set setInSightOfDisallowedAreas(bool isInSight) {
|
||||
_legalState = LegalState.disallowedRegion;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
set setIsInDisallowedTimeRanges(bool isInDisallowedTime) {
|
||||
_legalState = LegalState.disallowedTime;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
12
lib/state/mapState.dart
Normal file
12
lib/state/mapState.dart
Normal file
@ -0,0 +1,12 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
class MapState extends ChangeNotifier {
|
||||
bool _isLocationLock = false;
|
||||
|
||||
bool get followLocation => _isLocationLock;
|
||||
|
||||
toggleLocationLock() {
|
||||
_isLocationLock = !_isLocationLock;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
@ -1,5 +1,9 @@
|
||||
import 'package:app/extensions/obtainProviderExtension.dart';
|
||||
import 'package:app/state/mapState.dart';
|
||||
import 'package:app/view/legalStatus.dart';
|
||||
import 'package:app/view/map.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class HomeView extends StatefulWidget {
|
||||
const HomeView({super.key});
|
||||
@ -18,12 +22,20 @@ class _HomePageState extends State<HomeView> {
|
||||
actions: [],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.my_location),
|
||||
onPressed: () {
|
||||
|
||||
},
|
||||
child: Consumer<MapState>(builder: (context, value, child) => Icon(value.followLocation ? Icons.my_location : Icons.location_disabled)),
|
||||
onPressed: () => context.obtainState<MapState>().toggleLocationLock(),
|
||||
),
|
||||
body: const Column(
|
||||
children: [
|
||||
SizedBox(
|
||||
height: 100,
|
||||
child: LegalStatusView(),
|
||||
),
|
||||
Expanded(
|
||||
child: MapView(),
|
||||
)
|
||||
],
|
||||
),
|
||||
body: MapView(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
21
lib/view/legalStatus.dart
Normal file
21
lib/view/legalStatus.dart
Normal file
@ -0,0 +1,21 @@
|
||||
import 'package:app/state/legalStatusState.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class LegalStatusView extends StatefulWidget {
|
||||
const LegalStatusView({super.key});
|
||||
|
||||
@override
|
||||
State<LegalStatusView> createState() => _LegalStatusViewState();
|
||||
}
|
||||
|
||||
class _LegalStatusViewState extends State<LegalStatusView> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<LegalStatusState>(
|
||||
builder: (context, value, child) {
|
||||
return Placeholder();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
15
lib/view/status.dart
Normal file
15
lib/view/status.dart
Normal file
@ -0,0 +1,15 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class StatusView extends StatefulWidget {
|
||||
const StatusView({super.key});
|
||||
|
||||
@override
|
||||
State<StatusView> createState() => _StatusViewState();
|
||||
}
|
||||
|
||||
class _StatusViewState extends State<StatusView> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const Placeholder();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user