Skip to content

Instantly share code, notes, and snippets.

@aadomin
Created April 11, 2022 05:50
Show Gist options
  • Select an option

  • Save aadomin/c086c3edf3b5ab7ff0842434594d0754 to your computer and use it in GitHub Desktop.

Select an option

Save aadomin/c086c3edf3b5ab7ff0842434594d0754 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
inputDecorationTheme:
const InputDecorationTheme(border: OutlineInputBorder()),
),
home: createScreenExampleScreen(),
);
}
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Widget createScreenExampleScreen() {
return ChangeNotifierProvider(
create: (context) => ExampleScreenVM(context: context),
child: Builder(
builder: (context) => ExampleScreen(
viewModel: context.watch<ExampleScreenVM>(),
),
),
);
}
class ExampleScreen extends StatefulWidget {
const ExampleScreen({
required this.viewModel,
Key? key,
}) : super(key: key);
final ExampleScreenVM viewModel;
@override
State<ExampleScreen> createState() => _ExampleScreenState();
}
class _ExampleScreenState extends State<ExampleScreen> {
late final ___viewModel = widget.viewModel;
@override
void initState() {
super.initState();
___viewModel.initVM();
}
@override
void dispose() {
___viewModel.disposeVM();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text(___viewModel.i.toString()),
floatingActionButton: FloatingActionButton(
onPressed: () => ___viewModel.inc(),
),
);
}
}
class ExampleScreenVM with ChangeNotifier {
ExampleScreenVM({required this.context});
BuildContext context;
int i = 0;
void inc() {
i++;
notifyListeners();
}
void initVM() {}
void disposeVM() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment