Skip to content

Instantly share code, notes, and snippets.

@renefloor
Created July 15, 2022 15:41
Show Gist options
  • Save renefloor/66c4d5011f6ab8c899ce0f36acec1512 to your computer and use it in GitHub Desktop.
Save renefloor/66c4d5011f6ab8c899ce0f36acec1512 to your computer and use it in GitHub Desktop.
Example of broken dashbook
import 'package:dashbook/dashbook.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
enum TestBrand {
love,
hate,
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _selectedIndex = 0;
late final List<TestBook> books = createBooks();
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Row(
children: <Widget>[
NavigationRail(
selectedIndex: _selectedIndex,
onDestinationSelected: (int index) {
setState(() {
_selectedIndex = index;
});
},
labelType: NavigationRailLabelType.none,
useIndicator: true,
indicatorColor: Colors.grey.shade300,
destinations: createDestinations(),
),
const VerticalDivider(thickness: 1, width: 1),
// This is the main content.
Expanded(
child: Stack(children: [
for (var i = 0; i < TestBrand.values.length; i++)
IgnorePointer(
ignoring: i != _selectedIndex,
child: AnimatedOpacity(
opacity: i == _selectedIndex ? 1 : 0,
duration: const Duration(milliseconds: 200),
child: books[i],
),
),
]),
)
],
),
),
);
}
List<NavigationRailDestination> createDestinations() => TestBrand.values
.map((brand) => NavigationRailDestination(
icon: Icon(
brand.icon,
),
label: Text(brand.brandName),
))
.toList();
List<TestBook> createBooks() {
return TestBrand.values
.map(
(brand) => TestBook(
key: ValueKey(brand),
themes: getThemes(brand),
),
)
.toList();
}
Map<String, ThemeData> getThemes(TestBrand brand) {
switch (brand) {
case TestBrand.love:
return {
'light': ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.red,
brightness: Brightness.light,
),
),
'dark': ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.red,
brightness: Brightness.dark,
),
),
};
case TestBrand.hate:
return {
'light': ThemeData(
brightness: Brightness.light,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.black),
),
};
}
}
}
class TestBook extends StatelessWidget {
const TestBook({Key? key, required this.themes}) : super(key: key);
final Map<String, ThemeData> themes;
@override
Widget build(BuildContext context) {
final dashbook = Dashbook.multiTheme(themes: themes);
// Adds the Text widget stories
dashbook
.storiesOf('Text')
// Decorators are easy ways to apply a common layout to all of the story chapters, here are using onde of Dashbook's decorators,
// which will center all the widgets on the center of the screen
.decorator(CenterDecorator())
// The Widget story can have as many chapters as needed
.add('default', (ctx) {
return Container(
width: 300,
child: Text(
ctx.textProperty("text", "Text Example"),
textAlign: ctx.listProperty(
"text align",
TextAlign.center,
TextAlign.values,
),
textDirection: ctx.listProperty(
"text direction",
TextDirection.rtl,
TextDirection.values,
),
style: TextStyle(
fontWeight: ctx.listProperty(
"font weight",
FontWeight.normal,
FontWeight.values,
),
fontStyle: ctx.listProperty(
"font style",
FontStyle.normal,
FontStyle.values,
),
fontSize: ctx.numberProperty("font size", 20)),
));
});
dashbook
.storiesOf('RaisedButton')
.decorator(CenterDecorator())
.add('default', (ctx) => RaisedButton(child: Text('Ok'), onPressed: () {}));
return dashbook;
}
}
extension BrandExtensions on TestBrand {
String get brandName {
switch (this) {
case TestBrand.love:
return 'Love';
case TestBrand.hate:
return 'Hate';
}
}
IconData get icon {
switch (this) {
case TestBrand.love:
return Icons.favorite;
case TestBrand.hate:
return Icons.heart_broken;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment