Skip to content

Commit

Permalink
feat: integrate Widget Book with a sample button code #3
Browse files Browse the repository at this point in the history
- add `ButtonIconState` enum
- add `customButtonWidgetUseCase` for `custom button` widget
- add `CustomButtonWidget`
- update `main` & add widget book required setup
- add `main.directories`
- comment `widget_test`
  • Loading branch information
pouria.moradi authored and pouria.moradi committed Jan 3, 2025
1 parent c6ba61e commit 34e7f55
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 247 deletions.
132 changes: 21 additions & 111 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,125 +1,35 @@
import 'package:flutter/material.dart';
import 'package:widgetbook/widgetbook.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart' as widgetbook;

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
import 'main.directories.g.dart';

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
void main() {
runApp(const WidgetbookApp());
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@widgetbook.App()
class WidgetbookApp extends StatelessWidget {
const WidgetbookApp({super.key});

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
return Widgetbook(
appBuilder: (context, child) {
return child;
},
addons: [
DeviceFrameAddon(
devices: [
Devices.ios.iPhone13,
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
AlignmentAddon(),
],
// The [directories] variable does not exist yet,
// it will be generated in the next step
directories: directories,
);
}
}
39 changes: 39 additions & 0 deletions lib/main.directories.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// coverage:ignore-file
// ignore_for_file: type=lint
// ignore_for_file: unused_import, prefer_relative_imports, directives_ordering

// GENERATED CODE - DO NOT MODIFY BY HAND

// **************************************************************************
// AppGenerator
// **************************************************************************

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:pactus_gui_widgetbook/src/features/widgets/custom_button/custom_button_usecase.dart'
as _i2;
import 'package:widgetbook/widgetbook.dart' as _i1;

final directories = <_i1.WidgetbookNode>[
_i1.WidgetbookFolder(
name: 'features',
children: [
_i1.WidgetbookFolder(
name: 'widgets',
children: [
_i1.WidgetbookFolder(
name: 'custom_button',
children: [
_i1.WidgetbookLeafComponent(
name: 'CustomButtonWidget',
useCase: _i1.WidgetbookUseCase(
name: 'Custom Button with Knobs',
builder: _i2.customButtonWidgetUseCase,
),
)
],
)
],
)
],
)
];
4 changes: 4 additions & 0 deletions lib/src/core/enum/button_icon_state_enum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum ButtonIconState {
withIcon,
withoutIcon,
}
42 changes: 42 additions & 0 deletions lib/src/features/widgets/custom_button/custom_button_usecase.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';
import 'package:pactus_gui_widgetbook/src/core/enum/button_icon_state_enum.dart';
import 'package:pactus_gui_widgetbook/src/features/widgets/custom_button/custom_button_widget.dart';
import 'package:widgetbook/widgetbook.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart';

@UseCase(name: 'Custom Button with Knobs', type: CustomButtonWidget)
Widget customButtonWidgetUseCase(BuildContext context) {
// Knob for button size
final size = context.knobs.double.slider(
label: 'Button Size',
initialValue: 48.0,
min: 24.0,
max: 100.0,
description: 'Set the size of the button.',
);

// Knob for button color
final color = context.knobs.color(
label: 'Button Color',
initialValue: Colors.blue,
description: 'Set the color of the button.',
);

// Knob for button icon state
final iconStateString = context.knobs.string(
label: 'Icon State',
initialValue: 'withIcon',
description: 'Choose whether the button has an icon or not. Use "withIcon" or "withoutIcon".',
);

// Map string to ButtonIconState enum
final iconState = iconStateString == 'withIcon'
? ButtonIconState.withIcon
: ButtonIconState.withoutIcon;

return CustomButtonWidget(
size: size,
color: color,
iconState: iconState,
);
}
41 changes: 41 additions & 0 deletions lib/src/features/widgets/custom_button/custom_button_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flutter/material.dart';
import 'package:pactus_gui_widgetbook/src/core/enum/button_icon_state_enum.dart';

class CustomButtonWidget extends StatelessWidget {
final double size;
final Color color;
final ButtonIconState iconState;

const CustomButtonWidget({
super.key,
this.size = 48.0,
this.color = Colors.blue,
this.iconState = ButtonIconState.withIcon,
});

@override
Widget build(BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: color,
fixedSize: Size(size, size),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
onPressed: () {
// Action on button press
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Button Pressed!')),
);
},
child: iconState == ButtonIconState.withIcon
? Icon(
Icons.check,
size: size / 2,
color: Colors.white,
)
: null,
);
}
}
Loading

0 comments on commit 34e7f55

Please sign in to comment.