Created
October 5, 2022 02:22
-
-
Save HansMuller/16edccbdc26e5d22b1da2c67276be8e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class Star { | |
const Star({ | |
this.fillColor = Colors.white, | |
this.borderColor = Colors.black, | |
this.borderWidth = 9, | |
this.points = 7, | |
this.innerRadiusRatio = 0.75, | |
this.pointRounding = 0.5, | |
this.valleyRounding = 0.25, | |
this.title = '<no title>', | |
this.titleColor = Colors.black, | |
}); | |
final Color fillColor; | |
final Color borderColor; | |
final double borderWidth; | |
final double points; | |
final double innerRadiusRatio; | |
final double pointRounding; | |
final double valleyRounding; | |
final String title; | |
final Color titleColor; | |
} | |
class StarView extends StatefulWidget { | |
const StarView({ super.key, required this.star }); | |
final Star star; | |
@override | |
State<StarView> createState() => _StarViewState(); | |
} | |
class _StarViewState extends State<StarView> { | |
late final TextEditingController textController; | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
padding: const EdgeInsets.all(64), | |
alignment: Alignment.center, | |
decoration: ShapeDecoration( | |
color: widget.star.fillColor, | |
shape: StarBorder( | |
side: BorderSide( | |
color: widget.star.borderColor, | |
width: widget.star.borderWidth, | |
), | |
points: widget.star.points, | |
innerRadiusRatio: widget.star.innerRadiusRatio, | |
pointRounding: widget.star.pointRounding, | |
valleyRounding: widget.star.valleyRounding, | |
), | |
), | |
child: Text( | |
widget.star.title, | |
style: TextStyle(color: widget.star.titleColor), | |
), | |
); | |
} | |
} | |
class StarHome extends StatefulWidget { | |
const StarHome({ super.key, required this.toggleDarkMode, required this.isRailNavigation }); | |
final VoidCallback toggleDarkMode; | |
final bool isRailNavigation; | |
@override | |
State<StarHome> createState() => _StarHomeState(); | |
} | |
class _StarHomeState extends State<StarHome> { | |
int selectedIndex = 0; | |
late List<Star> stars; | |
@override | |
void didChangeDependencies() { | |
super.didChangeDependencies(); | |
final ColorScheme colorScheme = Theme.of(context).colorScheme; | |
stars = <Star>[ | |
Star( | |
title: 'One', | |
titleColor: colorScheme.onPrimaryContainer, | |
fillColor: colorScheme.primaryContainer, | |
borderColor: colorScheme.primary, | |
), | |
Star( | |
title: 'Two', | |
titleColor: colorScheme.onPrimaryContainer, | |
fillColor: colorScheme.primaryContainer, | |
borderColor: colorScheme.primary, | |
points: 5, | |
borderWidth: 13, | |
innerRadiusRatio: 0.5 | |
), | |
Star( | |
title: 'Free', | |
titleColor: colorScheme.onPrimaryContainer, | |
fillColor: colorScheme.tertiaryContainer, | |
borderColor: colorScheme.tertiary, | |
points: 13, | |
borderWidth: 7, | |
), | |
Star( | |
title: 'Four', | |
titleColor: colorScheme.onErrorContainer, | |
fillColor: colorScheme.errorContainer, | |
borderColor: colorScheme.error, | |
points: 3, | |
borderWidth: 15, | |
innerRadiusRatio: 0.35, | |
), | |
]; | |
} | |
@override | |
Widget build(BuildContext context) { | |
final ColorScheme colorScheme = Theme.of(context).colorScheme; | |
final Widget floatingActionButton = FloatingActionButton( | |
onPressed: widget.toggleDarkMode, | |
child: const Icon(Icons.wb_sunny_outlined), | |
); | |
final Widget view = Container( | |
padding: const EdgeInsets.all(64), | |
alignment: Alignment.center, | |
color: colorScheme.background, | |
child: AspectRatio( | |
aspectRatio: 1, | |
child: StarView( | |
star: stars[selectedIndex], | |
), | |
), | |
); | |
if (widget.isRailNavigation) { | |
final Widget navigationRail = NavigationRail( | |
selectedIndex: selectedIndex, | |
groupAlignment: 0.0, | |
onDestinationSelected: (int index) { | |
setState(() { | |
selectedIndex = index; | |
}); | |
}, | |
leading: floatingActionButton, | |
labelType: NavigationRailLabelType.all, | |
destinations: stars.map<NavigationRailDestination>((Star star) { | |
return NavigationRailDestination( | |
icon: const Icon(Icons.article), | |
label: Text(star.title), | |
); | |
}).toList(), | |
); | |
return Scaffold( | |
body: Row( | |
children: <Widget>[ | |
navigationRail, | |
const VerticalDivider(thickness: 1, width: 1), | |
Expanded(child: view), | |
], | |
), | |
); | |
} | |
final Widget bottomNavigationBar = BottomNavigationBar( | |
currentIndex: selectedIndex, | |
type: BottomNavigationBarType.fixed, | |
onTap: (int index) { | |
setState(() { | |
selectedIndex = index; | |
}); | |
}, | |
items: stars.map<BottomNavigationBarItem>((Star star) { | |
return BottomNavigationBarItem( | |
icon: const Icon(Icons.article), | |
label: star.title, | |
); | |
}).toList(), | |
); | |
return Scaffold( | |
body: view, | |
floatingActionButton: Padding( | |
padding: const EdgeInsets.only(top: 8), | |
child: floatingActionButton | |
), | |
floatingActionButtonLocation: FloatingActionButtonLocation.startTop, | |
bottomNavigationBar: bottomNavigationBar, | |
); | |
} | |
} | |
class StarApp extends StatefulWidget { | |
const StarApp({ super.key }); | |
@override | |
State<StarApp> createState() => _StarAppState(); | |
} | |
class _StarAppState extends State<StarApp> { | |
bool darkMode = false; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
themeMode: darkMode ? ThemeMode.dark : ThemeMode.light, | |
theme: ThemeData( | |
useMaterial3: true, | |
brightness: darkMode ? Brightness.dark : Brightness.light, | |
colorSchemeSeed: const Color(0xff6750a4), | |
), | |
home: LayoutBuilder( | |
builder: (BuildContext context, BoxConstraints constraints) { | |
return StarHome( | |
isRailNavigation: constraints.maxWidth > 440, | |
toggleDarkMode: () { | |
setState(() { | |
darkMode = !darkMode; | |
}); | |
}, | |
); | |
} | |
), | |
); | |
} | |
} | |
void main() { | |
runApp(const StarApp()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment