-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0907b3a
commit f4574a1
Showing
9 changed files
with
670 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class EditMode with ChangeNotifier { | ||
bool _enabled = false; | ||
bool get enabled => _enabled; | ||
bool get notEnabled => !_enabled; | ||
set setEnabled(bool value) { | ||
if (_enabled == value) return; | ||
_enabled = value; | ||
notifyListeners(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:gst_todo/src/common/widgets/coustome_snack_bar.dart'; | ||
import 'package:gst_todo/src/features/content/controllers/edit_mode.dart'; | ||
|
||
class ContentPage extends StatefulWidget { | ||
const ContentPage({Key? key, required this.documentSnapshot}) | ||
: super(key: key); | ||
final QueryDocumentSnapshot<Map<String, dynamic>> documentSnapshot; | ||
|
||
@override | ||
_ContentPageState createState() => _ContentPageState(); | ||
} | ||
|
||
class _ContentPageState extends State<ContentPage> { | ||
final EditMode _editMode = EditMode(); | ||
late final TextEditingController _textController; | ||
@override | ||
void initState() { | ||
_textController = | ||
TextEditingController(text: widget.documentSnapshot.data()["text"]); | ||
super.initState(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
_editMode.dispose(); | ||
_textController.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return AnimatedBuilder( | ||
animation: _editMode, | ||
builder: (context, child) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("Content"), | ||
), | ||
body: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>( | ||
stream: widget.documentSnapshot.reference.snapshots(), | ||
initialData: widget.documentSnapshot, | ||
builder: (context, snapshot) { | ||
return Container( | ||
color: Theme.of(context).disabledColor.withOpacity(0.2), | ||
child: Column( | ||
children: [ | ||
Expanded( | ||
child: ListView( | ||
padding: const EdgeInsets.all(10), | ||
children: [ | ||
Container( | ||
decoration: BoxDecoration( | ||
color: Theme.of(context).cardColor, | ||
borderRadius: BorderRadius.circular(10), | ||
), | ||
constraints: const BoxConstraints(minHeight: 120), | ||
child: Padding( | ||
padding: | ||
EdgeInsets.all(_editMode.enabled ? 15 : 25), | ||
child: Builder(builder: (context) { | ||
if (_editMode.enabled) { | ||
return TextField( | ||
controller: _textController, | ||
maxLines: null, | ||
style: const TextStyle( | ||
fontSize: 18, | ||
), | ||
decoration: InputDecoration( | ||
contentPadding: const EdgeInsets.all(10), | ||
focusedBorder: OutlineInputBorder( | ||
borderSide: const BorderSide( | ||
color: Colors.black, | ||
), | ||
borderRadius: BorderRadius.circular(10), | ||
), | ||
), | ||
); | ||
} else { | ||
return SelectableText( | ||
"${snapshot.data!.data()!["text"]}", | ||
style: const TextStyle(fontSize: 18), | ||
); | ||
} | ||
}), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
Container( | ||
color: Theme.of(context).cardColor, | ||
child: Row( | ||
children: [ | ||
if (_editMode.enabled) ...[ | ||
Expanded( | ||
child: TextButton( | ||
onPressed: () { | ||
_editMode.setEnabled = _editMode.notEnabled; | ||
}, | ||
child: const Text( | ||
"Cancle", | ||
), | ||
style: TextButton.styleFrom( | ||
primary: Theme.of(context).errorColor, | ||
), | ||
), | ||
), | ||
Expanded( | ||
child: TextButton( | ||
onPressed: () { | ||
if (_textController.text.isEmpty) { | ||
ScaffoldMessenger.of(context) | ||
.hideCurrentSnackBar(); | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
CoustomeSnackBar( | ||
context, | ||
content: const Text( | ||
"! Text is Empty. Failed"), | ||
isFailed: true, | ||
), | ||
); | ||
return; | ||
} | ||
snapshot.data!.reference | ||
.update({"text": _textController.text}); | ||
_editMode.setEnabled = _editMode.notEnabled; | ||
}, | ||
child: const Text("Save"), | ||
), | ||
), | ||
], | ||
if (_editMode.notEnabled) ...[ | ||
Expanded( | ||
child: TextButton( | ||
onPressed: () { | ||
_textController.text = | ||
snapshot.data!.data()!["text"] ?? ""; | ||
_editMode.setEnabled = _editMode.notEnabled; | ||
}, | ||
child: const Text("Edit"), | ||
), | ||
), | ||
], | ||
], | ||
), | ||
) | ||
], | ||
), | ||
); | ||
}, | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.