Skip to content

Instantly share code, notes, and snippets.

@thisisyusub
Created August 24, 2024 10:42
Show Gist options
  • Save thisisyusub/2690ec83c664335f21a42530f0d435c4 to your computer and use it in GitHub Desktop.
Save thisisyusub/2690ec83c664335f21a42530f0d435c4 to your computer and use it in GitHub Desktop.
import 'package:di_demo/app_kinject_observer.dart';
import 'package:di_demo/counter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:kinject/kinject.dart';
void main() {
Kinject.observer = AppKinjectObserver();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Injector Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
child: const Text('Go to Counter Page'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) {
return Kinject(
factory: (_) => CounterBloc(),
builder: (_) => const CounterPage(),
);
},
),
);
},
),
),
);
}
}
class CounterPage extends StatelessWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context) {
final counterBloc = context.resolve<CounterBloc>();
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Counter'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
StreamBuilder<int>(
stream: counterBloc.count,
builder: (context, snapshot) {
return Text(
'${snapshot.data}',
style: Theme.of(context).textTheme.headlineMedium,
);
},
),
],
),
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FloatingActionButton(
onPressed: counterBloc.decrease,
heroTag: 1,
child: const Icon(Icons.remove),
),
const SizedBox(width: 16),
FloatingActionButton(
onPressed: counterBloc.increase,
heroTag: 2,
child: const Icon(Icons.add),
),
],
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment