如何在Flutter的ListView Builder区域之外显示列表数据?

huangapple go评论61阅读模式
英文:

How to show the data of the list outside of the area of ListView Builder in Flutter?

问题

以下是您要翻译的内容:

"Is it technically impossible to show the data outside of the list? I searched through the internet but I couldn't get any answers at all smh -_-"

"我是否在列表之外显示数据在技术上是不可能的?我在互联网上搜索,但我完全找不到答案 smh -_-"

"I wanted to display the value of data rows of the list besides of the section ListViewBuilder."

"我想在ListViewBuilder部分之外显示列表的数据行的值。"

"Output:"

"输出:"

"> [ ListView Builder Screen ]"

"> [ ListView Builder 屏幕 ]"

"> Name: You, Age: 20 Name: Him, Age: 20"

"> 名称:You,年龄:20 名称:Him,年龄:20"

"An Output photo there."
"在此处输出照片。"

"在那里输出一张照片。"

"" ""

"String name = userList[index].name;"
"int? age = userList[index].age;"
"```"

"String name = userList[index].name;"
"int? age = userList[index].age;"
"```"

"class _Passenger extends State<Passenger> {"
"TextEditingController nameController = TextEditingController();"
"TextEditingController ageController = TextEditingController();"
"int currentIndex = 0;"
"final form = GlobalKey<FormState>();"
"bool update = false;"
"final User user = User(name: "", age: int.tryParse(''));"
"List<User> userList = ["
"User(name: "You", age: 20),"
"User(name: "Him", age: 20),"
"];"
"String text = '';"
"int? number = int.tryParse('');"
"@override"
"Widget build(BuildContext context) {"
"return MaterialApp( debugShowCheckedModeBanner: false,"
"home: Scaffold("
"body: Column(children: <Widget>["
"Column("
"children: <Widget>["
"Container("
"height: 550,"
"decoration: BoxDecoration(border: Border.all(color: Colors.black)),"
"child: ListView.builder("
"itemCount: userList.length,"
"itemBuilder: (context, index) {"
"String name = userList[index].name;"
"int? age = userList[index].age;"
"return SizedBox("
"width: 20,"
"child: Card("
"color: Colors.grey,"
"child: Padding("
"padding: const EdgeInsets.all(2.0),"
"child: ListTile("
"title: Text( "Name: $name Age: $age"),"
")),"
")),"
"}),"
"),"
"],"
"),"
"Container( child: Text("Display the data here, How?") ),"
"//Add Button"
"Container("
"width: 150,"
"height: 50,"
"margin: const EdgeInsets.all(10),"
"child: ElevatedButton("
"onPressed: () {"
"showDialog("
"context: context,"
"builder: (context) => SimpleDialog(children: ["
"TextField("
"decoration: const InputDecoration(labelText: 'Name'),"
"onChanged: (value) {"
"setState(() {"
"text = value;"
"});"
"},"
"),"
"TextField("
"keyboardType: TextInputType.number,"
"decoration: const InputDecoration(labelText: 'Age'),"
"onChanged: (value) {"
"setState(() {"
"number = int.parse(value);"
"});"
"},"
"),"
"ElevatedButton("
"onPressed: () {"
"setState(() {"
"userList.add(User(name: text, age: number));"
"});"
"},"
"child: const Text('Add'))"
"]));"
"},"
"child: const Text("Add"),"
")),"
"])));"
"}"
"}"

"class User {"
"String name;"
"int? age;"
"User({"
"required this.name,"
"this.age,"
"});"
"}"

英文:

Is it technically impossible to show the data outside of the list? I searched through the internet but I couldn't get any answers at all smh -_-

I wanted to display the value of data rows of the list besides of the section ListViewBuilder.

Output:

> [ ListView Builder Screen ]

> Name: You, Age: 20 Name: Him, Age: 20

An Output photo there.
enter image description here

String name = userList[index].name;
int? age = userList[index].age;
class _Passenger extends State&lt;Passenger&gt; {
  TextEditingController nameController = TextEditingController();
  TextEditingController ageController = TextEditingController();
  int currentIndex = 0;

  final form = GlobalKey&lt;FormState&gt;();
  bool update = false;
  final User user = User(name: &quot;&quot;, age: int.tryParse(&#39;&#39;));
  List&lt;User&gt; userList = [
    User(name: &quot;You&quot;, age: 20),
    User(name: &quot;Him&quot;, age: 20),
  ];
  String text = &#39;&#39;;
  int? number = int.tryParse(&#39;&#39;);
  @override
  Widget build(BuildContext context) {
    return MaterialApp( debugShowCheckedModeBanner: false,
    home: Scaffold(
        body: Column(children: &lt;Widget&gt;[
      Column(
          children: &lt;Widget&gt;[
            Container(
              height: 550,
              decoration: BoxDecoration(border: Border.all(color: Colors.black)),
              child: ListView.builder(
                  itemCount: userList.length,
                  itemBuilder: (context, index) {
                    String name = userList[index].name;
                    int? age = userList[index].age;

                    return SizedBox(
                      width: 20,
                      child: Card(
                          color: Colors.grey,
                          child: Padding(
                              padding: const EdgeInsets.all(2.0),
                              child: ListTile(
                                title: Text( &quot;Name: $name    Age: $age&quot;),
                              ))),
                    );
                  }),
            ),
          ],
        ),
      Container( child: Text(&quot;Display the data here, How?&quot;) ),
      //Add Button
      Container(
          width: 150,
          height: 50,
          margin: const EdgeInsets.all(10),
          child: ElevatedButton(
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (context) =&gt; SimpleDialog(children: [
                        TextField(
                          decoration: const InputDecoration(labelText: &#39;Name&#39;),
                          onChanged: (value) {
                            setState(() {
                              text = value;
                            });
                          },
                        ),
                        TextField(
                          keyboardType: TextInputType.number,
                          decoration: const InputDecoration(labelText: &#39;Age&#39;),
                          onChanged: (value) {
                            setState(() {
                              number = int.parse(value);
                            });
                          },
                        ),
                        ElevatedButton(
                            onPressed: () {
                              setState(() {
                                userList.add(User(name: text, age: number));
                              });
                            },
                            child: const Text(&#39;Add&#39;))
                      ]));
            },
            child: const Text(&quot;Add&quot;),
          )),
    ])));
  }
}

class User {
  String name;
  int? age;
  User({
    required this.name,
    this.age,
  });
}

答案1

得分: 1

所以...如果是相同的列表,只需添加这个:

而不是:

   Container( child: Text("在这里显示数据,如何做?") )

做:

          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: userList
                  .map((user) => Text("姓名:${user.name},年龄:${user.age} "))
                  .toList(),
            ),
          ),

我添加了一个具有水平滚动的SingleChildScrollView以避免问题。

要将用户列表发送到另一页,只需执行:

Navigator.push(
  context,
  MaterialPageRoute(
    settings: const RouteSettings(name: "no-route"),
    builder: (context) => OtherPage(userList: userList),
  ),
);


class OtherPage extends StatelessWidget {
  final List<User> userList;

  const OtherPage({
    required this.userList,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: userList
                    .map(
                        (user) => Text("姓名:${user.name},年龄:${user.age} "))
                    .toList(),
              ),
    );
  }
}
英文:

So... if it's the same list, just add this :

Instead :

   Container( child: Text(&quot;Display the data here, How?&quot;) )

Do :

          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: userList
                  .map((user) =&gt; Text(&quot;Name: ${user.name}, Age: ${user.age} &quot;))
                  .toList(),
            ),
          ),

I added a SingleChildScrollView with horizontal scroll to avoid problems

To send the list of users to another page, just do :

Navigator.push(
  context,
  MaterialPageRoute(
    settings: const RouteSettings(name: &quot;no-route&quot;),
    builder: (context) =&gt; OtherPage(userList: userList),
  ),
);


class OtherPage extends StatelessWidget {
  final List&lt;User&gt; userList;

  const OtherPage({
    required this.userList,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: userList
                    .map(
                        (user) =&gt; Text(&quot;Name: ${user.name}, Age: ${user.age} &quot;))
                    .toList(),
              ),
    );
  }
}

答案2

得分: 0

如果您想显示列表的行数,请将以下代码部分替换为:

Text("行数:${userList.length}")

如果您想要在列表的末尾添加一些内容,可以像这样操作:

ListView(
  shrinkWrap: true,
  physics: const AlwaysScrollableScrollPhysics(),
  children: <Widget>[
    ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      // 其他 ListView.builder 中的内容
    ),
    Text("在可滚动列表内的不同内容")
  ]
)

如果这不是您想要的,请编辑问题并明确所需的结果。

英文:

If you want to display the number of rows of your list, replace

Container( child: Text(&quot;Display the data here, How?&quot;) )

with

Text(&quot;Number of rows: ${userList.length}&quot;)

If you want to add something to the end of the list, you can do something like this:

ListView(
      shrinkWrap: true,
      physics: const AlwaysScrollableScrollPhysics(),
      children: &lt;Widget&gt;[
        ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                // everything else from your ListView.builder
        ),
        Text(&quot;Something different, inside scrollable list&quot;)

If that is not what you want, please edit the question and make clear what is intended results.

huangapple
  • 本文由 发表于 2023年6月1日 21:34:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定