Skip to content

Instantly share code, notes, and snippets.

@polonski
Last active August 20, 2023 10:35
Show Gist options
  • Save polonski/ffde6670eea0448fe5eae23138b33905 to your computer and use it in GitHub Desktop.
Save polonski/ffde6670eea0448fe5eae23138b33905 to your computer and use it in GitHub Desktop.
hollow-glimmer-3946
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
void main() => runApp(const MyPage());
class MyPage extends StatelessWidget {
const MyPage({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => MyCubit(),
child: const MyView(),
);
}
}
class MyView extends StatefulWidget {
const MyView({super.key});
@override
State<MyView> createState() => MyApp();
}
class MyApp extends State<MyView> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Input Controller Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final myNameController = TextEditingController()
..text = BlocProvider.of<MyCubit>(context)
.state
.myName;
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: myNameController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Label',
),
onChanged: (value) {
// myNameController.text = value;
context
.read<MyCubit>()
.setMyName(value);
},
),
],
),
),
);
}
}
class MyCubit extends Cubit<MyState> {
MyCubit() : super(const MyState());
void reset() => emit(
const MyState(
myName: '',
),
);
void setMyName(String myName) {
//blergh - plz fix
final _myName = '${state.myName}${myName.split(state.myName)[0]}';
print(myName);
emit(
MyState(
myName: myName,
),
);
}
}
class MyState extends Equatable {
const MyState({
this.myName = '',
});
final String myName;
@override
List<Object?> get props => [
myName,
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment