srt_app/lib/main.dart

209 lines
6.4 KiB
Dart

import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'utils/scroll.dart';
import 'pages/home.dart';
import 'pages/map.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const Color fallbackColorSchemeSeed = Colors.blue;
return DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
ColorScheme lightColorScheme;
ColorScheme darkColorScheme;
if (lightDynamic != null && darkDynamic != null) {
lightColorScheme = lightDynamic.harmonized();
darkColorScheme = darkDynamic.harmonized();
} else {
lightColorScheme = ColorScheme.fromSeed(
seedColor: fallbackColorSchemeSeed,
);
darkColorScheme = ColorScheme.fromSeed(
seedColor: fallbackColorSchemeSeed,
brightness: Brightness.dark,
);
}
final textTheme = Theme.of(context).textTheme;
ThemeData lightTheme = ThemeData(
colorScheme: lightColorScheme,
useMaterial3: true,
navigationRailTheme: NavigationRailThemeData(
unselectedLabelTextStyle: TextStyle(
fontSize: textTheme.labelLarge?.fontSize, color: Colors.black),
unselectedIconTheme: const IconThemeData(
color: Colors.black,
),
selectedLabelTextStyle: TextStyle(
fontSize: textTheme.labelLarge?.fontSize,
color: Colors.black,
),
selectedIconTheme: const IconThemeData(
color: Colors.black,
),
),
);
ThemeData darkTheme = ThemeData(
brightness: Brightness.dark,
colorScheme: darkColorScheme,
useMaterial3: true,
navigationRailTheme: NavigationRailThemeData(
unselectedLabelTextStyle: TextStyle(
fontSize: textTheme.labelLarge?.fontSize, color: Colors.white),
unselectedIconTheme: const IconThemeData(
color: Colors.white,
),
selectedLabelTextStyle: TextStyle(
fontSize: textTheme.labelLarge?.fontSize,
color: Colors.white,
),
selectedIconTheme: const IconThemeData(
color: Colors.white,
),
),
);
return MaterialApp(
scrollBehavior: CustomScrollBehavior(),
debugShowCheckedModeBanner: false,
themeMode: ThemeMode.system,
theme: lightTheme,
darkTheme: darkTheme,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', 'GB'), // English, UK
Locale('ar', 'AE'), // Arabic, UAE
Locale('en', 'IN'), // English, India
],
home: const MyHomePage(),
);
});
}
}
class MyHomePage extends StatefulWidget {
/// Creates a const [MyHomePage].
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedTab = 0;
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
bool extendedValue = true;
NavigationRailLabelType labelType = NavigationRailLabelType.none;
Widget leading = const Visibility(
visible: false,
child: SizedBox.shrink(),
);
if (screenWidth >= 600 && screenWidth < 800) {
extendedValue = false;
labelType = NavigationRailLabelType.all;
}
return LayoutBuilder(builder: (context, constraints) {
return Scaffold(
bottomNavigationBar: MediaQuery.of(context).size.width < 600
? NavigationBar(
onDestinationSelected: (int index) {
setState(() {
_selectedTab = index;
});
},
indicatorColor:
Theme.of(context).colorScheme.secondaryContainer,
selectedIndex: _selectedTab,
destinations: const <Widget>[
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.map_outlined),
selectedIcon: Icon(Icons.map),
label: 'Map',
),
])
: null,
body: Row(
mainAxisSize: MainAxisSize.max,
children: [
if (MediaQuery.of(context).size.width >= 600)
SafeArea(
child: NavigationRail(
labelType: labelType,
elevation: 4,
leading: leading,
backgroundColor: ElevationOverlay.applySurfaceTint(
Theme.of(context).cardColor,
Theme.of(context).colorScheme.surfaceTint,
1),
extended: extendedValue,
destinations: const [
NavigationRailDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: Text('Home'),
),
NavigationRailDestination(
icon: Icon(Icons.map_outlined),
selectedIcon: Icon(Icons.map),
label: Text('Map'),
),
],
selectedIndex: _selectedTab,
onDestinationSelected: (value) {
setState(() {
_selectedTab = value;
});
},
),
),
Expanded(
child: Container(
child: _getScreen(context, _selectedTab),
),
),
],
),
);
});
}
}
Widget _getScreen(BuildContext context, int index) {
switch (index) {
case 0:
return const HomeScreen();
case 1:
return const MapScreen();
default:
return const Text('Something went wrong');
}
}