0

I'm using Provider in Flutter for state management. And I want to display some text in my widget using this Provider. The test is shown but is looks very strange height or padding I don't know. So here is a code.

class JobDetailsScreen extends HookWidget {
  const JobDetailsScreen({required this.id, Key? key}) : super(key: key);

  final String id;

  @override
  Widget build(BuildContext context) {
    final job = Provider.of<JobsNotifier>(context).currentJob;
    var loading = Provider.of<JobsNotifier>(context).isLoadingCurrentJob;
    useEffect(() {
      if (job == null) {
        Future.microtask(() async {
          await Provider.of<JobsNotifier>(context, listen: false)
              .getCurrentJob(id);
        });
      }
      return () => {};
    }, [id]);
    return Scaffold(
      appBar: const NavBarTop(
        title: 'Job Details',
        innerAppBar: true,
      ),
      body: loading
          ? const Center(
              child: CircularProgressIndicator(),
            )
          : SingleChildScrollView(
              child: Container(
                padding: const EdgeInsets.all(15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(job.title,
                        style: const TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 24)),
                    Text(job.company,
                        style: const TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 18)),
                    ElevatedButton(
                      onPressed: () async {
                        try {
                          await openUrl(job.applyUrl!);
                        } on String catch (e) {
                          ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                            content: Text(
                              e,
                              style: const TextStyle(color: Colors.white),
                            ),
                            backgroundColor: Colors.red,
                          ));
                        }
                      },
                      style: ButtonStyle(
                          minimumSize: MaterialStateProperty.all(
                              const Size(double.infinity, 50))),
                      child: const Text('Apply'),
                    ),
                  ],
                ),
              ),
            ),
    );
  }
}

And I see this on my screen

Screen with wrong text behaviour

I'm expecting widget to show text the right way without any paading and some redundant height. It should be like this:

Right text behaviour screen

  • Similar code works fine for me. Maybe it's because of the parent of this Widget. I just used MaterialApp. What are you using? – Ivo Feb 13 '23 at 08:46
  • @ivo yes, Material App as well. But as I said below the it seems to me that the issue is in dynamic values which is used in Text widget -> Text(job.title, styles...) – Dantes1993 Feb 13 '23 at 09:02
  • Maybe the response has newlines? Like maybe job.title equals "\nUI/UI Design Lead\n\n". Try use `job.title.trim()` maybe – Ivo Feb 13 '23 at 09:05
  • @ivo thanks a lot, man, I completely forgot that I received 'dirty' data because this data is parsed from different websites. Thank you again) – Dantes1993 Feb 13 '23 at 09:39
  • 1
    @Ivo if it's not difficult for you, could you suggest what you wrote as answer? And I would select it as right answer. – Dantes1993 Feb 13 '23 at 09:41

2 Answers2

0

i have tried to run your code, and it seems to work fine without any padding to the text widgets or height, heres's a screenshot:

i believe what you can do is to check through you NavBarTop widget, to see if there could be any property affecting this.

Code test

Godson
  • 117
  • 5
  • As I understood the issue is in dynamic value 'job' which is received from Provider, because in case when I use text value directly or even from variables is looks fine. But once this data is let's say dynamic in runtime, it looks awfull. – Dantes1993 Feb 13 '23 at 08:30
0

The response might contain newlines. Like maybe job.title equals "\nUI/UI Design Lead\n\n".

Try use job.title.trim() so any leading and trailing whitespaces and newlines are removed.

Ivo
  • 18,659
  • 2
  • 23
  • 35