Skip to content

Instantly share code, notes, and snippets.

@Metal-666
Created January 11, 2022 12:04
Show Gist options
  • Select an option

  • Save Metal-666/16770c6b47fae409c3846e65cf7fa772 to your computer and use it in GitHub Desktop.

Select an option

Save Metal-666/16770c6b47fae409c3846e65cf7fa772 to your computer and use it in GitHub Desktop.
Challenge 2 (BLoC)
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) => BlocProvider(
create: (BuildContext context) => RootBloc(1),
child: MaterialApp(
home: Scaffold(body: MyWidget()),
));
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) => Stack(children: <Widget>[
Center(
child: BlocBuilder<RootBloc, RootState>(
builder: (context, state) => Transform.scale(
scale: state.scale,
child: Container(
width: 100,
height: 100,
color: Colors.yellow,
),
))),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: BlocBuilder<RootBloc, RootState>(
builder: (context, state) => Slider(
value: state.scale,
max: 2,
onChanged: (newValue) =>
context.read<RootBloc>().add(MoveSlider(newValue)),
),
))
]);
}
//------------------------------Events------------------------------
abstract class RootEvents {}
class MoveSlider extends RootEvents {
final double newValue;
MoveSlider(this.newValue);
}
//------------------------------Events------------------------------
//------------------------------States------------------------------
class RootState {
final double scale;
RootState(this.scale);
}
//------------------------------States------------------------------
//-------------------------------Bloc-------------------------------
class RootBloc extends Bloc<RootEvents, RootState> {
RootBloc(double initialValue) : super(RootState(initialValue)) {
on<MoveSlider>((event, emit) => emit(RootState(event.newValue)));
}
}
//-------------------------------Bloc-------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment