You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.3 KiB

7 months ago
import 'dart:io';
import 'package:counters/address.dart';
import 'package:counters/counters_page.dart';
import 'package:counters/datbase.dart';
import 'package:counters/new_address_page.dart';
7 months ago
import 'package:counters/record_action_pane.dart';
import 'package:counters/update_address_page.dart';
7 months ago
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
7 months ago
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:intl/intl_standalone.dart';
7 months ago
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
7 months ago
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await findSystemLocale();
7 months ago
if (Platform.isWindows || Platform.isLinux) {
sqfliteFfiInit();
}
databaseFactory = databaseFactoryFfi;
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Журнал счетчиков',
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
],
supportedLocales: AppLocalizations.supportedLocales,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
// ignore: prefer_const_constructors
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final addresses = DBProvider.db.getAllAddress();
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(AppLocalizations.of(context)!.app_title),
),
body: Center(
child: FutureBuilder<List<Address>>(
future: addresses,
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
7 months ago
return AddressesListView(
addresses: snapshot.data!,
onUpdated: () {
setState(() {});
},
);
7 months ago
}
return Text(AppLocalizations.of(context)!.empty_addresses_list);
},
)),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => NewAddressPage()))
.then((_) => setState(() {}));
},
tooltip: AppLocalizations.of(context)!.add_new_address_tooltip,
child: const Icon(Icons.add),
),
);
}
}
7 months ago
class AddressesListView extends StatefulWidget {
7 months ago
const AddressesListView({
super.key,
required this.addresses,
7 months ago
this.onUpdated,
7 months ago
});
final List<Address> addresses;
7 months ago
final VoidCallback? onUpdated;
7 months ago
7 months ago
@override
State<AddressesListView> createState() => _AddressesListViewState();
}
class _AddressesListViewState extends State<AddressesListView> {
7 months ago
@override
Widget build(BuildContext context) {
return ListView.builder(
7 months ago
itemCount: widget.addresses.length,
7 months ago
itemBuilder: (context, possition) {
7 months ago
return Slidable(
key: const ValueKey(0),
endActionPane: RecordActionPane(
onEdit: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => UpdateAddressPage(
address: widget.addresses[possition],
))).then((_) => widget.onUpdated?.call());
},
onDelete: () {
DBProvider.db.deleteAddress(widget.addresses[possition]);
widget.onUpdated?.call();
},
).build(context),
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CountersPage(
address: widget.addresses[possition],
title: widget.addresses[possition].streetName,
comments: widget.addresses[possition].comments)));
},
style:
TextButton.styleFrom(shape: const BeveledRectangleBorder()),
child: Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.addresses[possition].streetName,
style: Theme.of(context).textTheme.bodyLarge,
),
Text(
widget.addresses[possition].comments,
style: Theme.of(context).textTheme.bodySmall,
),
],
)),
),
7 months ago
),
);
});
}
}