flutter_demo_gps/lib/gps_bloc_app.dart

89 lines
2.3 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:gps/blocs/bloc_provider.dart';
import 'package:gps/blocs/gps_bloc.dart';
import 'package:gps/utils/environment.dart';
import 'package:gps/utils/gps_persister.dart';
import 'package:gps/utils/gps_utils.dart';
import 'package:gps/widgets/gps_buttons_widget_bloc.dart';
import 'package:gps/widgets/gps_position_widget_bloc.dart';
import 'package:gps/widgets/gps_title_widget_bloc.dart';
import 'package:location/location.dart';
void main() {
runApp(const GpsBlocApp());
}
class GpsBlocApp extends StatefulWidget {
const GpsBlocApp({Key? key}) : super(key: key);
@override
GpsBlocAppState createState() => GpsBlocAppState();
}
class GpsBlocAppState extends State<GpsBlocApp> {
Locale locale = AppLocalizations.supportedLocales.first;
StreamSubscription<LocationData>? gps;
@override
void initState() {
super.initState();
}
@override
void deactivate() {
gps?.cancel();
super.deactivate();
}
void _nextLocale() {
const locales = AppLocalizations.supportedLocales;
final nextIdx = (locales.indexOf(locale) + 1) % locales.length;
setState(() {
locale = locales[nextIdx];
});
}
@override
Widget build(BuildContext context) {
GpsBloc myBloc = GpsBloc(CsvGpsPersister(Environment.storageDir()));
gps = GpsUtils().stream.listen((event) {
myBloc.gpsIn.add(event);
});
return MaterialApp(
onGenerateTitle: (BuildContext context) =>
AppLocalizations.of(context)!.appName,
theme: ThemeData(
primarySwatch: Colors.blue,
),
locale: locale,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: BlocProvider<GpsBloc>(
bloc: myBloc,
child: buildScaffold(context),
),
);
}
Widget buildScaffold(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const GpsTitleWidgetBloc(),
),
body: Center(
child: Column(children: const [
GpsPositionWidget(),
GpsButtonsWidgetBloc(),
])),
floatingActionButton: FloatingActionButton(
onPressed: _nextLocale,
child: const Icon(Icons.update),
));
}
}