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

Need to override tap click on "Read More" & "Read Less". #76

Open
MOhsain opened this issue Jan 29, 2025 · 4 comments
Open

Need to override tap click on "Read More" & "Read Less". #76

MOhsain opened this issue Jan 29, 2025 · 4 comments

Comments

@MOhsain
Copy link

MOhsain commented Jan 29, 2025

I have a scenario in which when user click on "Read More" then i need to show a bottom sheet.
Currently I added GestureDetector on whole ReadMore widget. Tap is working on whole but not on the "Read More" text.
So i just need to override the onTap click.
Looking forward your swift response.
Thanks

@maRci002
Copy link
Collaborator

Can you check if this works for you?

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final isCollapsed = ValueNotifier<bool>(true);

  @override
  void initState() {
    super.initState();

    isCollapsed.addListener(() {
      final currentValue = isCollapsed.value;
      
      if (!currentValue) {
        // optionaly close back
        isCollapsed.value = false;
		
        // Open bottom sheet
        showModalBottomSheet<void>(
          context: context,
          ...
        );
      }
    });
  }

  @override
  void dispose() {
    isCollapsed.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ReadMoreText(
      'ReadMoreText',
      isCollapsed: isCollapsed,
    );
  }
}

@Sameer330
Copy link

Sameer330 commented Feb 4, 2025

I tried this code and function got executed. But for my requirement, I don't want the text to expand.

I tried the isExpandable property also. If it is set to true, the function won't execute.

class CustomReadMoreText extends StatefulWidget {
  const CustomReadMoreText({
    required this.text,
    this.onTap,
    super.key,
  });

  final String text;
  final Function()? onTap;

  @override
  State<CustomReadMoreText> createState() => CustomReadMoreTextState();
}

class CustomReadMoreTextState extends State<CustomReadMoreText> {
  final isCollapsed = ValueNotifier<bool>(true);

  @override
  void initState() {
    super.initState();

    isCollapsed.addListener(() {
      final currentValue = isCollapsed.value;

      if (!currentValue) {
        // optionaly close back
        isCollapsed.value = false;

        if (widget.onTap != null) {
          widget.onTap!();
        }
      }
    });
  }

  @override
  void dispose() {
    isCollapsed.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ReadMoreText(
      widget.text,
      // isExpandable: false,
      isCollapsed: isCollapsed,
      textAlign: TextAlign.left,
      trimMode: TrimMode.Line,
      postDataText: ' ',
      trimCollapsedText: 'more',
      trimExpandedText: ' less',
      style: const TextStyle(
        color: Color(0xFF545454),
        fontSize: 14,
        fontFamily: 'Avenir',
        fontWeight: FontWeight.w400,
      ),
      moreStyle: const TextStyle(
        color: Colors.black,
        fontSize: 14,
        fontFamily: 'Avenir',
        fontWeight: FontWeight.w500,
      ),
      lessStyle: const TextStyle(
        color: Colors.black,
        fontSize: 14,
        fontFamily: 'Avenir',
        fontWeight: FontWeight.w500,
      ),
    );
  }
}

@maRci002
Copy link
Collaborator

maRci002 commented Feb 4, 2025

Do not use isExpandable.

This one should work.

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  final isCollapsed = ValueNotifier<bool>(true);

  @override
  void initState() {
    super.initState();

    isCollapsed.addListener(() {
      final currentValue = isCollapsed.value;
      
      if (!currentValue) {
        isCollapsed.value = true; // Always set to true so Text will never expand
		
        // Open bottom sheet
        ScaffoldMessenger.of(context)
          ..hideCurrentSnackBar()
          ..showSnackBar(SnackBar(content: Text('Fake dialog opened')));
      }
    });
  }

  @override
  void dispose() {
    isCollapsed.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ReadMoreText(
      'ReadMoreText',
      isCollapsed: isCollapsed,
    );
  }
}

@Sameer330
Copy link

Will try this and let you know. Thanks for the reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants