Container組件嵌套chid【Container】可岂,chid【Container】大小調(diào)整技巧
場景:有一個(gè)全局大小的父Container設(shè)置背景色,內(nèi)部有一個(gè)子Container負(fù)責(zé)裝配各種組件践樱,有另一種背景色厂画,且需隨裝配的組件自適應(yīng)大小【用于彈出式遮蓋層的布局】
問題:子Container會(huì)跟隨父Container大小,無法自適應(yīng)
技巧:如代碼所示拷邢,使用Stack【或者Column袱院,Algin,Center等組件】包一層
Container(
width: ToolScreenWidth,
height: ToolScreenHeight,
color: Color.fromRGBO(0, 0, 0, 0.5),
child: Stack(
children: [
Container(
color: Colors.white,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(height: ToolStatusBarHeight,),
Container(
width: ToolScreenWidth,
height: 70,
color: Colors.red,
),
Container(
width: ToolScreenWidth,
height: 70,
color: Colors.yellow,
),
Container(
width: ToolScreenWidth,
height: 90,
color: Colors.greenAccent,
),
],
),
),
],
),
);
鍵盤遮蓋輸入框問題
場景:在鍵盤彈出時(shí)需要在鍵盤上附加一個(gè)工具view【Container】瞭稼,在輸入框【TextField】外層套一個(gè)滾動(dòng)組件【SingleChildScrollView】忽洛,默認(rèn)輸入框會(huì)處理鍵盤遮蓋問題【自動(dòng)向上滾動(dòng)】,但無法處理工具view的遮蓋环肘,在這里需要監(jiān)聽鍵盤的滾動(dòng)事件欲虚,以及工具view的位置,和輸入框的位置以及滾動(dòng)組件的滾動(dòng)位置悔雹,計(jì)算出差值复哆,進(jìn)行滾動(dòng)。
輸入框遮擋方案
///基礎(chǔ)
import 'package:flutter/material.dart';
import 'package:firstproject/app/AppBarCustomBase.dart';
///工具
import 'package:firstproject/until/Tool.dart';
class CrosswordGameSetingPage extends StatefulWidget {
final arguments;
CrosswordGameSetingPage({this.arguments, Key? key}) : super(key: key) {
print("看看頁面參數(shù)---${this.arguments}");
}
@override
State<CrosswordGameSetingPage> createState() =>
_CrosswordGameSetingPageState();
}
class _CrosswordGameSetingPageState extends State<CrosswordGameSetingPage> with WidgetsBindingObserver {
ScrollController _bgScrController = ScrollController();
///工具UI-GlobalKey
GlobalKey _toolViewlKey = GlobalKey();
///輸入框
GlobalKey _filelKey = GlobalKey();
///初始化方法
void initState() {
super.initState();
//監(jiān)聽滾動(dòng)事件腌零,打印滾動(dòng)位置
_bgScrController.addListener(() {
print("滾動(dòng)位置變化--${_bgScrController.offset}"); //打印滾動(dòng)位置
});
///監(jiān)聽鍵盤高度變化
WidgetsBinding.instance.addObserver(this);
}
///在頁面高度發(fā)生變化的時(shí)候會(huì)觸發(fā)該回調(diào)
@override
void didChangeMetrics() {
super.didChangeMetrics();
WidgetsBinding.instance.addPostFrameCallback((_) {
print('鍵盤高度---${MediaQuery.of(context).viewInsets.bottom}');
});
}
///析構(gòu)
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Widget _getBodyWidget(BuildContext context) {
return Container(
width: ToolScreenWidth,
height: ToolScreenHeight,
color: Colors.white,
child: Stack(
alignment: Alignment.topCenter,
children: [
Positioned(
child:Container(
width: ToolScreenWidth,
height: ToolScreenHeight,
color: ToolColorRandomColor(),
child: SingleChildScrollView(
controller: _bgScrController,
child:Column(
children: [
Container(
width: ToolScreenWidth,
height: 300,
color: ToolColorRandomColor(),
),
Container(
width: ToolScreenWidth,
height: 200,
color: ToolColorRandomColor(),
),
Container(
width: ToolScreenWidth,
height: 40,
child: TextField(
key: _filelKey,
controller: TextEditingController(text: "輸入框"),
onTap: (){
judgeKeyboardOverspreadTextFile();
},
),
),
Container(
width: ToolScreenWidth,
height: 200,
color: ToolColorRandomColor(),
),
],
)),
),
),
Positioned(
bottom: 0,
child: Container(
key: _toolViewlKey,
width: ToolScreenWidth,
height: 100,
color: Color.fromRGBO(0, 0, 0, 0.5),
child: Text("工具欄"),
),
)
],
),
);
}
///處理鍵盤遮蓋問題
void judgeKeyboardOverspreadTextFile() {
Future.delayed(Duration(milliseconds: 800), () {
final toolViewBox =
this._toolViewlKey.currentContext?.findRenderObject() as RenderBox?;
double boxY = toolViewBox?.localToGlobal(Offset.zero).dy ?? 0;
print(
"我想看看--提示view位置---${toolViewBox?.localToGlobal(Offset.zero)}---${toolViewBox?.size}");
GlobalKey filelKey = _filelKey;
final filebox = filelKey.currentContext?.findRenderObject() as RenderBox?;
double fileboxY = filebox?.localToGlobal(Offset.zero).dy ?? 0;
double fileboxHeight = filebox?.size.height ?? 0;
print(
"我想看看--file位置---${filebox?.localToGlobal(Offset.zero)}---${filebox?.size}");
if (fileboxY + fileboxHeight >= boxY) {
double? scroY =
fileboxY - boxY + fileboxHeight + _bgScrController.offset + 10;
this._bgScrController.animateTo(
scroY,
duration: Duration(milliseconds: 200),
curve: Curves.ease,
);
print("我想看看--滾動(dòng)位置---${scroY}---");
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: RawKeyboardListener(
autofocus: true,
onKey: (event) {
print("鍵盤----event=${event.toString()}");
},
focusNode: FocusNode(),
child: _getBodyWidget(context),
),
appBar: AppBarCustomBase(
barHeight: 100,
backgroundImageName: 'assets/images/bg_375x90.png',
leadingWidget: GestureDetector(
child: Image.asset(
'assets/images/icon_back.png',
width: 66,
height: 41,
fit: BoxFit.cover,
),
onTap: () {
Navigator.of(context).pop();
},
),
trailingWidget: Image.asset(
'assets/images/icon_share.png',
width: 66,
height: 41,
),
),
);
}
}