Ask Your Question
3

How to include action icons to the AppBar in Flutter when utilizing the go_router?

asked 2023-06-11 19:33:06 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-11 19:37:02 +0000

bukephalos gravatar image

To include action icons to the AppBar when using the go_router in Flutter, you can use the AppBar's actions parameter. Here's an example code snippet:

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

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

class MyApp extends StatelessWidget {
  final router = GoRouter(
    routes: [
      GoRoute(
        path: '/',
        pageBuilder: (context, state) => MyHomePage(),
      ),
    ],
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerDelegate: router.delegate(),
      routeInformationParser: router.routeInformationParser(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My App'),
        actions: [
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {},
          ),
          IconButton(
            icon: Icon(Icons.more_vert),
            onPressed: () {},
          ),
        ],
      ),
      body: Center(
        child: Text('Hello, World!'),
      ),
    );
  }
}

In this example, we added two IconButton widgets to the actions parameter of the AppBar. The first IconButton uses the Icons.search icon and the second IconButton uses the Icons.more_vert icon. Both icons have an empty onPressed function for demonstration purposes.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-11 19:33:06 +0000

Seen: 13 times

Last updated: Jun 11 '23