Как добавить несколько детей к телу лески - PullRequest
0 голосов
/ 25 апреля 2020

Я пытаюсь записать несколько child в flutter метку body: тега.

Я новичок в трепетании и не знаю, как сделать несколько детей внутри тега body. это то, что я пробовал.

return new Scaffold(
      appBar: AppBar(
        leading: new Icon(Icons.live_tv),
        title: const Text('SnapNews'),
        backgroundColor: Colors.red,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.help),
            onPressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (_) => Help()));
            },
          ),
        ],
      ),
      body: new ListView.builder(
        itemCount: 10,
        itemBuilder: (context, index) {
          return new GestureDetector(
            onTap: () {
              print("tapped");
            },
            child: new Padding(
              padding: const EdgeInsets.all(8.0),
              child: new Container(
                color: Colors.grey,
                height: 100.0,
              ),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton.extended(
        elevation: 4.0,
        icon: const Icon(Icons.camera_enhance),
        label: const Text('Snap'),
        backgroundColor: Colors.red,
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (_) => CameraScreen()),
          );
        },
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomAppBar(
        child: new Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            IconButton(
              icon: Icon(null),
              onPressed: () {},
            ),
            IconButton(
              icon: Icon(null),
              onPressed: () {},
            )
          ],
        ),
      ),
    );

И это выдает GUI, как показано ниже:

enter image description here

Но я хочу добавить Search Bar поверх этого ListView

Как это сделать, добавив еще одного ребенка / детей в тег body:

enter image description here

Спасибо.

Ответы [ 3 ]

2 голосов
/ 25 апреля 2020

Вы можете поместить ListView.builder в ListView.

Редактировать: чтобы сделать SearchBar () недоступным для прокрутки, поместите его внутри Column вместо ListView.

        body: Column(
          children: [
            SearchBar(),
            Expanded(
              child: ListView.builder(
                itemCount: 10,
                itemBuilder: (context, index) {
                  return new GestureDetector(
                    onTap: () {
                      print("tapped");
                    },
                    child: new Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: new Container(
                        color: Colors.grey,
                        height: 100.0,
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
0 голосов
/ 25 апреля 2020

Рекомендуется размещать строку поиска внутри виджета AppBar внутри title. Прямо сейчас вы используете этот title: const Text('SnapNews'),, поэтому вы можете переключаться между виджетом Text('SnapNes') и панелью поиска, скажем, TextField. Поместите search button и cancel button в actions на панели приложений, чтобы получить желаемый эффект. Например:

bool isSearching = false;

appBar: AppBar(
    title: !isSearching
        ? Text('SnapNews')
        : TextField(
            onChanged: (value) {
              // search logic
            },
            style: TextStyle(color: Colors.white),
            decoration: InputDecoration(
                icon: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
                hintText: "Search News Here",
                hintStyle: TextStyle(color: Colors.white)),
          ),
    actions: <Widget>[
      isSearching
          ? IconButton(
              icon: Icon(Icons.cancel),
              onPressed: () {
                setState(() {
                  this.isSearching = false;
                });
              },
            )
          : IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                setState(() {
                  this.isSearching = true;
                });
              },
            )
    ],
  ),

Проверьте следующую ссылку .

0 голосов
/ 25 апреля 2020

вам нужно дать body Column виджет в качестве ребенка, а затем вы можете использовать столько детей, сколько захотите.

пример

Scaffold(
  body: Column(
    children: <Widget>[
      //all the children widgets that you need
    ],
  ),
),
...