Skip to content

Instantly share code, notes, and snippets.

@lukepighetti
Last active July 5, 2019 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukepighetti/cb007bc754a2cadef72ac83915fec3c9 to your computer and use it in GitHub Desktop.
Save lukepighetti/cb007bc754a2cadef72ac83915fec3c9 to your computer and use it in GitHub Desktop.
Provider to bottomSheet
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<AppState>(
builder: (_) => AppState(),
child: MaterialApp(
title: 'Sandbox',
home: Scaffold(
body: Body(),
),
),
);
}
}
class AppState with ChangeNotifier {
bool _isAwesome = true;
bool get isAwesome => _isAwesome;
String get awesomeText => _isAwesome ? "Totally awesome" : "Not so great";
toggleAwesome() {
_isAwesome = !_isAwesome;
notifyListeners();
}
}
class Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(Provider.of<AppState>(context).awesomeText),
RaisedButton(
child: Text("Toggle awesomeness"),
onPressed: Provider.of<AppState>(context).toggleAwesome,
),
RaisedButton(
child: Text("Open bottom sheet"),
onPressed: () {
showBottomSheet(
context: context, builder: (_) => BottomSheet());
},
)
],
),
),
);
}
}
class BottomSheet extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.grey,
padding: EdgeInsets.all(38),
alignment: Alignment.bottomCenter,
height: 100,
child: Text(Provider.of<AppState>(context).awesomeText),
);
}
}
name: sandbox
description: A new Flutter project.
dependencies:
flutter:
sdk: flutter
provider: ^3.0.0+1
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment