Home > Software engineering >  I can't get data from a widget I've created in flutter
I can't get data from a widget I've created in flutter

Time:07-23

Widget build(BuildContext context) {
return Scaffold(
  body: Container(
    width: MediaQuery.of(context).size.width,
    height: MediaQuery.of(context).size.height,
    decoration: const BoxDecoration(color: Color.fromARGB(255, 80, 51, 51)),
    child: SingleChildScrollView(
      child: Padding(
        padding: EdgeInsets.all(20),
        child: Column(
          children: [studentText(123, "asd", "asd")],
        ),
      ),
    ),
  ),
);

I want to get data from the widget I created to this page inside the application.

Container studentText(int schoolNumber, String name, String surname) {
  return Container(
    width: 50,
    height: 50,
  );
}

This is a widget I created. I am returning two string values ​​and one integer value in the widget. In the first code, I'm trying to get this data with studentText() but it doesn't show this data. I never understood this issue in Flutter. Create a widget, then call it where you want to use it and write its values. It looks very nice and simple in practice, but I have difficulty even doing this simple operation.

CodePudding user response:

The container does not show the values. Try as follows:

Container studentText(int schoolNumber, String name, String surname) {
  return Container(
    width: 50,
    height: 50,
    child:Column(children:[
      Text(schoolNumber.toString()),
      Text(name),
      Text(surname)
    ])
  );
}

CodePudding user response:

instead create widget like this

as a Widget function

Widget studentText(int schoolNumber,String name ,String surname){
return Container(
    width: 50,
    height: 50,
    child:Column(children:[
      Text(schoolNumber.toString()),
      Text(name),
      Text(surname)
    ])
  );
}

you should have return type Widget not Container

or as a class

import 'package:flutter/material.dart';

    class StudentText extends StatelessWidget {
  final int schoolNumber;
  final String name;
  final String surname;

  const StudentText({Key? key, required this.schoolNumber, required this.name, required this.surname}) : super(key: key);
  @override
  Widget build(BuildContext context) =>   Container(
    width: 50,
    height: 50,
    child:Column(children:[
      Text(schoolNumber.toString()),
      Text(name),
      Text(surname)
    ])
  );
}

CodePudding user response:

Container widget does not has the ability to show text, to show text inside the container it needs a Text() widget.

Container studentText(int schoolNumber, String name, String surname) {
  return Container(
    width: 50,
    height: 50,
    child : Text(schoolNumber.toString  "/n " name "\n" surname),
  );
}
  • Related