簡體   English   中英

Flutter 中的對齊、垂直滾動和響應性

[英]Alignment ,Vertical scrolling and Responsiveness in flutter

問題:

  1. 我一直在嘗試將通知和搜索圖標設置在頁面的右側,但無論我做什么,它總是會改變容器中圖標的位置,即使我將對齊屬​​性賦予容器本身。
  2. 當他們單擊服務/產品時,我只想顯示少數人/產品,如果他們想查看更多內容,請單擊以查看所有內容。
  3. 目前,所有值(“寬度”和“高度”)都是靜態的,所以我在想如何確保應用程序即使在越來越小的屏幕設備中也能響應並處理不同的字體方案(“超小”,“小”、“大”和“特大”)他們選擇了
  4. 我還想在正文中添加更多內容,例如熱門服務和我們的產品,但希望它們可以滾動,因此用戶一開始只能看到熱門服務和我們的產品,然后如果他向下滾動,他可以看到更多內容。

我當前的輸出

在此處輸入圖像描述

目前,如果我嘗試添加更多類別,它會自動使用相同的空間並減小其他類別的大小以適應它在此處輸入圖像描述

我的代碼

class _HomeScreenState extends State<HomeScreen> {
  int _selectedIndex = 0;
  int _selectedCategoryIndex = -1;
  String _selectedUserType = "Maid";
  PageController pageController = PageController();
  List array = ["Maid", "Driver", "Engineer", "Gardener", "Pilot","carpainter", "guard", "plumber"];

  List data = [
    {
      "name": "Sachin Rajput",
      "profilePic":
          "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["Maid", "Engineer"],
      "rating": 5,
      "bg": Colors.red
    },
    {
      "name": "Sachin Tendulkar",
      "profilePic":
          "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["Gardener", "Pilot", "Engineer"],
      "rating": 5,
      "bg": Colors.amberAccent
    },
    {
      "name": "Sachin Test",
      "profilePic":
          "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["carpainter", "guard", "plumber"],
      "rating": 5,
      "bg": Colors.blue
    }
  ];
  List product_data = [
    {
      "name": "P1",
      "profilePic":
          "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["Dusting"],
      "rating": 5,
      "bg": Colors.red
    },
    {
      "name": "P2",
      "profilePic":
          "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["Mopping"],
      "rating": 5,
      "bg": Colors.amberAccent
    },
    {
      "name": "P3",
      "profilePic":
          "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["cleaning"],
      "rating": 5,
      "bg": Colors.blue
    }
  ];
  List filteredData = [];

  void onTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
    pageController.jumpToPage(index);
  }

  void tappedCategory(int index) {
    _selectedCategoryIndex = index;
    _selectedUserType = array[index];
    _filterData();
  }

  @override
  void initState() {
    super.initState();
    _filterData();
  }

  _filterData() {
    filteredData = _selectedCategoryIndex >= 0 ? data.where((element) => element["category"].contains(_selectedUserType)).toList() : data;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(mainAxisSize: MainAxisSize.min, children: [
          Container(
              margin: const EdgeInsets.only(top: 45, bottom: 15),
              padding: const EdgeInsets.only(left: 20, right: 20),
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Column(children: const [
                      Text("Bengaluru"),
                      Text("R.T Nagar")
                    ]),
                    Container(
                      width: 45,
                      height: 45,
                      padding: const EdgeInsets.only(left: 0, right: 0),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),
                          color: Colors.red),
                      child: const Icon(
                        Icons.search,
                        color: Colors.white,
                      ),
                    ),
                    Container(
                      width: 45,
                      height: 45,
                      padding: const EdgeInsets.only(left: 0, right: 0),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),
                          color: Colors.red),
                      child: const Icon(
                        Icons.notifications,
                        color: Colors.white,
                      ),
                    ),
                  ])),
          
          Align(
            alignment: Alignment.centerLeft,
            child: 
              Text(
                  'Popular Services',
                ),
          ),
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: List<Widget>.generate(
                  array.length, // place the length of the array here
                  (int index) {
                return Container(
                  margin: const EdgeInsets.all(2.0),
                  child: GestureDetector(
                    onTap: () {
                      tappedCategory(index);
                    },
                    child: Chip(label: Text(array[index])),
                  ),
                );
              }).toList(),
            ),
          ),
          Expanded(
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: filteredData.length,
              itemBuilder: (context, index) {
                var item = filteredData[index];
                return Container(
                  width: 160.0,
                  height:10,
                  color: item['bg'],
                  child: Center(
                    child: Text(item["name"].toString()),
                  ),
                );
              },
              // This next line does the trick.
            ),
          ),
        Align(
            alignment: Alignment.centerRight,
            child: 
              Text(
                  'View All',
                ),
          ),
        Align(
            alignment: Alignment.centerLeft,
            child: 
              Text(
                  'Popular Products',
                ),
          ),
        Expanded(
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: product_data.length,
              itemBuilder: (context, index) {
                var item = product_data[index];
                return Container(
                  width: 160.0,
                  height:10,
                  color: item['bg'],
                  child: Center(
                    child: Text(item["name"].toString()),
                  ),
                );
              },
              // This next line does the trick.
            ),
          ),
        ]),
        
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
            BottomNavigationBarItem(
                icon: Icon(Icons.cleaning_services), label: 'House Keeping'),
            BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
            BottomNavigationBarItem(
                icon: Icon(Icons.account_balance_wallet), label: 'Wallet'),
            BottomNavigationBarItem(
                icon: Icon(Icons.bookmarks), label: 'Bookmarked'),
            BottomNavigationBarItem(
                icon: Icon(Icons.local_convenience_store), label: 'Store'),
            BottomNavigationBarItem(
                icon: Icon(Icons.notifications), label: 'Notifications'),
            BottomNavigationBarItem(
                icon: Icon(Icons.assessment), label: 'Notifications'),
            BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: Colors.blue,
          unselectedItemColor: Colors.grey,
          onTap: onTapped,
        ));
  }
}

我是新手,所以請原諒我提出的愚蠢問題,並隨時向我詢問有關該問題的任何澄清

用下面的代碼替換你的整個代碼:

*** 主屏幕***

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _selectedIndex = 0;
  int _selectedCategoryIndex = -1;
  String _selectedUserType = "Maid";
  PageController pageController = PageController();
  List array = ["Maid", "Driver", "Engineer", "Gardener", "Pilot","carpainter", "guard", "plumber"];

  List data = [
    {
      "name": "Sachin Rajput",
      "profilePic":
      "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["Maid", "Engineer"],
      "rating": 5,
      "bg": Colors.red
    },
    {
      "name": "Sachin Tendulkar",
      "profilePic":
      "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["Gardener", "Pilot", "Engineer"],
      "rating": 5,
      "bg": Colors.amberAccent
    },
    {
      "name": "Sachin Test",
      "profilePic":
      "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["carpainter", "guard", "plumber"],
      "rating": 5,
      "bg": Colors.blue
    }
  ];
  List product_data = [
    {
      "name": "P1",
      "profilePic":
      "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["Dusting"],
      "rating": 5,
      "bg": Colors.red
    },
    {
      "name": "P2",
      "profilePic":
      "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["Mopping"],
      "rating": 5,
      "bg": Colors.amberAccent
    },
    {
      "name": "P3",
      "profilePic":
      "https://lh3.googleusercontent.com/a-/AAuE7mCfQn-gP_FJZUUU4GC4aSU1km9t_e5PL6zsV-NwdA=k-s48",
      "category": ["cleaning"],
      "rating": 5,
      "bg": Colors.blue
    }
  ];
  List filteredData = [];

  void onTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
    pageController.jumpToPage(index);
  }

  void tappedCategory(int index) {
    _selectedCategoryIndex = index;
    _selectedUserType = array[index];
    _filterData();
  }

  @override
  void initState() {
    super.initState();
    _filterData();
  }

  _filterData() {
    filteredData = _selectedCategoryIndex >= 0 ? data.where((element) => element["category"].contains(_selectedUserType)).toList() : data;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
          child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
            Padding(
              padding:  EdgeInsets.symmetric(vertical: 5),
              child: Container(
                // color: Colors.purple,
                  // margin: const EdgeInsets.only(top: 45, bottom: 15),
                  padding: const EdgeInsets.only(left: 20, right: 20),
                  child: Row(
                      children: [
                        Expanded(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                              children:  [
                            Text("Bengaluru"),
                            Text("R.T Nagar")
                          ]),
                        ),
                        customContainer(iconData: Icons.search,),
                        SizedBox(width: 10,),
                        customContainer(iconData: Icons.notifications,),
                      ])),
            ),
             Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [

                  Padding(
                    padding:  EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child:
                      Text(
                        'Popular Services',
                      ),
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 12,
                    child: ListView(
                      shrinkWrap: true,
                      scrollDirection: Axis.horizontal,
                      children: List<Widget>.generate(
                          array.length, // place the length of the array here
                              (int index) {
                            return Container(
                              margin: const EdgeInsets.all(2.0),
                              child: GestureDetector(
                                onTap: () {
                                  tappedCategory(index);
                                },
                                child: Chip(label: Text(array[index])),
                              ),
                            );
                          }).toList(),
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 6,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      // physics: NeverScrollableScrollPhysics(),
                      itemCount: filteredData.length,
                      itemBuilder: (context, index) {
                        var item = filteredData[index];
                        return Padding(
                          padding:  EdgeInsets.symmetric(horizontal: 10),
                          child: Container(
                            color: item['bg'],
                            child: Center(
                              child: Padding(
                                padding:  EdgeInsets.symmetric(horizontal: 20),
                                child: Text(item["name"].toString()),
                              ),
                            ),
                          ),
                        );
                      },
                      // This next line does the trick.
                    ),
                  ),
                  Padding(
                    padding:  EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                    child: Row(
                      mainAxisAlignment:MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          'Popular Products',
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child:
                          Text(
                            'View All',
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 6,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: product_data.length,
                      itemBuilder: (context, index) {
                        var item = product_data[index];
                        return Padding(
                          padding:  EdgeInsets.symmetric(horizontal: 10),
                          child: Container(
                            color: item['bg'],
                            child: Center(
                              child: Padding(
                                padding:  EdgeInsets.symmetric(horizontal: 60),
                                child: Text(item["name"].toString()),
                              ),
                            ),
                          ),
                        );
                      },
                      // This next line does the trick.
                    ),
                  ),
                  Padding(
                    padding:  EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                    child: Row(
                      mainAxisAlignment:MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          'Our Products',
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child:
                          Text(
                            'View All',
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 6,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: product_data.length,
                      itemBuilder: (context, index) {
                        var item = product_data[index];
                        return Padding(
                          padding:  EdgeInsets.symmetric(horizontal: 10),
                          child: Container(
                            color: item['bg'],
                            child: Center(
                              child: Padding(
                                padding:  EdgeInsets.symmetric(horizontal: 60),
                                child: Text(item["name"].toString()),
                              ),
                            ),
                          ),
                        );
                      },
                      // This next line does the trick.
                    ),
                  ),
                  Padding(
                    padding:  EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                    child: Row(
                      mainAxisAlignment:MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          'New Products',
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child:
                          Text(
                            'View All',
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 6,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: product_data.length,
                      itemBuilder: (context, index) {
                        var item = product_data[index];
                        return Padding(
                          padding:  EdgeInsets.symmetric(horizontal: 10),
                          child: Container(
                            color: item['bg'],
                            child: Center(
                              child: Padding(
                                padding:  EdgeInsets.symmetric(horizontal: 60),
                                child: Text(item["name"].toString()),
                              ),
                            ),
                          ),
                        );
                      },
                      // This next line does the trick.
                    ),
                  ),
                ],
              ),
            ),
          )
          ]),
        ),

        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
            BottomNavigationBarItem(
                icon: Icon(Icons.cleaning_services), label: 'House Keeping'),
            BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
            BottomNavigationBarItem(
                icon: Icon(Icons.account_balance_wallet), label: 'Wallet'),
            BottomNavigationBarItem(
                icon: Icon(Icons.bookmarks), label: 'Bookmarked'),
            BottomNavigationBarItem(
                icon: Icon(Icons.local_convenience_store), label: 'Store'),
            BottomNavigationBarItem(
                icon: Icon(Icons.notifications), label: 'Notifications'),
            BottomNavigationBarItem(
                icon: Icon(Icons.assessment), label: 'Notifications'),
            BottomNavigationBarItem(icon: Icon(Icons.person), label: 'Profile'),
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: Colors.blue,
          unselectedItemColor: Colors.grey,
          onTap: onTapped,
        ));
  }

  Widget customContainer({required IconData iconData}){
    return Container(
      width: 45,
      height: 45,
      padding: const EdgeInsets.only(left: 0, right: 0),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15),
          color: Colors.red),
      child:  Icon(
        iconData,
        color: Colors.white,
      ),
    );
  }

}

檢查我在下面的代碼中提到的注釋

   Padding(
              padding:  EdgeInsets.symmetric(vertical: 5),
              child: Container(
                // color: Colors.purple,
                  // margin: const EdgeInsets.only(top: 45, bottom: 15),
                  padding: const EdgeInsets.only(left: 20, right: 20),
                  child: Row(
                      children: [
                        Expanded(
                          child: Column(
//Note : Add this 
                            crossAxisAlignment: CrossAxisAlignment.start,
                              children:  [
                            Text("Bengaluru"),
                            Text("R.T Nagar")
                          ]),
                        ),
                        customContainer(iconData: Icons.search,),
                        SizedBox(width: 10,),
                        customContainer(iconData: Icons.notifications,),
                      ])),
            ),

最后 Expanded => single child scroll view =>column => in Column 您需要為所有子項定義高度,如下面的代碼並檢查注釋:

 Expanded(
            child: SingleChildScrollView(
              child: Column(
                children: [

                  Padding(
                    padding:  EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child:
                      Text(
                        'Popular Services',
                      ),
                    ),
                  ),
//Note : Here I remove row and used container with height and used list view with horizontal scroll
                  Container(
                    height: MediaQuery.of(context).size.height / 12,
                    child: ListView(
                      shrinkWrap: true,
                      scrollDirection: Axis.horizontal,
                      children: List<Widget>.generate(
                          array.length, // place the length of the array here
                              (int index) {
                            return Container(
                              margin: const EdgeInsets.all(2.0),
                              child: GestureDetector(
                                onTap: () {
                                  tappedCategory(index);
                                },
                                child: Chip(label: Text(array[index])),
                              ),
                            );
                          }).toList(),
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 6,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      // physics: NeverScrollableScrollPhysics(),
                      itemCount: filteredData.length,
                      itemBuilder: (context, index) {
                        var item = filteredData[index];
                        return Padding(
                          padding:  EdgeInsets.symmetric(horizontal: 10),
                          child: Container(
                            color: item['bg'],
                            child: Center(
                              child: Padding(
                                padding:  EdgeInsets.symmetric(horizontal: 20),
                                child: Text(item["name"].toString()),
                              ),
                            ),
                          ),
                        );
                      },
                      // This next line does the trick.
                    ),
                  ),
                  Padding(
                    padding:  EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                    child: Row(
                      mainAxisAlignment:MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          'Popular Products',
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child:
                          Text(
                            'View All',
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 6,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: product_data.length,
                      itemBuilder: (context, index) {
                        var item = product_data[index];
                        return Padding(
                          padding:  EdgeInsets.symmetric(horizontal: 10),
                          child: Container(
                            color: item['bg'],
                            child: Center(
                              child: Padding(
                                padding:  EdgeInsets.symmetric(horizontal: 60),
                                child: Text(item["name"].toString()),
                              ),
                            ),
                          ),
                        );
                      },
                      // This next line does the trick.
                    ),
                  ),
                  Padding(
                    padding:  EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                    child: Row(
                      mainAxisAlignment:MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          'Our Products',
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child:
                          Text(
                            'View All',
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 6,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: product_data.length,
                      itemBuilder: (context, index) {
                        var item = product_data[index];
                        return Padding(
                          padding:  EdgeInsets.symmetric(horizontal: 10),
                          child: Container(
                            color: item['bg'],
                            child: Center(
                              child: Padding(
                                padding:  EdgeInsets.symmetric(horizontal: 60),
                                child: Text(item["name"].toString()),
                              ),
                            ),
                          ),
                        );
                      },
                      // This next line does the trick.
                    ),
                  ),
                  Padding(
                    padding:  EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                    child: Row(
                      mainAxisAlignment:MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          'New Products',
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child:
                          Text(
                            'View All',
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height / 6,
                    child: ListView.builder(
                      scrollDirection: Axis.horizontal,
                      itemCount: product_data.length,
                      itemBuilder: (context, index) {
                        var item = product_data[index];
                        return Padding(
                          padding:  EdgeInsets.symmetric(horizontal: 10),
                          child: Container(
                            color: item['bg'],
                            child: Center(
                              child: Padding(
                                padding:  EdgeInsets.symmetric(horizontal: 60),
                                child: Text(item["name"].toString()),
                              ),
                            ),
                          ),
                        );
                      },
                      // This next line does the trick.
                    ),
                  ),
                ],
              ),
            ),
          )

做這個。

  1. 您在代碼中為父 Row 小部件中的 3 個小部件提供了 spaceBetween。 但如果你想要兩個將兩個容器向右移動,將列向左移動:-

     Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Column(children: const [ Text("Bengaluru"), Text("RT Nagar") ]), Row( children: [ Container( width: 45, height: 45, padding: const EdgeInsets.only(left: 0, right: 0), decoration: BoxDecoration( borderRadius: BorderRadius.circular(15), color: Colors.red), child: const Icon( Icons.search, color: Colors.white, ), ), Container( width: 45, height: 45, padding: const EdgeInsets.only(left: 0, right: 0), decoration: BoxDecoration( borderRadius: BorderRadius.circular(15), color: Colors.red), child: const Icon( Icons.notifications, color: Colors.white, ), ], ), ])),
  2. 如果您想限制要顯示的項目數量(例如,列表視圖構建器中的前 3 個項目)

     ListView.builder( scrollDirection: Axis.horizontal, itemCount: filteredData == null ? 0 : (filteredData.length > 3 ? 3 : filteredData .length),// here

當查看所有點擊時,與您的代碼相同

ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: filteredData.length,// here
  1. 如果高度和寬度響應當前設備的方向。 您應該使用 MediaQuery:- 它控制設備當前窗口的大小。

     size = MediaQuery.of(context).size; height = size.height; width = size.width;

檢查此鏈接:媒體查詢鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM