Created
December 6, 2024 19:24
-
-
Save VictorCostaSantos/e40738e499730dab770691c2603e2fc0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| import 'package:isar/isar.dart'; | |
| import 'package:path_provider/path_provider.dart'; | |
| part 'main.g.dart'; | |
| void main() async { | |
| WidgetsFlutterBinding.ensureInitialized(); | |
| final dir = await getApplicationDocumentsDirectory(); | |
| final isar = await Isar.open([ProdutoSchema], | |
| directory: dir.path, | |
| inspector: true, | |
| ); | |
| runApp(MyApp(isar: isar)); | |
| } | |
| class MyApp extends StatelessWidget { | |
| final Isar isar; | |
| const MyApp({Key? key, required this.isar}) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| debugShowCheckedModeBanner: false, | |
| title: 'Lista de Produtos', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: ProdutoScreen(isar: isar), | |
| ); | |
| } | |
| } | |
| class ProdutoScreen extends StatefulWidget { | |
| final Isar isar; | |
| const ProdutoScreen({Key? key, required this.isar}) : super(key: key); | |
| @override | |
| _ProdutoScreenState createState() => _ProdutoScreenState(); | |
| } | |
| class _ProdutoScreenState extends State<ProdutoScreen> { | |
| final TextEditingController _produtoController = TextEditingController(); | |
| void _adicionarProduto(String nomeProduto) async { | |
| final produto = Produto(nome: nomeProduto); | |
| await widget.isar.writeTxn(() async { | |
| await widget.isar.produtos.put(produto); | |
| }); | |
| setState(() {}); | |
| _produtoController.clear(); | |
| } | |
| void _removerProduto(Produto produto) async { | |
| await widget.isar.writeTxn(() async { | |
| await widget.isar.produtos.delete(produto.id!); | |
| }); | |
| setState(() {}); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Lista de Produtos'), | |
| ), | |
| body: Column( | |
| children: [ | |
| Padding( | |
| padding: const EdgeInsets.all(8.0), | |
| child: TextField( | |
| controller: _produtoController, | |
| decoration: const InputDecoration( | |
| labelText: 'Nome do Produto', | |
| border: OutlineInputBorder(), | |
| ), | |
| ), | |
| ), | |
| ElevatedButton( | |
| onPressed: () { | |
| if (_produtoController.text.isNotEmpty) { | |
| _adicionarProduto(_produtoController.text); | |
| } | |
| }, | |
| child: const Text('Adicionar Produto'), | |
| ), | |
| Expanded( | |
| child: FutureBuilder<List<Produto>>( | |
| future: widget.isar.produtos.where().findAll(), | |
| builder: (context, snapshot) { | |
| if (snapshot.connectionState == ConnectionState.waiting) { | |
| return const Center(child: CircularProgressIndicator()); | |
| } | |
| if (!snapshot.hasData || snapshot.data!.isEmpty) { | |
| return const Center(child: Text('Nenhum produto adicionado')); | |
| } | |
| final produtos = snapshot.data!; | |
| return ListView.builder( | |
| itemCount: produtos.length, | |
| itemBuilder: (context, index) { | |
| final produto = produtos[index]; | |
| return ListTile( | |
| title: Text(produto.nome), | |
| trailing: IconButton( | |
| icon: const Icon(Icons.delete), | |
| onPressed: () => _removerProduto(produto), | |
| ), | |
| ); | |
| }, | |
| ); | |
| }, | |
| ), | |
| ), | |
| ], | |
| ), | |
| ); | |
| } | |
| } | |
| @Collection() | |
| class Produto { | |
| Id? id; | |
| late String nome; | |
| Produto({required this.nome}); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment