Files
app-grosir/lib/main.dart
2026-07-23 22:12:32 +07:00

177 lines
5.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'config.dart';
import 'state/app_state.dart';
import 'services/printer_service.dart';
import 'screens/login_screen.dart';
import 'screens/home_page.dart';
import 'utils/colors.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Supabase.initialize(url: supabaseUrl, publishableKey: supabaseAnonKey);
await printerService.init();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
late final AppState _appState;
@override
void initState() {
super.initState();
_appState = AppState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_appState.stopPolling();
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
_appState.startPolling();
} else {
_appState.stopPolling();
}
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: _appState,
child: MaterialApp(
title: 'Dutra Store',
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: true,
brightness: Brightness.dark,
scaffoldBackgroundColor: kBackground,
colorScheme: ColorScheme.dark(
primary: kAccent,
secondary: kSuccess,
surface: kSurface,
onSurface: kText,
error: kDanger,
),
appBarTheme: const AppBarTheme(
backgroundColor: kBackground,
foregroundColor: kText,
elevation: 0,
centerTitle: true,
titleTextStyle: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700,
color: kText,
),
),
cardTheme: CardThemeData(
color: kSurface2,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(kRadius),
side: const BorderSide(color: kBorder),
),
),
inputDecorationTheme: InputDecorationTheme(
filled: true,
fillColor: kSurface2,
contentPadding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(14),
borderSide: BorderSide.none,
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(14),
borderSide: const BorderSide(color: kBorder),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(14),
borderSide: const BorderSide(color: kAccent, width: 1.5),
),
labelStyle: const TextStyle(color: kMuted),
hintStyle: const TextStyle(color: kMuted),
),
filledButtonTheme: FilledButtonThemeData(
style: FilledButton.styleFrom(
backgroundColor: kAccent,
foregroundColor: const Color(0xFF1A1206),
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14),
),
textStyle: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
),
),
),
dialogTheme: DialogThemeData(
backgroundColor: kSurface,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
titleTextStyle: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w700,
color: kText,
),
),
bottomNavigationBarTheme: const BottomNavigationBarThemeData(
backgroundColor: kSurface,
selectedItemColor: kAccent,
unselectedItemColor: kMuted,
selectedLabelStyle: TextStyle(fontWeight: FontWeight.w600),
type: BottomNavigationBarType.fixed,
elevation: 0,
),
),
home: const AuthGate(),
),
);
}
}
class AuthGate extends StatelessWidget {
const AuthGate({super.key});
@override
Widget build(BuildContext context) {
return Selector<AppState, bool>(
selector: (context, state) => state.loading,
builder: (context, loading, child) {
if (loading) {
return const Scaffold(
body: Center(
child: CircularProgressIndicator(color: kAccent),
),
);
}
return Selector<AppState, bool>(
selector: (context, state) => state.isLoggedIn,
builder: (context, loggedIn, child) {
if (loggedIn) {
return const HomePage();
}
return const LoginScreen();
},
);
},
);
}
}