반응형
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebViewApp extends StatefulWidget {
@override
State<WebViewApp> createState() => _WebViewAppState();
}
class _WebViewAppState extends State<WebViewApp> {
late WebViewController _controller;
bool buttonshow = false;
void scrollToTop(){
_controller.runJavascript("window.scrollTo({top: 0, behavior: 'smooth'});");
floatingButtonVisibility();
}
void floatingButtonVisibility() async {
int y = await _controller.getScrollY();
if(y>50){
setState(() {
buttonshow = true;
});
}else {
setState(() {
buttonshow = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter WebView'),
),
body: WebView(
initialUrl: 'https://flutter.dev',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
},
gestureRecognizers: Set()
..add(
Factory<VerticalDragGestureRecognizer>(() => VerticalDragGestureRecognizer()
..onDown = (tap) {
floatingButtonVisibility();
}))
),
floatingActionButton: Visibility(
visible: buttonshow,
child: FloatingActionButton(
onPressed: () {
scrollToTop();
},
backgroundColor: Colors.blue,
child: const Icon(Icons.navigation),
),
),
);
}
}
반응형
'[====== Development ======] > Flutter' 카테고리의 다른 글
[Flutter] Visual Studio Code 설정 (0) | 2024.03.01 |
---|---|
Dart 버전 업그레이드 및 Pub 버전 업그레이드 후 Firebase 관련 문제로 빌드 오류 발생시 대응 방법 (1) | 2024.02.11 |
Flutter - DropdownButton 스타일 샘플 (0) | 2023.12.17 |
Flutter - Double Siver AppBar (0) | 2023.12.17 |
Flutter release 로 실행하는 방법 (0) | 2023.12.17 |