Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Searchfield to use OverlayPortal #86

Open
maheshj01 opened this issue May 26, 2023 · 4 comments
Open

Migrate Searchfield to use OverlayPortal #86

maheshj01 opened this issue May 26, 2023 · 4 comments
Labels
P3 Low Priority: A bug or a feature request we plan to work on in near future (default) Proposal A detailed proposal for change to Searchfield. tech-debt Issues regarding refactor and code quality

Comments

@maheshj01
Copy link
Owner

maheshj01 commented May 26, 2023

This will solve

sample demo code
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(
          title: "My Home Page",
        ));
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(8.0),
              child: CustomWidget(),
            ),
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class CustomWidget extends StatefulWidget {
  const CustomWidget({Key? key}) : super(key: key);
  @override
  State<CustomWidget> createState() => _CustomWidgetState();
}

class _CustomWidgetState extends State<CustomWidget> {
  final ScrollController _scrollController = ScrollController();

  Widget _list() {
    return Container(
      height: 5 * 40,
      child: Scrollbar(
          controller: _scrollController,
          thumbVisibility: true,
          child: ListView.builder(
              controller: _scrollController,
              padding: EdgeInsets.zero,
              itemCount: 20,
              itemBuilder: (context, index) => ListTile(
                    title: Text('item $index'),
                    onTap: () {
                      _portalController.hide();
                    },
                  ))),
    );
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((x) {
      final renderBox = gkey.currentContext!.findRenderObject() as RenderBox;
      tSize = renderBox.size;
      offset = renderBox.localToGlobal(Offset.zero);
    });
  }

  late Offset offset;
  late Size tSize;
  GlobalKey gkey = GlobalKey();
  final OverlayPortalController _portalController = OverlayPortalController();
  @override
  Widget build(BuildContext context) {
    Widget textfield() => TextFormField(
          key: gkey,
          decoration: InputDecoration(
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(
                color: Colors.black.withOpacity(0.8),
              ),
            ),
            border: const OutlineInputBorder(
              borderSide: BorderSide(color: Colors.red),
            ),
          ),
          onTap: () {
            print("offset: $offset, size= $tSize");
            if (!_portalController.isShowing) {
              _portalController.show();
            }
          },
          onChanged: (query) {},
        );
    
    final size = MediaQuery.of(context).size;
    final padding = MediaQuery.of(context).padding;
    print(padding);
//     final rightOffset = size.width - tSize.width - offset.dx;
    return OverlayPortal(
        controller: _portalController,
        overlayChildBuilder: (BuildContext context) {
          return Positioned(
              right: 0,
              left: offset.dx,
              top: offset.dy + tSize.height, // + textfield height
              child: Material(color: Colors.red, child: _list()));
        },
        child: textfield());
  }
}
@maheshj01 maheshj01 added Proposal A detailed proposal for change to Searchfield. P3 Low Priority: A bug or a feature request we plan to work on in near future (default) labels May 26, 2023
@maheshj01
Copy link
Owner Author

Update: Currently blocked by this issue flutter/flutter#78746 (comment)

@lucasjinreal
Copy link

Release mode still can not use?

@maheshj01
Copy link
Owner Author

maheshj01 commented Feb 7, 2024

Release mode still can not use?

@lucasjinreal It works on release.

Edit: This issue does not block any capability of the searchfield.

@maheshj01 maheshj01 added the tech-debt Issues regarding refactor and code quality label Mar 6, 2024
@sabari7320
Copy link

@maheshj01 sir when i close the keyboard all the suggestion showing from scratch not showing hat i have entered char in the textfield
https://drive.google.com/file/d/132iZYWgLzDNMllLGyOlkA15SayzkrcZe/view check this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 Low Priority: A bug or a feature request we plan to work on in near future (default) Proposal A detailed proposal for change to Searchfield. tech-debt Issues regarding refactor and code quality
Projects
None yet
Development

No branches or pull requests

3 participants