Флаттер: - Добавление нового виджета в дерево виджетов, заставляет исчезать другие виджеты - PullRequest
1 голос
/ 03 мая 2020

Привет, я пытался добавить несколько виджетов в виджет столбца во флаттере, но каждый раз, когда я добавляю второй виджет, первый виджет исчезает, как если бы виджет столбца разбился, я запускаю его на своем телефоне android, используя vscode консоль отладки не показывает ошибок вообще. Я прилагаю код ниже.

import 'package:flutter/material.dart';

import 'package:thejointapp/Appbar.dart';
import 'package:thejointapp/Grid.dart';
import 'package:thejointapp/SubjectSelector.dart';

void main() => runApp(Home());

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
   return HomeState();
  }
}

class HomeState extends State<Home> {
Widget build(BuildContext context) {
return MaterialApp(
    title: 'Home',
    debugShowCheckedModeBanner: false,
    theme: ThemeData(
      primaryColor: Colors.red,
      accentColor: Color(0xFFFEF9EB),
    ),
    home: Scaffold(
      appBar: Appbar(),
      body: Column(
        children: <Widget>[
          SubjectSelector(),
          GridView.count(
            crossAxisCount: 2,
            children: List.generate(100, (index) {
              return (Center(
                  child: Text(
                'Item $index',
                style: Theme.of(context).textTheme.headline,
                // TextStyle(
                //     color: Colors.white,
                //     fontSize: 20.0,
                //     fontWeight: FontWeight.bold),
              )));
            }),
          )
        ],
      ),
    ));
}
}

1 Ответ

1 голос
/ 03 мая 2020

Вам просто нужно использовать виджет Expand в Gridview, как показано ниже

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

class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return HomeState();
  }
}

class HomeState extends State<HomeScreen> {
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Home',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primaryColor: Colors.red,
          accentColor: Color(0xFFFEF9EB),
        ),
        home: Scaffold(
          body: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).size.height * 0.1,
                color: Colors.green,
              ),
              Expanded(
                child: GridView.count(
                  crossAxisCount: 2,
                  children: List.generate(100, (index) {
                    return (Center(
                        child: Text(
                      'Item $index',
                      style: Theme.of(context).textTheme.headline,
                      // TextStyle(
                      //     color: Colors.white,
                      //     fontSize: 20.0,
                      //     fontWeight: FontWeight.bold),
                    )));
                  }),
                ),
              ),
              Container(
                height: MediaQuery.of(context).size.height*0.1,
                color: Colors.yellow,
              ),
            ],
          ),
        ));
  }
}

И вывод будет следующий

enter image description here

И написание еще одного ответа для полноэкранного прокрутки с помощью gridview и других виджетов. Для этого вам необходимо использовать CustomScrollView с Silver, как показано ниже

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

    class HomeScreen extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        // TODO: implement createState
        return HomeState();
      }
    }

    class HomeState extends State<HomeScreen> {
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Home',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
              primaryColor: Colors.red,
              accentColor: Color(0xFFFEF9EB),
            ),
            home: Scaffold(
                appBar: new AppBar(),
                body: CustomScrollView(
                  slivers: [
                    SliverToBoxAdapter(
                      child: Container(
                        color: Colors.green,
                        height: MediaQuery.of(context).size.height*0.2,
                        child: Center(
                          child: Text("Top"),
                        ),
                      )
                    ),

                    SliverGrid(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,
                        childAspectRatio: 1.5,
                      ),
                      delegate: SliverChildBuilderDelegate(
                        (context, index) => Container(
                          margin: EdgeInsets.all(5.0),
                          color: Colors.yellow,
                        ),
                        childCount: 10,

                      ),
                    ),

                    SliverToBoxAdapter(
                        child: Container(
                          color: Colors.green,
                          height: MediaQuery.of(context).size.height*0.2,
                          child: Center(
                            child: Text("Bottom"),
                          ),
                        )
                    ),
                  ],
                )));
      }
    }

И вывод следующей программы, как следует


enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...