flutter-開發(fā)中恰力,各種組件的應(yīng)用場景【二】

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,
        ),
      ),
    );
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末梯找,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子益涧,更是在濱河造成了極大的恐慌锈锤,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件闲询,死亡現(xiàn)場離奇詭異久免,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)嘹裂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進(jìn)店門妄壶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來摔握,“玉大人寄狼,你說我怎么就攤上這事。” “怎么了泊愧?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵伊磺,是天一觀的道長。 經(jīng)常有香客問我删咱,道長屑埋,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任痰滋,我火速辦了婚禮摘能,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘敲街。我一直安慰自己团搞,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布多艇。 她就那樣靜靜地躺著逻恐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪峻黍。 梳的紋絲不亂的頭發(fā)上复隆,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機(jī)與錄音姆涩,去河邊找鬼挽拂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛阵面,可吹牛的內(nèi)容都是我干的轻局。 我是一名探鬼主播,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼样刷,長吁一口氣:“原來是場噩夢啊……” “哼仑扑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起置鼻,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤镇饮,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后箕母,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體储藐,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年嘶是,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钙勃。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,096評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡聂喇,死狀恐怖辖源,靈堂內(nèi)的尸體忽然破棺而出蔚携,到底是詐尸還是另有隱情,我是刑警寧澤克饶,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布酝蜒,位于F島的核電站,受9級特大地震影響矾湃,放射性物質(zhì)發(fā)生泄漏亡脑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一邀跃、第九天 我趴在偏房一處隱蔽的房頂上張望霉咨。 院中可真熱鬧,春花似錦拍屑、人聲如沸躯护。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽棺滞。三九已至,卻和暖如春矢渊,著一層夾襖步出監(jiān)牢的瞬間继准,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工矮男, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留移必,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓毡鉴,卻偏偏與公主長得像崔泵,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子猪瞬,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內(nèi)容