Create custom book details app bar
Presentation (Building the UI)
Summary
This lesson focuses on building the user interface for the book details screen, specifically the custom app bar. It guides through the process of setting up navigation to the new screen using GoRouter, defining the screen's layout with a Column and a custom app bar widget, and incorporating a SafeAreawidget to ensure the UI starts below the system status bar. The instructor emphasizes the importance of maintaining a clean and modular codebase by creating separate widgets for distinct UI components and encourages following design specifications for padding and layout consistency.
Key Ideas
- Use
GestureDetectorwith anonTapcallback to trigger navigation to a new screen. - GoRouter's
pushmethod allows navigating to a specific path defined in the application's router configuration. - Define route paths as constants in a dedicated
AppRouterclass for better manageability and consistency. - A
Scaffoldwidget provides the basic structure for a screen, with thebodyproperty containing the main UI content. SafeAreais essential for preventing UI elements from being obscured by system-level features like the status bar or notch.- A
Columnwidget is used for vertical layout, while aRowis suitable for arranging items horizontally, such as icons in an app bar. IconButtonprovides a clickable icon with built-in padding and a visual ripple effect on press.- Use
MainAxisAlignment.spaceBetweenin aRowto push children to the extreme ends of the available horizontal space. - Consistent padding based on design specifications ensures a professional and visually appealing user interface.
- Creating separate files for custom widgets promotes code reusability and maintains a clear project structure.
Code Snippets
GestureDetector(
onTap: () {
GoRouter.of(context).push(AppRouter.kBookDetailsView);
},
child: Sizedbox(
height: 125,
child: Row(
// ... list item content
),
),
)abstract class AppRouter {
static const kHomeView = '/homeView';
static const kBookDetailsView = '/bookDetailsView';
static final router = GoRouter(
routes: [
// ... other routes
GoRoute(
path: kBookDetailsView,
builder: (context, state) => const BookDetailsView(),
),
],
);
}class BookDetailsView extends StatelessWidget {
const BookDetailsView({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: SafeArea(
child: BookDetailsViewBody(),
),
);
}
}class CustomBookDetailsAppBar extends StatelessWidget {
const CustomBookDetailsAppBar({super.key});
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {},
icon: const Icon(Icons.close),
),
IconButton(
onPressed: () {},
icon: const Icon(Icons.shopping_cart_outlined),
),
],
);
}
}My Notes
This lesson highlights several practical UI development techniques in Flutter:
- Navigation with GoRouter: The instructor demonstrates how to seamlessly integrate screen navigation using a popular routing package.
- Layout Organization: The use of
Column,Row, and custom widgets showcases a clear approach to structuring complex UI designs. - SafeArea Utility: The lesson emphasizes the importance of
SafeAreafor a consistent user experience across different devices and screen configurations. - Design Fidelity: Paying attention to padding and spacing based on design assets is crucial for achieving the intended visual result.
- Code Modularity: Encouraging the creation of separate widget files for distinct UI components is a fundamental principle of clean and maintainable code.
- [Editor's note] While the instructor uses
pushfor navigation, consider ifgomight be more appropriate depending on your application's navigation stack and deep-linking requirements.