Flutter Bloc.nvim is a Neovim replacement for the bloc extension in Vscode that provides easy bloc template generation and wrapping with bloc widgets. For more information on Flutter and Bloc, please checkout the following links:
example.mp4
-
Bloc Template Generation: Quickly generate boilerplate code for your BLoC classes, including events, states, and the bloc itself.
-
Bloc Widget Wrapping: Effortlessly wrap your Flutter widgets with bloc-related classes, such as
BlocBuilder
andBlocProvider
, with a few simple commands. Bloc.nvim eliminates the hassle of manually importing and setting up these widgets, allowing you to focus on your app's core logic.
-
Make sure you have Neovim installed on your system. You can download it from the official Neovim website.
-
Install the BLoc.nvim plugin with your prefered package manager:
use { 'RobertPietraru/bloc.nvim', requires = { { 'jose-elias-alvarez/null-ls.nvim' }, } }
Plug 'RobertPietraru/bloc.nvim'
-
Configure the language server
-
Open your Flutter project in Neovim.
-
To generate a BLoC template, position your cursor on the line where you want to create the BLoC class.
-
Run the following command:
require('bloc').create_cubit('cubit_name', 'parent/folder/relative/path') require('bloc').create_bloc('bloc_name', 'parent/folder/relative/path')
:lua require('bloc').create_cubit('cubit_name', 'parent/folder/relative/path') :lua require('bloc').create_bloc('bloc_name', 'parent/folder/relative/path')
This will create a new BLoC class file with the necessary boilerplate code, including events, states, and the bloc itself.
Bloc.nvim uses null-ls to add additional actions to the language server's default code actions. Make sure you have it installed
-
To wrap a widget with a bloc-related class, position your cursor on the line containing the widget.
-
Run the following command:
vim.lsp.buf.code_action()
:lua vim.lsp.buf.code_action()
This will show you a list of all possible code actions
Currently you can't do much, but we are on it. Here's my personal config for the plugin using nvim-tree
local tree_api = require('nvim-tree.api')
local bloc = require('bloc')
--- for widget wrapping
bloc.setup()
--- the cubit will be the child of the folder or the sibling of the file
vim.keymap.set("n", "<leader>q", function()
local input = vim.fn.input("name (snake_case): ")
if (input == "")then
print("Aborted cubit creation")
return;
end
local clipboard_before = vim.fn.getreg("*");
tree_api.fs.copy.relative_path()
local clipboard_after = vim.fn.getreg("*");
vim.fn.setreg("*", clipboard_before)
bloc.create_cubit(input, clipboard_after)
end)
vim.keymap.set("n", "<leader>Q", function()
local input = vim.fn.input("name (snake_case): ")
if (input == "")then
print("Aborted bloc creation")
return;
end
local clipboard_before = vim.fn.getreg("*");
tree_api.fs.copy.relative_path()
local clipboard_after = vim.fn.getreg("*");
vim.fn.setreg("*", clipboard_before)
bloc.create_bloc(input, clipboard_after)
end)
-- TODO: remove this if you want, though you should always format after wrapping the widgets
vim.keymap.set("n", "tt", function()
vim.lsp.buf.format()
end)
We welcome contributions to Bloc.nvim! If you encounter any issues or have ideas for enhancements, please submit them to our GitHub repository. We appreciate your feedback and pull requests.
Bloc.nvim is licensed under the MIT License. Feel free to use and distribute this plugin while keeping the license information intact.
Thank you for using Bloc.nvim! We hope this plugin improves your Flutter development experience. If you have any questions or need support, don't hesitate to reach out to us. Happy coding!