如何在 flutter 中将类别列表添加到我的正文中?拜托,我是新来的

问题描述 投票:0回答:1

导入'包:flutter/material.dart'; 导入'包:hlibrary/main.dart';

类面板扩展StatelessWidget { const Panel({Key? key});

@覆盖 小部件构建(BuildContext 上下文){

return Stack(
  children: [
    Scaffold(
      appBar: AppBar(
        centerTitle: false,
        title: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
          Text("Hlibrary",
          style: Theme.of(context).textTheme.bodyLarge!.copyWith(
            color: Colors.orangeAccent,
          ),),
          Text("Admin Panel",
          style: Theme.of(context).textTheme.titleLarge!.copyWith(
            color: Colors.orangeAccent,
          ),),
        ],)
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){},
        backgroundColor: Colors.orangeAccent,
        child: const Icon(Icons.add),
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            const DrawerHeader(
              decoration: BoxDecoration(
                color: Color(0xFFFFB800),
              ),
              child: Text(
                'Admin Tengis',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                ),
              ),
            ),
            ListTile(
              leading: Icon(Icons.add),
              title: const Text('Add'),
              onTap: () {
                // Handle Option 1
              },
            ),
            ListTile(
              leading: Icon(Icons.list),
              title: const Text('Added files'),
              onTap: () {
                // Handle Option 2
              },
            ),
            ListTile(
              leading: Icon(Icons.arrow_back),
              title: const Text('Go back'),
              onTap: () {
                Navigator.pop(context);
              },
            ),
            // Add more ListTile for additional options
          ],
        ),
      ),
      body:  ListView(
        
      ),
    ),
  ],
);

} }

enter image description here 它看起来像这样,当我点击右下角的添加按钮时,我想添加书籍类别

flutter dart listview categories
1个回答
0
投票

您可以这样处理,只需创建一个列表来存储选定的类别并显示该列表即可。

无论这些类别在哪里,都显示在抽屉中甚至正文中。

      class HomeScreen extends StatefulWidget{

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
List<String> catgs = [];

        @override
        Widget build(BuildContext context){
        
        return Scaffold(

          body: Column(
            children: [
              ListTile(
                title: Text('Science'),
                onTap: (){
                  setState((){
                    catgs.add('Science');
                  });
                },
              ),

              ListTile(
                title: Text('Sports'),
                onTap: (){
                  setState((){
                    catgs.add('Sports');
                  });
                },
              ),
              if(catgs.isNotEmpty)
              Expanded(
                child: ListView(
                  children: catgs.map((e)=> ListTile(title: Text(e),)).toList(),
                ),
              )
            ],
          )
        );
        }
}

弄清楚这个想法是如何运作的,然后应用它。

© www.soinside.com 2019 - 2024. All rights reserved.