srt_app/lib/main.dart

209 lines
6.4 KiB
Dart
Raw Permalink Normal View History

2024-09-23 11:47:23 +07:00
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'utils/scroll.dart';
2024-09-23 11:52:20 +07:00
import 'pages/home.dart';
import 'pages/map.dart';
2024-09-23 11:47:23 +07:00
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
2024-09-23 11:52:20 +07:00
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,
2024-09-23 11:47:23 +07:00
useMaterial3: true,
2024-09-23 11:52:20 +07:00
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(),
);
});
2024-09-23 11:47:23 +07:00
}
}
class MyHomePage extends StatefulWidget {
2024-09-23 11:52:20 +07:00
/// Creates a const [MyHomePage].
const MyHomePage({super.key});
2024-09-23 11:47:23 +07:00
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
2024-09-23 11:52:20 +07:00
int _selectedTab = 0;
2024-09-23 11:47:23 +07:00
@override
Widget build(BuildContext context) {
2024-09-23 11:52:20 +07:00
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),
),
2024-09-23 11:47:23 +07:00
),
],
),
2024-09-23 11:52:20 +07:00
);
});
}
}
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');
2024-09-23 11:47:23 +07:00
}
}