InlineKeyboard class abstract

Represents an inline keyboard that appears right next to the message it belongs to. This is a utility wrapper around InlineKeyboardMarkup that provides a fluent API for building inline keyboards.

With InlineKeyboard you can create keyboards with buttons that can be pressed inline. This is useful for creating interactive menus, handling callbacks, opening URLs, launching web apps, and more.

Basic Usage

// Create a simple inline keyboard
final keyboard = InlineKeyboard()
  .text("Button 1", "callback_1")
  .text("Button 2", "callback_2")
  .row()
  .url("Visit Website", "https://example.com");

// Send with message
await ctx.reply("Choose an option:", replyMarkup: keyboard);

Advanced Usage

// Create keyboard with various button types
final keyboard = InlineKeyboard()
  .text("Callback", "data")
  .url("Website", "https://example.com")
  .row()
  .webApp("Open App", "https://app.example.com")
  .switchInline("Share", "query")
  .row()
  .login("Login", "https://auth.example.com")
  .pay("Pay Now");

Building from Arrays

// From callback data pairs
final keyboard = InlineKeyboard.from([
  [["Button 1", "data1"], ["Button 2", "data2"]],
  [["Button 3", "data3"]]
]);

// From button objects
final buttons = [
  [InlineKeyboard.buttonText("A", "a"), InlineKeyboard.buttonUrl("B", "https://b.com")],
  [InlineKeyboard.buttonText("C", "c")]
];
final keyboard = InlineKeyboard.fromButtons(buttons);
Implemented types
Available extensions
Annotations
  • @freezed

Constructors

InlineKeyboard({@Default.new([<InlineKeyboardButton>[]]) @JsonKey.new(name: 'inline_keyboard') List<List<InlineKeyboardButton>> inlineKeyboard})
Creates a new InlineKeyboard instance.
const
factory
InlineKeyboard.column(List<List<String>> pairs)
Creates a single-column inline keyboard from label-data pairs.
factory
InlineKeyboard.from(List<List<(String, String)>> source)
Creates an inline keyboard from a 2D array of (text, callbackData) records.
factory
InlineKeyboard.fromButtons(List<List<InlineKeyboardButton>> buttons)
Creates an inline keyboard from a 2D array of InlineKeyboardButton objects.
factory
InlineKeyboard.fromJson(Map<String, dynamic> json)
Creates a new InlineKeyboard from JSON.
factory
InlineKeyboard.grid(List<List<String>> pairs, {required int columns})
Creates an inline keyboard with a grid layout.
factory
InlineKeyboard.singleRow(List<List<String>> pairs)
Creates a single-row inline keyboard from label-data pairs.
factory

Properties

$type String
Runtime type
no setter
buttonCount int
Returns the total number of buttons in the keyboard.
no setter
copyWith → $InlineKeyboardCopyWith<InlineKeyboard>
Create a copy of InlineKeyboard with the given fields replaced by the non-null parameter values.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
inlineKeyboard List<List<InlineKeyboardButton>>
Array of button rows, each represented by an Array of InlineKeyboardButton objects
no setterinherited
isEmpty bool
Returns true if the keyboard has no buttons.
no setter
isNotEmpty bool
Returns true if the keyboard has buttons.
no setter
rowCount int
Returns the number of rows in the keyboard.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(InlineKeyboardButton button) InlineKeyboard
Adds an InlineKeyboardButton to the current row.
addAll(List<InlineKeyboardButton> buttons) InlineKeyboard
Adds multiple buttons to the current row.
addRow(List<InlineKeyboardButton> buttons) InlineKeyboard
Adds a row of buttons to the keyboard.
copyText(String text, {required String copyText, String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds a copy text button to the current row.
game(String text, {CallbackGame? game, String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds a callback game button to the current row.
login(String text, String url, {String? forwardText, String? botUsername, bool? requestWriteAccess, String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds a login button to the current row.
map<TResult extends Object?>(TResult $default(_InlineKeyboard value)) → TResult

Available on InlineKeyboard, provided by the InlineKeyboardPatterns extension

A switch-like method, using callbacks.
map<TResult extends Object?>({required TResult forceReply(ForceReply value), required TResult inlineKeyboard(InlineKeyboardMarkup value), required TResult keyboard(ReplyKeyboardMarkup value), required TResult keyboardRemove(ReplyKeyboardRemove value)}) → TResult

Available on ReplyMarkup, provided by the ReplyMarkupPatterns extension

A switch-like method, using callbacks.
mapOrNull<TResult extends Object?>(TResult? $default(_InlineKeyboard value)?) → TResult?

Available on InlineKeyboard, provided by the InlineKeyboardPatterns extension

A variant of map that fallback to returning null.
mapOrNull<TResult extends Object?>({TResult? forceReply(ForceReply value)?, TResult? inlineKeyboard(InlineKeyboardMarkup value)?, TResult? keyboard(ReplyKeyboardMarkup value)?, TResult? keyboardRemove(ReplyKeyboardRemove value)?}) → TResult?

Available on ReplyMarkup, provided by the ReplyMarkupPatterns extension

A variant of map that fallback to returning null.
maybeMap<TResult extends Object?>(TResult $default(_InlineKeyboard value)?, {required TResult orElse()}) → TResult

Available on InlineKeyboard, provided by the InlineKeyboardPatterns extension

A variant of map that fallback to returning orElse.
maybeMap<TResult extends Object?>({TResult forceReply(ForceReply value)?, TResult inlineKeyboard(InlineKeyboardMarkup value)?, TResult keyboard(ReplyKeyboardMarkup value)?, TResult keyboardRemove(ReplyKeyboardRemove value)?, required TResult orElse()}) → TResult

Available on ReplyMarkup, provided by the ReplyMarkupPatterns extension

A variant of map that fallback to returning orElse.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pay(String text, {bool pay = true, String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds a pay button to the current row.
row() InlineKeyboard
Adds a new row to the keyboard.
switchInline(String text, {String query = "", String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds an inline query button to the current row.
switchInlineChosen(String text, {String query = "", bool? allowUserChats, bool? allowBotChats, bool? allowGroupChats, bool? allowChannelChats, String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds an inline query button for chosen chat type to the current row.
switchInlineCurrentChat(String text, {String query = "", String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds an inline query button for current chat to the current row.
text(String text, String data, {String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds a callback query button to the current row.
toJson() Map<String, dynamic>
Converts the InlineKeyboard to JSON.
toString() String
A string representation of this object.
override
transpose() InlineKeyboard
Creates a new keyboard with the rows and columns transposed.
url(String text, String url, {String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds a URL button to the current row.
webApp(String text, String url, {String? iconCustomEmojiId, StyleType? style}) InlineKeyboard
Adds a web app button to the current row.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

buttonCopyText(String text, {required String copyText}) InlineKeyboardButton
Creates a copy text button.
buttonGame(String text, {CallbackGame? game}) InlineKeyboardButton
Creates a callback game button.
buttonLogin(String text, String url, {String? forwardText, String? botUsername, bool? requestWriteAccess}) InlineKeyboardButton
Creates a login button.
buttonPay(String text, {bool pay = true}) InlineKeyboardButton
Creates a pay button.
buttonSwitchInline(String text, [String query = ""]) InlineKeyboardButton
Creates an inline query button.
buttonSwitchInlineChosen(String text, {String query = "", bool? allowUserChats, bool? allowBotChats, bool? allowGroupChats, bool? allowChannelChats}) InlineKeyboardButton
Creates an inline query button for chosen chat type.
buttonSwitchInlineCurrent(String text, [String query = ""]) InlineKeyboardButton
Creates an inline query button for the current chat.
buttonText(String text, [String? data]) InlineKeyboardButton
Creates a callback query button.
buttonUrl(String text, String url) InlineKeyboardButton
Creates a URL button.
buttonWebApp(String text, String url) InlineKeyboardButton
Creates a web app button.