admin管理员组

文章数量:1390555

When I open the keyboard on one page, it will trigger the rebuild of components on other pages, resulting in frame dropping. How can I avoid triggering the rebuild of other pages? Since invoking the keyboard will affect the size of the current window, I found that components involving MediaQuery in the possible components will all be rebuilt (components such as Scaffold, TabBar, ListView, etc.). How can I make invoking the keyboard on the current page not affect the rebuild of other pages? I have set resizeToAvoidBottomInset to false. This problem has been bothering me for a long time. How can I solve this problem?

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:responsive_sizer_example/homepage.dart';
import 'package:sizer/sizer.dart';
import 'package:flutter/cupertino.dart';

// import 'package:responsive_sizer/responsive_sizer.dart';
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('page1')),
      body: Column(
        children: [
          ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => PageTwo()),
                );
              },
              child: Text('page2')),
          Text('This is page1')
        ],
      ),
    );
  }
}

class PageTwo extends StatefulWidget {
  const PageTwo();

  @override
  State<PageTwo> createState() => _PageTwoState();
}

class _PageTwoState extends State<PageTwo> with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(length: 3, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('page2'),
        ),
        body: Column(
          children: [
            TabBar(
              controller: _tabController,
              tabs: const [
                Tab(text: 'Tab 1'),
                Tab(text: 'Tab 2'),
                Tab(text: 'Tab 3'),
              ],
            ),
            ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => PageThree()),
                  );
                },
                child: Text('page3')),
            Container(
              height: 200,
              child: ListView.builder(
                itemCount: 2000,
                itemExtent: 20,
                itemBuilder: (context, index) {
                  return Container(child: Text('Item $index'),);
                },
              ),
            )
          ],
        ));
  }
}

class PageThree extends StatefulWidget {
  const PageThree();

  @override
  State<PageThree> createState() => _PageThreeState();
}

class _PageThreeState extends State<PageThree> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        title: Text('Input'),
      ),
      body: Column(children: [
        Container(
          height: 100,
        ),
        CupertinoTextField()
      ]),
    );
  }
}

本文标签: