-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from avuenja/14-adicionar-publicar-novo-conteúdo
feat(content): criação de conteúdo pelo app
- Loading branch information
Showing
9 changed files
with
282 additions
and
22 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
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,48 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:tabnews/src/providers/user.dart'; | ||
import 'package:tabnews/src/services/api.dart'; | ||
|
||
class ContentProvider extends ChangeNotifier { | ||
static final ContentProvider _instance = ContentProvider._internal(); | ||
|
||
final api = Api(); | ||
|
||
factory ContentProvider() { | ||
return _instance; | ||
} | ||
|
||
ContentProvider._internal() { | ||
_isLoading = false; | ||
_isCreated = false; | ||
} | ||
|
||
late bool _isLoading; | ||
bool get isLoading => _isLoading; | ||
|
||
late bool _isCreated; | ||
bool get isCreated => _isCreated; | ||
|
||
void _loading(bool value) { | ||
_isLoading = value; | ||
notifyListeners(); | ||
} | ||
|
||
void create(String title, String body, String source) async { | ||
_loading(true); | ||
|
||
var content = await api.postContent( | ||
UserProvider().sessionId, | ||
title, | ||
body, | ||
source, | ||
); | ||
|
||
if (content.id!.isNotEmpty) { | ||
_isCreated = true; | ||
} | ||
|
||
_isLoading = false; | ||
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
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,163 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:markdown_editable_textinput/format_markdown.dart'; | ||
import 'package:markdown_editable_textinput/markdown_text_input.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
import 'package:tabnews/src/constants.dart'; | ||
import 'package:tabnews/src/providers/content.dart'; | ||
import 'package:tabnews/src/ui/layouts/page.dart'; | ||
import 'package:tabnews/src/ui/pages/my_contents.dart'; | ||
import 'package:tabnews/src/ui/widgets/markdown.dart'; | ||
import 'package:tabnews/src/utils/navigation.dart'; | ||
|
||
class NewContentPage extends StatefulWidget { | ||
const NewContentPage({super.key}); | ||
|
||
@override | ||
State<NewContentPage> createState() => _NewContentPageState(); | ||
} | ||
|
||
class _NewContentPageState extends State<NewContentPage> { | ||
TextEditingController titleTextController = TextEditingController(); | ||
TextEditingController bodyTextController = TextEditingController(); | ||
TextEditingController sourceTextController = TextEditingController(); | ||
final _formKey = GlobalKey<FormState>(); | ||
|
||
bool isViewMarkdown = false; | ||
|
||
@override | ||
void dispose() { | ||
titleTextController.dispose(); | ||
bodyTextController.dispose(); | ||
sourceTextController.dispose(); | ||
|
||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return PageLayout( | ||
onRefresh: () async {}, | ||
body: SingleChildScrollView( | ||
child: Padding( | ||
padding: const EdgeInsets.all(15.0), | ||
child: Form( | ||
key: _formKey, | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||
children: [ | ||
TextFormField( | ||
cursorColor: AppColors.primaryColor, | ||
decoration: const InputDecoration( | ||
focusedBorder: UnderlineInputBorder( | ||
borderSide: BorderSide( | ||
color: AppColors.primaryColor, | ||
width: 2.0, | ||
), | ||
), | ||
hintText: 'Título', | ||
), | ||
controller: titleTextController, | ||
), | ||
const SizedBox(height: 15.0), | ||
isViewMarkdown | ||
? SizedBox( | ||
height: 420.0, | ||
child: MarkedownReader(body: bodyTextController.text), | ||
) | ||
: MarkdownTextInput( | ||
maxLines: 20, | ||
label: 'Conteúdo', | ||
controller: bodyTextController, | ||
actions: MarkdownType.values, | ||
(value) {}, | ||
bodyTextController.text, | ||
), | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.end, | ||
children: [ | ||
TextButton( | ||
onPressed: () { | ||
setState(() { | ||
isViewMarkdown = !isViewMarkdown; | ||
}); | ||
}, | ||
child: Text(isViewMarkdown ? 'Escrever' : 'Visualizar'), | ||
), | ||
], | ||
), | ||
const SizedBox(height: 15.0), | ||
TextFormField( | ||
cursorColor: AppColors.primaryColor, | ||
decoration: const InputDecoration( | ||
focusedBorder: UnderlineInputBorder( | ||
borderSide: BorderSide( | ||
color: AppColors.primaryColor, | ||
width: 2.0, | ||
), | ||
), | ||
hintText: 'Fonte (opcional)', | ||
), | ||
controller: sourceTextController, | ||
), | ||
const SizedBox(height: 30.0), | ||
Consumer<ContentProvider>( | ||
builder: (context, provider, _) => ElevatedButton( | ||
style: ButtonStyle( | ||
elevation: MaterialStateProperty.all<double>(0.0), | ||
backgroundColor: MaterialStateProperty.all<Color>( | ||
AppColors.primaryColor.withOpacity( | ||
provider.isLoading ? 0.5 : 1.0, | ||
), | ||
), | ||
foregroundColor: MaterialStateProperty.all<Color>( | ||
Colors.white, | ||
), | ||
shape: MaterialStateProperty.all<OutlinedBorder>( | ||
RoundedRectangleBorder( | ||
borderRadius: BorderRadius.circular(4.0), | ||
), | ||
), | ||
), | ||
onPressed: provider.isLoading | ||
? null | ||
: () { | ||
if (titleTextController.text.isEmpty || | ||
bodyTextController.text.isEmpty) { | ||
return; | ||
} | ||
|
||
provider.create( | ||
titleTextController.text, | ||
bodyTextController.text, | ||
sourceTextController.text, | ||
); | ||
|
||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar( | ||
content: Text( | ||
'Conteúdo publicado com sucesso!', | ||
), | ||
), | ||
); | ||
|
||
Navigation.pop(context); | ||
Navigation.push(context, const MyContentsPage()); | ||
}, | ||
child: Text( | ||
provider.isLoading ? 'Aguarde...' : 'Publicar', | ||
style: const TextStyle().copyWith( | ||
fontSize: 16.0, | ||
), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
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
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.