Search code examples
flutterwebview

WebView without height inside Scroll not working


On Flutter...I tried to add Webview inside all the available containers that support scroll but it didn't work on Android.

ListView(
          shrinkWrap: true,
          children: <Widget>[
            Text("hiiiiiiiiiiiii "),
            Text("hiiiiiiiiiiiii "),
            Text("hiiiiiiiiiiiii "),
           Expanded(
                child: WebView(
                    key: Key("webview1"),
                    debuggingEnabled:  true,
                    javascriptMode: JavascriptMode.unrestricted,
                    initialUrl: "https://flutter.dev/")),
            Text("hiiiiiiiiiiiii "),
            Text("hiiiiiiiiiiiii "),
            Text("hiiiiiiiiiiiii "),
          ],
)

Solution

  • If you want to put your scrollable WebView between other widget's and WebView will be scrollable, other's widget's are not, then just change your ListView for Column like that:

    Column(
      children: <Widget>[
        Text("hiiiiiiiiiiiii "),
        Text("hiiiiiiiiiiiii "),
        Container(
          height: 300,
          child: WebView(
              key: Key("webview1"),
              debuggingEnabled: true,
              javascriptMode: JavascriptMode.unrestricted,
              initialUrl: "https://flutter.dev/")
        ),
        Text("hiiiiiiiiiiiii "),
        Text("hiiiiiiiiiiiii "),
    )
    

    But if u want to have some scrollable widgets on top of WebView, then try CustomScrollView or SliverList.

    Can you tell what exact logic you are trying to make?

    additions:

    If you want to have scrollable area and some WebView inside, you can have to know height of WebView:

    ListView(
      physics: ClampingScrollPhysics(),
      children: <Widget>[
        Text("hiiiiiiiiiiiii "),
        Text("hiiiiiiiiiiiii "),
        ConstrainedBox(
          constraints: BoxConstraints(maxHeight: 10000),
            child: WebView(
              gestureRecognizers: Set()
                ..add(
                  Factory<VerticalDragGestureRecognizer>(
                      () => VerticalDragGestureRecognizer(),
                  ), // or null
                ),
                key: Key("webview1"),
                debuggingEnabled: true,
                javascriptMode: JavascriptMode.unrestricted,
                initialUrl: "https://flutter.dev/")),
        Text("hiiiiiiiiiiiii "),
        Text("hiiiiiiiiiiiii "),
      ],
    );
    

    Height of WebView you could get dynamically, inspiration is here: https://gist.github.com/PonnamKarthik/877a90917a576ecff613d5169680d02c