Last active
October 21, 2025 07:48
-
-
Save vlasentiy/ddf2fd27118a455a0ccc853ad91cf25d to your computer and use it in GitHub Desktop.
Problematic Widget
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
| // ignore_for_file: non_constant_identifier_names | |
| import 'dart:async'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:flutter_bloc/flutter_bloc.dart'; | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return const MaterialApp(home: CompactProblematicWidget()); | |
| } | |
| } | |
| // --- Data & Service --- | |
| class AuthServiceImpl { | |
| Future<String> getLoggedInUser() async => 'Taras Shevchenko'; | |
| } | |
| class ListItem { | |
| final int id; | |
| String value; | |
| ListItem(this.id, this.value); | |
| } | |
| // --- BLoC Layer --- | |
| // BLoC Events | |
| abstract class ItemsEvent {} | |
| class LoadItems extends ItemsEvent {} | |
| class UpdateItem extends ItemsEvent { | |
| final int id; | |
| final BuildContext context; | |
| UpdateItem(this.id, this.context); | |
| } | |
| // BLoC State | |
| class ItemsState { | |
| bool isLoading; | |
| String user; | |
| List<ListItem> list_of_items; | |
| ItemsState({ | |
| this.isLoading = false, | |
| this.user = '', | |
| List<ListItem>? listOfItems, | |
| }) : list_of_items = listOfItems ?? []; | |
| } | |
| class ItemsBloc extends Bloc<ItemsEvent, ItemsState> { | |
| final AuthServiceImpl _authService = AuthServiceImpl(); | |
| ItemsBloc() : super(ItemsState()) { | |
| on<LoadItems>((event, emit) async { | |
| emit(ItemsState(isLoading: true)); | |
| final user = await _authService.getLoggedInUser(); | |
| final items = List.generate(7, (i) => ListItem(i, 'Item $i')); | |
| emit(ItemsState(isLoading: false, user: user, listOfItems: items)); | |
| }); | |
| on<UpdateItem>((event, emit) { | |
| final index = state.list_of_items.indexWhere( | |
| (item) => item.id == event.id, | |
| ); | |
| if (index != -1) { | |
| state.list_of_items[index].value = | |
| 'Updated @ ${TimeOfDay.now().format(event.context)}'; | |
| } | |
| emit(state); | |
| }); | |
| } | |
| Future<String> getUserWelcomeText() async { | |
| await Future.delayed(const Duration(seconds: 1)); | |
| return '${await _authService.getLoggedInUser()}, welcome to the app!'; | |
| } | |
| } | |
| // --- UI Layer --- | |
| class CompactProblematicWidget extends StatelessWidget { | |
| const CompactProblematicWidget({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return BlocProvider( | |
| create: (_) => ItemsBloc()..add(LoadItems()), | |
| child: const _ProblematicView(), | |
| ); | |
| } | |
| } | |
| class _ProblematicView extends StatefulWidget { | |
| const _ProblematicView(); | |
| @override | |
| State<_ProblematicView> createState() => _ProblematicViewState(); | |
| } | |
| class _ProblematicViewState extends State<_ProblematicView> { | |
| final _controller = TextEditingController(); | |
| @override | |
| Widget build(BuildContext context) { | |
| final state = context.watch<ItemsBloc>().state; | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(state.isLoading ? 'Loading...' : 'Welcome, ${state.user}'), | |
| ), | |
| body: Column( | |
| children: [ | |
| TextField( | |
| controller: _controller, | |
| decoration: const InputDecoration(hintText: 'Filter...'), | |
| ), | |
| Expanded( | |
| child: ListView.builder( | |
| itemCount: state.list_of_items.length, | |
| itemBuilder: (context, index) { | |
| final item = state.list_of_items[index]; | |
| return ListTile( | |
| title: Text(item.value), | |
| trailing: ElevatedButton( | |
| onPressed: () => context.read<ItemsBloc>().add( | |
| UpdateItem(item.id, context), | |
| ), | |
| child: const Text('Update'), | |
| ), | |
| ); | |
| }, | |
| ), | |
| ), | |
| ElevatedButton(onPressed: _showDialog, child: const Text('Dialog')), | |
| ], | |
| ), | |
| ); | |
| } | |
| Future<void> _showDialog() async { | |
| final _bloc = context.read<ItemsBloc>(); | |
| final _text = await _bloc.getUserWelcomeText(); | |
| showDialog( | |
| context: context, | |
| builder: (ctx) => AlertDialog(title: Text(_text)), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment